use of com.google.gson.JsonDeserializationContext in project ninja by ninjaframework.
the class ApiControllerDocTest method getGsonWithLongToDateParsing.
private Gson getGsonWithLongToDateParsing() {
// Creates the json object which will manage the information received
GsonBuilder builder = new GsonBuilder();
// Register an adapter to manage the date types as long values
builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return new Date(json.getAsJsonPrimitive().getAsLong());
}
});
Gson gson = builder.create();
return gson;
}
use of com.google.gson.JsonDeserializationContext in project ninja by ninjaframework.
the class ApiControllerDocTesterTest method getGsonWithLongToDateParsing.
private Gson getGsonWithLongToDateParsing() {
// Creates the json object which will manage the information received
GsonBuilder builder = new GsonBuilder();
// Register an adapter to manage the date types as long values
builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return new Date(json.getAsJsonPrimitive().getAsLong());
}
});
Gson gson = builder.create();
return gson;
}
use of com.google.gson.JsonDeserializationContext in project ninja by ninjaframework.
the class ApiControllerTest method getGsonWithLongToDateParsing.
private Gson getGsonWithLongToDateParsing() {
// Creates the json object which will manage the information received
GsonBuilder builder = new GsonBuilder();
// Register an adapter to manage the date types as long values
builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return new Date(json.getAsJsonPrimitive().getAsLong());
}
});
Gson gson = builder.create();
return gson;
}
use of com.google.gson.JsonDeserializationContext in project intellij-plugins by StepicOrg.
the class ReplyDeserializer method deserialize.
@Override
public Reply deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (json == null || !(json instanceof JsonObject)) {
return null;
}
Reply reply = new Reply();
JsonObject object = json.getAsJsonObject();
String language = getString(object, "language");
reply.setLanguage(language);
String code = getString(object, "code");
reply.setCode(code);
String formula = getString(object, "formula");
reply.setFormula(formula);
String text = getString(object, "text");
reply.setText(text);
String number = getString(object, "number");
reply.setNumber(number);
JsonArray ordering = getJsonArray(object, "ordering");
if (ordering != null) {
List<Integer> intOrdering = new ArrayList<>();
ordering.forEach(item -> intOrdering.add(item.getAsInt()));
reply.setOrdering(intOrdering);
}
reply.setAttachments(getList(object, "attachments", (jsonElement) -> context.deserialize(jsonElement, Attachment.class)));
reply.setFiles(getStringList(object, "files"));
JsonArray choices = getJsonArray(object, "choices");
if (choices != null && choices.size() > 0) {
if (choices.get(0).isJsonPrimitive()) {
List<Boolean> list = new ArrayList<>();
choices.forEach(item -> list.add(item.getAsBoolean()));
reply.setChoices(list);
} else {
List<Choice> list = new ArrayList<>();
choices.forEach(item -> list.add(context.deserialize(item, Choice.class)));
reply.setChoices(list);
}
}
reply.setBlanks(getStringList(object, "blanks"));
return reply;
}
use of com.google.gson.JsonDeserializationContext in project AngularBeans by bessemHmidi.
the class ByteArrayJsonAdapter method initJsonSerialiser.
public void initJsonSerialiser() {
GsonBuilder builder = new GsonBuilder().serializeNulls();
builder.setExclusionStrategies(NGConfiguration.getGsonExclusionStrategy());
builder.registerTypeAdapter(LobWrapper.class, new LobWrapperJsonAdapter(cache));
builder.registerTypeAdapter(byte[].class, new ByteArrayJsonAdapter(cache, contextPath));
builder.registerTypeAdapter(LobWrapper.class, new JsonDeserializer<LobWrapper>() {
@Override
public LobWrapper deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
return null;
}
});
if (NGConfiguration.getProperty("DATE_PATTERN") != null) {
final SimpleDateFormat dateFormat = new SimpleDateFormat(NGConfiguration.getProperty("DATE_PATTERN"));
if (dateFormat != null && NGConfiguration.getProperty("TIME_ZONE") != null) {
dateFormat.setTimeZone(TimeZone.getTimeZone(NGConfiguration.getProperty("TIME_ZONE")));
}
builder.registerTypeAdapter(java.sql.Date.class, new JsonSerializer<java.sql.Date>() {
@Override
public JsonElement serialize(java.sql.Date src, Type typeOfSrc, JsonSerializationContext context) {
if (src != null) {
Calendar cal = Calendar.getInstance();
cal.setTime(src);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return new JsonPrimitive(dateFormat.format(cal.getTime()));
}
return null;
}
});
builder.registerTypeAdapter(java.sql.Date.class, new JsonDeserializer<java.sql.Date>() {
@Override
public java.sql.Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
try {
Calendar cal = Calendar.getInstance();
cal.setTime(dateFormat.parse(json.getAsString()));
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return new java.sql.Date(cal.getTime().getTime());
} catch (Exception e) {
}
return null;
}
});
builder.registerTypeAdapter(java.sql.Time.class, new JsonSerializer<java.sql.Time>() {
@Override
public JsonElement serialize(java.sql.Time src, Type typeOfSrc, JsonSerializationContext context) {
if (src != null) {
Calendar cal = Calendar.getInstance();
cal.setTime(src);
return new JsonPrimitive(dateFormat.format(cal.getTime()));
}
return null;
}
});
builder.registerTypeAdapter(java.sql.Time.class, new JsonDeserializer<java.sql.Time>() {
@Override
public java.sql.Time deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
try {
Calendar cal = Calendar.getInstance();
cal.setTime(dateFormat.parse(json.getAsString()));
return new java.sql.Time(cal.getTime().getTime());
} catch (Exception e) {
}
return null;
}
});
builder.registerTypeAdapter(java.sql.Timestamp.class, new JsonSerializer<java.sql.Timestamp>() {
@Override
public JsonElement serialize(java.sql.Timestamp src, Type typeOfSrc, JsonSerializationContext context) {
if (src != null) {
Calendar cal = Calendar.getInstance();
cal.setTime(src);
return new JsonPrimitive(dateFormat.format(cal.getTime()));
}
return null;
}
});
builder.registerTypeAdapter(java.sql.Timestamp.class, new JsonDeserializer<java.sql.Timestamp>() {
@Override
public java.sql.Timestamp deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
try {
Calendar cal = Calendar.getInstance();
cal.setTime(dateFormat.parse(json.getAsString()));
return new java.sql.Timestamp(cal.getTime().getTime());
} catch (Exception e) {
}
return null;
}
});
builder.registerTypeAdapter(java.util.Date.class, new JsonSerializer<java.util.Date>() {
@Override
public JsonElement serialize(java.util.Date src, Type typeOfSrc, JsonSerializationContext context) {
return src == null ? null : new JsonPrimitive(dateFormat.format(src));
}
});
builder.registerTypeAdapter(java.util.Date.class, new JsonDeserializer<java.util.Date>() {
@Override
public java.util.Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
try {
return dateFormat.parse(json.getAsString());
} catch (Exception e) {
}
return null;
}
});
}
mainSerializer = builder.create();
}
Aggregations