| | |
| | | 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;
|
| | |
| | |
|
| | | /**
|
| | | * Utility methods for json calls to a Gitblit server.
|
| | | * |
| | | *
|
| | | * @author James Moger
|
| | | * |
| | | *
|
| | | */
|
| | | public class JsonUtils {
|
| | |
|
| | |
| | |
|
| | | /**
|
| | | * Creates JSON from the specified object.
|
| | | * |
| | | *
|
| | | * @param o
|
| | | * @return json
|
| | | */
|
| | |
| | | *
|
| | | * @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);
|
| | | }
|
| | |
|
| | |
| | | * 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
|
| | |
| | |
|
| | | /**
|
| | | * Reads a gson object from the specified url.
|
| | | * |
| | | *
|
| | | * @param url
|
| | | * @param type
|
| | | * @return the deserialized object
|
| | |
| | |
|
| | | /**
|
| | | * Reads a gson object from the specified url.
|
| | | * |
| | | *
|
| | | * @param url
|
| | | * @param type
|
| | | * @param username
|
| | |
| | |
|
| | | /**
|
| | | * Reads a gson object from the specified url.
|
| | | * |
| | | *
|
| | | * @param url
|
| | | * @param clazz
|
| | | * @param username
|
| | |
| | |
|
| | | /**
|
| | | * Retrieves a JSON message.
|
| | | * |
| | | *
|
| | | * @param url
|
| | | * @return the JSON message as a string
|
| | | * @throws {@link IOException}
|
| | |
| | |
|
| | | /**
|
| | | * Sends a JSON message.
|
| | | * |
| | | *
|
| | | * @param url
|
| | | * the url to write to
|
| | | * @param json
|
| | |
| | |
|
| | | /**
|
| | | * Sends a JSON message.
|
| | | * |
| | | *
|
| | | * @param url
|
| | | * the url to write to
|
| | | * @param json
|
| | |
| | | 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"));
|
| | | }
|
| | |
| | | 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) {
|
| | |
| | | }
|
| | | }
|
| | | }
|
| | | |
| | |
|
| | | private static class AccessPermissionTypeAdapter implements JsonSerializer<AccessPermission>, JsonDeserializer<AccessPermission> {
|
| | |
|
| | | private AccessPermissionTypeAdapter() {
|
| | |
| | | @Override
|
| | | public synchronized AccessPermission deserialize(JsonElement jsonElement, Type type,
|
| | | JsonDeserializationContext jsonDeserializationContext) {
|
| | | return AccessPermission.fromCode(jsonElement.getAsString()); |
| | | return AccessPermission.fromCode(jsonElement.getAsString());
|
| | | }
|
| | | }
|
| | |
|
| | |
| | | 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));
|
| | | }
|