Example usage for com.google.gson JsonDeserializer JsonDeserializer

List of usage examples for com.google.gson JsonDeserializer JsonDeserializer

Introduction

In this page you can find the example usage for com.google.gson JsonDeserializer JsonDeserializer.

Prototype

JsonDeserializer

Source Link

Usage

From source file:org.tinymediamanager.scraper.omdb.service.Controller.java

License:Apache License

private GsonBuilder getGsonBuilder() {
    GsonBuilder builder = new GsonBuilder();
    // class types
    builder.registerTypeAdapter(Integer.class, new JsonDeserializer<Integer>() {
        @Override//ww  w  .ja v  a  2s.c o  m
        public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            try {
                return json.getAsInt();
            } catch (NumberFormatException e) {
                return 0;
            }
        }
    });
    builder.registerTypeAdapter(Date.class, new DateTypeAdapter());
    return builder;
}

From source file:org.unitime.timetable.onlinesectioning.custom.purdue.DegreeWorksPlanScraper.java

License:Apache License

protected Gson getGson() {
    return new GsonBuilder().registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() {
        @Override//w  w  w . ja va2s . c  o  m
        public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src.toString("yyyy-MM-dd'T'HH:mm:ss'Z'"));
        }
    }).registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
        @Override
        public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return new DateTime(json.getAsJsonPrimitive().getAsString(), DateTimeZone.UTC);
        }
    }).registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
        @Override
        public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(src));
        }
    }).registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            try {
                return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                        .parse(json.getAsJsonPrimitive().getAsString());
            } catch (ParseException e) {
                throw new JsonParseException(e.getMessage(), e);
            }
        }
    }).setPrettyPrinting().create();
}

From source file:org.unitime.timetable.onlinesectioning.custom.purdue.GsonRepresentation.java

License:Apache License

public GsonBuilder getBuilder() {
    if (iBuilder == null) {
        iBuilder = new GsonBuilder().registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() {
            @Override/*from w  w  w .j  a  v a  2 s . co  m*/
            public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) {
                return new JsonPrimitive(src.toString("yyyy-MM-dd'T'HH:mm:ss'Z'"));
            }
        }).registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
            @Override
            public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                    throws JsonParseException {
                return new DateTime(json.getAsJsonPrimitive().getAsString(), DateTimeZone.UTC);
            }
        }).registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
            @Override
            public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
                return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(src));
            }
        }).registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
            @Override
            public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                    throws JsonParseException {
                try {
                    return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                            .parse(json.getAsJsonPrimitive().getAsString());
                } catch (ParseException e) {
                    throw new JsonParseException(e.getMessage(), e);
                }
            }
        });
    }
    return iBuilder;
}

From source file:org.unitime.timetable.onlinesectioning.custom.purdue.PurdueBatchSolverValidator.java

License:Apache License

protected Gson getGson() {
    GsonBuilder builder = new GsonBuilder().registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() {
        @Override/*from  w  w w .  ja v a 2s  .c  o  m*/
        public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src.toString("yyyy-MM-dd'T'HH:mm:ss'Z'"));
        }
    }).registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
        @Override
        public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return new DateTime(json.getAsJsonPrimitive().getAsString(), DateTimeZone.UTC);
        }
    });
    return builder.create();
}

From source file:org.unitime.timetable.onlinesectioning.custom.purdue.PurdueCourseRequestsValidationProvider.java

License:Apache License

protected Gson getGson(OnlineSectioningHelper helper) {
    GsonBuilder builder = new GsonBuilder().registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() {
        @Override// w  ww  .java2 s.  co m
        public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src.toString("yyyy-MM-dd'T'HH:mm:ss'Z'"));
        }
    }).registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
        @Override
        public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return new DateTime(json.getAsJsonPrimitive().getAsString(), DateTimeZone.UTC);
        }
    }).registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
        @Override
        public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(src));
        }
    }).registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            try {
                return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                        .parse(json.getAsJsonPrimitive().getAsString());
            } catch (ParseException e) {
                throw new JsonParseException(e.getMessage(), e);
            }
        }
    });
    if (helper.isDebugEnabled())
        builder.setPrettyPrinting();
    return builder.create();
}

From source file:org.unitime.timetable.onlinesectioning.custom.purdue.XEStudentEnrollment.java

License:Open Source License

protected Gson getGson(OnlineSectioningHelper helper) {
    GsonBuilder builder = new GsonBuilder().registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() {
        @Override//w w w  . ja  va2s  . com
        public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src.toString("yyyy-MM-dd'T'HH:mm:ss'Z'"));
        }
    }).registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
        @Override
        public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return new DateTime(json.getAsJsonPrimitive().getAsString(), DateTimeZone.UTC);
        }
    });
    if (helper.isDebugEnabled())
        builder.setPrettyPrinting();
    return builder.create();
}

From source file:org.unitime.timetable.server.sectioning.PublishedSectioningSolutionsBackend.java

License:Apache License

