use of com.google.gson.JsonSerializationContext in project AuthMeReloaded by AuthMe.
the class LimboPlayerSerializer method serialize.
@Override
public JsonElement serialize(LimboPlayer limboPlayer, Type type, JsonSerializationContext context) {
Location loc = limboPlayer.getLocation();
JsonObject locationObject = new JsonObject();
locationObject.addProperty(LOC_WORLD, loc.getWorld().getName());
locationObject.addProperty(LOC_X, loc.getX());
locationObject.addProperty(LOC_Y, loc.getY());
locationObject.addProperty(LOC_Z, loc.getZ());
locationObject.addProperty(LOC_YAW, loc.getYaw());
locationObject.addProperty(LOC_PITCH, loc.getPitch());
JsonObject obj = new JsonObject();
obj.add(LOCATION, locationObject);
List<JsonObject> groups = limboPlayer.getGroups().stream().map(g -> {
JsonObject jsonGroup = new JsonObject();
jsonGroup.addProperty("groupName", g.getGroupName());
if (g.getContextMap() != null) {
jsonGroup.addProperty("contextMap", GSON.toJson(g.getContextMap()));
}
return jsonGroup;
}).collect(Collectors.toList());
JsonArray jsonGroups = new JsonArray();
groups.forEach(jsonGroups::add);
obj.add(GROUPS, jsonGroups);
obj.addProperty(IS_OP, limboPlayer.isOperator());
obj.addProperty(CAN_FLY, limboPlayer.isCanFly());
obj.addProperty(WALK_SPEED, limboPlayer.getWalkSpeed());
obj.addProperty(FLY_SPEED, limboPlayer.getFlySpeed());
return obj;
}
use of com.google.gson.JsonSerializationContext in project msgraph-sdk-java by microsoftgraph.
the class GsonFactory method getGsonInstance.
/**
* Creates an instance of GSON
*
* @param logger the logger
* @return the new instance
*/
public static Gson getGsonInstance(final ILogger logger) {
final JsonSerializer<Calendar> calendarJsonSerializer = new JsonSerializer<Calendar>() {
@Override
public JsonElement serialize(final Calendar src, final Type typeOfSrc, final JsonSerializationContext context) {
if (src == null) {
return null;
}
try {
return new JsonPrimitive(CalendarSerializer.serialize(src));
} catch (final Exception e) {
logger.logError(PARSING_MESSAGE + src, e);
return null;
}
}
};
final JsonDeserializer<Calendar> calendarJsonDeserializer = new JsonDeserializer<Calendar>() {
@Override
public Calendar deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
if (json == null) {
return null;
}
try {
return CalendarSerializer.deserialize(json.getAsString());
} catch (final ParseException e) {
logger.logError(PARSING_MESSAGE + json.getAsString(), e);
return null;
}
}
};
final JsonSerializer<byte[]> byteArrayJsonSerializer = new JsonSerializer<byte[]>() {
@Override
public JsonElement serialize(final byte[] src, final Type typeOfSrc, final JsonSerializationContext context) {
if (src == null) {
return null;
}
try {
return new JsonPrimitive(ByteArraySerializer.serialize(src));
} catch (final Exception e) {
logger.logError(PARSING_MESSAGE + src, e);
return null;
}
}
};
final JsonDeserializer<byte[]> byteArrayJsonDeserializer = new JsonDeserializer<byte[]>() {
@Override
public byte[] deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
if (json == null) {
return null;
}
try {
return ByteArraySerializer.deserialize(json.getAsString());
} catch (final ParseException e) {
logger.logError(PARSING_MESSAGE + json.getAsString(), e);
return null;
}
}
};
final JsonSerializer<DateOnly> dateJsonSerializer = new JsonSerializer<DateOnly>() {
@Override
public JsonElement serialize(final DateOnly src, final Type typeOfSrc, final JsonSerializationContext context) {
if (src == null) {
return null;
}
return new JsonPrimitive(src.toString());
}
};
final JsonDeserializer<DateOnly> dateJsonDeserializer = new JsonDeserializer<DateOnly>() {
@Override
public DateOnly deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
if (json == null) {
return null;
}
try {
return DateOnly.parse(json.getAsString());
} catch (final ParseException e) {
logger.logError(PARSING_MESSAGE + json.getAsString(), e);
return null;
}
}
};
final JsonSerializer<EnumSet<?>> enumSetJsonSerializer = new JsonSerializer<EnumSet<?>>() {
@Override
public JsonElement serialize(final EnumSet<?> src, final Type typeOfSrc, final JsonSerializationContext context) {
if (src == null || src.isEmpty()) {
return null;
}
return EnumSetSerializer.serialize(src);
}
};
final JsonDeserializer<EnumSet<?>> enumSetJsonDeserializer = new JsonDeserializer<EnumSet<?>>() {
@Override
public EnumSet<?> deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
if (json == null) {
return null;
}
return EnumSetSerializer.deserialize(typeOfT, json.getAsString());
}
};
final JsonSerializer<Duration> durationJsonSerializer = new JsonSerializer<Duration>() {
@Override
public JsonElement serialize(final Duration src, final Type typeOfSrc, final JsonSerializationContext context) {
return new JsonPrimitive(src.toString());
}
};
final JsonDeserializer<Duration> durationJsonDeserializer = new JsonDeserializer<Duration>() {
@Override
public Duration deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
try {
return DatatypeFactory.newInstance().newDuration(json.toString());
} catch (Exception e) {
return null;
}
}
};
return new GsonBuilder().excludeFieldsWithoutExposeAnnotation().registerTypeAdapter(Calendar.class, calendarJsonSerializer).registerTypeAdapter(Calendar.class, calendarJsonDeserializer).registerTypeAdapter(GregorianCalendar.class, calendarJsonSerializer).registerTypeAdapter(GregorianCalendar.class, calendarJsonDeserializer).registerTypeAdapter(byte[].class, byteArrayJsonDeserializer).registerTypeAdapter(byte[].class, byteArrayJsonSerializer).registerTypeAdapter(DateOnly.class, dateJsonSerializer).registerTypeAdapter(DateOnly.class, dateJsonDeserializer).registerTypeAdapter(EnumSet.class, enumSetJsonSerializer).registerTypeAdapter(EnumSet.class, enumSetJsonDeserializer).registerTypeAdapter(Duration.class, durationJsonSerializer).registerTypeAdapter(Duration.class, durationJsonDeserializer).registerTypeAdapterFactory(new FallBackEnumTypeAdapter()).create();
}
use of com.google.gson.JsonSerializationContext in project AngularBeans by bessemHmidi.
the class ByteArrayJsonAdapter method initJsonSerialiser.
public void initJsonSerialiser() {
GsonBuilder builder = new GsonBuilder();
builder.serializeNulls();
builder.setExclusionStrategies(NGConfiguration.getGsonExclusionStrategy());
builder.registerTypeAdapter(LobWrapper.class, new LobWrapperJsonAdapter(cache));
builder.registerTypeAdapter(LobWrapper.class, new JsonDeserializer<LobWrapper>() {
@Override
public LobWrapper deserialize(JsonElement element, Type typeOfT, JsonDeserializationContext context) {
return null;
}
});
// --- BYTE[] BLOCK BEGIN ---
builder.registerTypeAdapter(NGLob.class, new ByteArrayJsonAdapter(cache, contextPath));
builder.registerTypeAdapter(NGBase64.class, new JsonSerializer<NGBase64>() {
@Override
public JsonElement serialize(NGBase64 src, Type typeOfSrc, JsonSerializationContext context) {
if (src != null) {
return CommonUtils.getBase64Json(src.getType(), src.getBytes());
}
return null;
}
});
builder.registerTypeAdapter(NGBase64.class, new JsonDeserializer<NGBase64>() {
@Override
public NGBase64 deserialize(JsonElement element, Type typeOfT, JsonDeserializationContext context) {
byte[] bytes = CommonUtils.getBytesFromJson(element);
if (bytes != null && bytes.length > 0) {
return new NGBase64(bytes);
}
return null;
}
});
if (CommonUtils.getBytesArrayBind().equals(Constants.BASE64_BIND)) {
builder.registerTypeAdapter(byte[].class, new JsonSerializer<byte[]>() {
@Override
public JsonElement serialize(byte[] src, Type typeOfSrc, JsonSerializationContext context) {
return CommonUtils.getBase64Json(null, src);
}
});
builder.registerTypeAdapter(byte[].class, new JsonDeserializer<byte[]>() {
@Override
public byte[] deserialize(JsonElement element, Type typeOfT, JsonDeserializationContext context) {
return CommonUtils.getBytesFromJson(element);
}
});
} else {
builder.registerTypeAdapter(byte[].class, new ByteArrayJsonAdapter(cache, contextPath));
}
// --- DATE FORMAT BLOCK BEGIN ---
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 element, Type typeOfT, JsonDeserializationContext context) {
try {
String dateFormated = element.getAsString();
if (dateFormated != null && dateFormated.trim().length() > 0) {
Calendar cal = Calendar.getInstance();
cal.setTime(dateFormat.parse(dateFormated));
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 element, Type typeOfT, JsonDeserializationContext context) {
try {
String dateFormated = element.getAsString();
if (dateFormated != null && dateFormated.trim().length() > 0) {
Calendar cal = Calendar.getInstance();
cal.setTime(dateFormat.parse(dateFormated));
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 element, Type typeOfT, JsonDeserializationContext context) {
try {
String dateFormated = element.getAsString();
if (dateFormated != null && dateFormated.trim().length() > 0) {
Calendar cal = Calendar.getInstance();
cal.setTime(dateFormat.parse(dateFormated));
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 element, Type typeOfT, JsonDeserializationContext context) {
try {
String dateFormated = element.getAsString();
if (dateFormated != null && dateFormated.trim().length() > 0) {
return dateFormat.parse(dateFormated);
}
} catch (Exception e) {
}
return null;
}
});
}
// --- DATE FORMAT BLOCK END ---
mainSerializer = builder.create();
}
use of com.google.gson.JsonSerializationContext in project Tangram-Android by alibaba.
the class SampleDataParser method addCardStyle.
private void addCardStyle(JSONObject cardData, Card card) {
try {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Float.class, new JsonSerializer<Float>() {
@Override
public JsonElement serialize(final Float src, final Type typeOfSrc, final JsonSerializationContext context) {
try {
if (src.isInfinite() || src.isNaN()) {
return new JsonPrimitive(0f);
}
BigDecimal value = BigDecimal.valueOf(src);
return new JsonPrimitive(value);
} catch (Exception e) {
e.printStackTrace();
}
return new JsonPrimitive(0f);
}
});
Gson gson = gsonBuilder.create();
GridCard.GridStyle gridStyle = new GridCard.GridStyle();
if (card instanceof BannerCard) {
gridStyle.aspectRatio = 3.223f;
}
cardData.put(Card.KEY_STYLE, new JSONObject(gson.toJson(gridStyle)));
} catch (JSONException e) {
e.printStackTrace();
}
}
Aggregations