Paul Martin
2016-04-27 c2188a840bc4153ae92112b04b2e06a90d3944aa
src/main/java/com/gitblit/utils/JsonUtils.java
@@ -46,6 +46,7 @@
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
@@ -54,9 +55,9 @@
/**
 * Utility methods for json calls to a Gitblit server.
 *
 *
 * @author James Moger
 *
 *
 */
public class JsonUtils {
@@ -68,7 +69,7 @@
   /**
    * Creates JSON from the specified object.
    *
    *
    * @param o
    * @return json
    */
@@ -82,9 +83,12 @@
    * 
    * @param json
    * @param clazz
    * @return an object
    * @return the deserialized object
    * @throws JsonParseException
    * @throws JsonSyntaxException
    */
   public static <X> X fromJsonString(String json, Class<X> clazz) {
   public static <X> X fromJsonString(String json, Class<X> clazz) throws JsonParseException,
         JsonSyntaxException {
      return gson().fromJson(json, clazz);
   }
@@ -92,16 +96,19 @@
    * Convert a json string to an object of the specified type.
    * 
    * @param json
    * @param clazz
    * @return an object
    * @param type
    * @return the deserialized object
    * @throws JsonParseException
    * @throws JsonSyntaxException
    */
   public static <X> X fromJsonString(String json, Type type) {
   public static <X> X fromJsonString(String json, Type type) throws JsonParseException,
         JsonSyntaxException {
      return gson().fromJson(json, type);
   }
   /**
    * Reads a gson object from the specified url.
    *
    *
    * @param url
    * @param type
    * @return the deserialized object
@@ -114,7 +121,7 @@
   /**
    * Reads a gson object from the specified url.
    *
    *
    * @param url
    * @param type
    * @return the deserialized object
@@ -127,7 +134,7 @@
   /**
    * Reads a gson object from the specified url.
    *
    *
    * @param url
    * @param type
    * @param username
@@ -146,7 +153,7 @@
   /**
    * Reads a gson object from the specified url.
    *
    *
    * @param url
    * @param clazz
    * @param username
@@ -165,7 +172,7 @@
   /**
    * Retrieves a JSON message.
    *
    *
    * @param url
    * @return the JSON message as a string
    * @throws {@link IOException}
@@ -205,7 +212,7 @@
   /**
    * Sends a JSON message.
    *
    *
    * @param url
    *            the url to write to
    * @param json
@@ -219,7 +226,7 @@
   /**
    * Sends a JSON message.
    *
    *
    * @param url
    *            the url to write to
    * @param json
@@ -268,17 +275,16 @@
      GsonBuilder builder = new GsonBuilder();
      builder.registerTypeAdapter(Date.class, new GmtDateTypeAdapter());
      builder.registerTypeAdapter(AccessPermission.class, new AccessPermissionTypeAdapter());
      builder.setPrettyPrinting();
      if (!ArrayUtils.isEmpty(strategies)) {
         builder.setExclusionStrategies(strategies);
      }
      return builder.create();
   }
   private static class GmtDateTypeAdapter implements JsonSerializer<Date>, JsonDeserializer<Date> {
   public static class GmtDateTypeAdapter implements JsonSerializer<Date>, JsonDeserializer<Date> {
      private final DateFormat dateFormat;
      private GmtDateTypeAdapter() {
      public GmtDateTypeAdapter() {
         dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
         dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
      }
@@ -297,7 +303,7 @@
            JsonDeserializationContext jsonDeserializationContext) {
         try {
            synchronized (dateFormat) {
               Date date = dateFormat.parse(jsonElement.getAsString());
               Date date = dateFormat.parse(jsonElement.getAsString());
               return new Date((date.getTime() / 1000) * 1000);
            }
         } catch (ParseException e) {
@@ -305,7 +311,7 @@
         }
      }
   }
   private static class AccessPermissionTypeAdapter implements JsonSerializer<AccessPermission>, JsonDeserializer<AccessPermission> {
      private AccessPermissionTypeAdapter() {
@@ -320,7 +326,7 @@
      @Override
      public synchronized AccessPermission deserialize(JsonElement jsonElement, Type type,
            JsonDeserializationContext jsonDeserializationContext) {
         return AccessPermission.fromCode(jsonElement.getAsString());
         return AccessPermission.fromCode(jsonElement.getAsString());
      }
   }
@@ -335,10 +341,12 @@
         this.fieldName = fqfn.substring(fqfn.lastIndexOf(".") + 1);
      }
      @Override
      public boolean shouldSkipClass(Class<?> arg0) {
         return false;
      }
      @Override
      public boolean shouldSkipField(FieldAttributes f) {
         return (f.getDeclaringClass() == c && f.getName().equals(fieldName));
      }