protected Gson getGson() {
    GsonBuilder builder = new GsonBuilder().registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() {
        @Override/*from   w  ww . j  a v  a 2s  .c  o  m*/
        public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src.toString("yyyy-MM-dd'T'HH:mm:ss'Z'"));
        }
    }).registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
        @Override
        public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return new DateTime(json.getAsJsonPrimitive().getAsString(), DateTimeZone.UTC);
        }
    }).registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
        @Override
        public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(src));
        }
    }).registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            try {
                return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                        .parse(json.getAsJsonPrimitive().getAsString());
            } catch (ParseException e) {
                throw new JsonParseException(e.getMessage(), e);
            }
        }
    });
    builder.setPrettyPrinting();
    return builder.create();
}

From source file:org.ut.biolab.medsavant.MedSavantServlet.java

License:Open Source License

@Override
public void init() throws ServletException {
    LOG.info("MedSavant JSON Client/Server booted.");
    try {//from w w w .java  2s  . c o m
        loadConfiguration();
        initializeRegistry(this.medSavantServerHost, Integer.toString(this.medSavantServerPort));
        session = new Session(sessionManager, username, password, db);
        GsonBuilder gsonBuilder = new GsonBuilder();

        // Handle the condition type.
        gsonBuilder.registerTypeAdapter(Condition.class, new JsonDeserializer<Condition>() {
            @Override
            public Condition deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
                    throws JsonParseException {
                return gson.fromJson(je, SimplifiedCondition.class).getCondition(session.getSessionId(),
                        variantManager);
            }
        });
        gson = gsonBuilder.create();

    } catch (Exception ex) {
        LOG.error(ex);
        throw new ServletException("Failed to initialize the medsavant JSON client: " + ex.getMessage());
    }
}

From source file:travel.izi.sdk.util.GsonHelper.java

License:Apache License

/**
 * Create a {@link com.google.gson.GsonBuilder} and register all of the custom types needed in order to properly
 * deserialize complex IZITravel-specific type.
 *
 * @return Assembled GSON builder instance.
 */// w  ww.  j a  va2s .  c  o m
public static GsonBuilder getGsonBuilder() {
    GsonBuilder builder = new GsonBuilder();

    builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");

    //enum types
    builder.registerTypeAdapter(Category.class, new JsonDeserializer<Category>() {
        @Override
        public Category deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return Category.fromValue(json.getAsString());
        }
    });
    builder.registerTypeAdapter(MediaType.class, new JsonDeserializer<MediaType>() {
        @Override
        public MediaType deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return MediaType.fromValue(json.getAsString());
        }
    });
    builder.registerTypeAdapter(MtgObjectType.class, new JsonDeserializer<MtgObjectType>() {
        @Override
        public MtgObjectType deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return MtgObjectType.fromValue(json.getAsString());
        }
    });
    builder.registerTypeAdapter(Placement.class, new JsonDeserializer<Placement>() {
        @Override
        public Placement deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return Placement.fromValue(json.getAsString());
        }
    });
    builder.registerTypeAdapter(PlaybackType.class, new JsonDeserializer<PlaybackType>() {
        @Override
        public PlaybackType deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return PlaybackType.fromValue(json.getAsString());
        }
    });
    builder.registerTypeAdapter(Status.class, new JsonDeserializer<Status>() {
        @Override
        public Status deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return Status.fromValue(json.getAsString());
        }
    });
    builder.registerTypeAdapter(TriggerZoneType.class, new JsonDeserializer<TriggerZoneType>() {
        @Override
        public TriggerZoneType deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return TriggerZoneType.fromValue(json.getAsString());
        }
    });

    return builder;
}

From source file:tweets.UserRepo.java

public static User[] getUsers() throws URISyntaxException {
    URI fromUri = new URI("http://localhost:7474/db/data/cypher");

    WebResource resource = Client.create().resource(fromUri);
    String cypher = "{\n"
            + "  \"query\" : \"MATCH (n:user) RETURN n.name, n.LastName,n.Email ,id(n), n.Pseudo \",\n"
            + "  \"params\" : {\n" + "  }\n" + "}";

    // POST JSON to the relationships URI
    ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON)
            .entity(cypher).post(ClientResponse.class);
    String fileContent = response.getEntity(String.class);
    // System.out.println(fileContent);
    response.close();/*w  ww.j  a  v a2s . co  m*/
    GsonBuilder builder = new GsonBuilder();

    builder.registerTypeAdapter(User[].class, new JsonDeserializer<User[]>() {

        @Override
        public User[] deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
                throws JsonParseException {

            List<User> s = new ArrayList<>();
            // System.out.println(je.getAsJsonObject().get("data").getAsJsonArray().get(1));
            // JsonArray coordinatesArray = fields.getAsJsonArray();

            for (int i = 0; i < je.getAsJsonObject().get("data").getAsJsonArray().size(); i++) {

                String[] parts = je.getAsJsonObject().get("data").getAsJsonArray().get(i).toString()
                        .replace("\"", "").replace("[", "").replace("]", "").split(",");
                try {
                    s.add(new User(parts[0], parts[1], parts[2], new URI(BASE_URI + "node/" + parts[3]),
                            parts[4]));

                } catch (URISyntaxException ex) {
                    Logger.getLogger(UserRepo.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            return s.toArray(new User[s.size()]);
        }
    });
    Gson gson = builder.create();

    User[] S = gson.fromJson(fileContent, User[].class);
    return S;
}