use of com.google.gson.JsonSerializer in project incubator-gobblin by apache.
the class AvroToJdbcEntryConverterTest method testFlattening.
@Test
public void testFlattening() throws IOException, SchemaConversionException, SQLException, URISyntaxException, DataConversionException {
final String db = "db";
final String table = "users";
Map<String, JdbcType> dateColums = new HashMap<>();
dateColums.put("date_of_birth", JdbcType.DATE);
dateColums.put("last_modified", JdbcType.TIME);
dateColums.put("created", JdbcType.TIMESTAMP);
JdbcWriterCommands mockWriterCommands = mock(JdbcWriterCommands.class);
when(mockWriterCommands.retrieveDateColumns(db, table)).thenReturn(dateColums);
JdbcWriterCommandsFactory factory = mock(JdbcWriterCommandsFactory.class);
when(factory.newInstance(any(State.class), any(Connection.class))).thenReturn(mockWriterCommands);
List<JdbcEntryMetaDatum> jdbcEntryMetaData = new ArrayList<>();
jdbcEntryMetaData.add(new JdbcEntryMetaDatum("name", JdbcType.VARCHAR));
jdbcEntryMetaData.add(new JdbcEntryMetaDatum("favorite_number", JdbcType.VARCHAR));
jdbcEntryMetaData.add(new JdbcEntryMetaDatum("favorite_color", JdbcType.VARCHAR));
jdbcEntryMetaData.add(new JdbcEntryMetaDatum("date_of_birth", JdbcType.DATE));
jdbcEntryMetaData.add(new JdbcEntryMetaDatum("last_modified", JdbcType.TIME));
jdbcEntryMetaData.add(new JdbcEntryMetaDatum("created", JdbcType.TIMESTAMP));
jdbcEntryMetaData.add(new JdbcEntryMetaDatum("nested1_nested1_string", JdbcType.VARCHAR));
jdbcEntryMetaData.add(new JdbcEntryMetaDatum("nested1_nested1_int", JdbcType.INTEGER));
jdbcEntryMetaData.add(new JdbcEntryMetaDatum("nested1_nested2_union_nested2_string", JdbcType.VARCHAR));
jdbcEntryMetaData.add(new JdbcEntryMetaDatum("nested1_nested2_union_nested2_int", JdbcType.INTEGER));
JdbcEntrySchema expected = new JdbcEntrySchema(jdbcEntryMetaData);
Schema inputSchema = new Schema.Parser().parse(getClass().getResourceAsStream("/converter/pickfields_nested_with_union.avsc"));
WorkUnitState workUnitState = new WorkUnitState();
workUnitState.appendToListProp(JdbcPublisher.JDBC_PUBLISHER_FINAL_TABLE_NAME, table);
AvroToJdbcEntryConverter converter = new AvroToJdbcEntryConverter(workUnitState);
Map<String, JdbcType> dateColumnMapping = Maps.newHashMap();
dateColumnMapping.put("date_of_birth", JdbcType.DATE);
dateColumnMapping.put("last_modified", JdbcType.TIME);
dateColumnMapping.put("created", JdbcType.TIMESTAMP);
workUnitState.appendToListProp(AvroToJdbcEntryConverter.CONVERTER_AVRO_JDBC_DATE_FIELDS, new Gson().toJson(dateColumnMapping));
JdbcEntrySchema actualSchema = converter.convertSchema(inputSchema, workUnitState);
Assert.assertEquals(expected, actualSchema);
try (DataFileReader<GenericRecord> srcDataFileReader = new DataFileReader<GenericRecord>(new File(getClass().getResource("/converter/pickfields_nested_with_union.avro").toURI()), new GenericDatumReader<GenericRecord>(inputSchema))) {
List<JdbcEntryData> entries = new ArrayList<>();
while (srcDataFileReader.hasNext()) {
JdbcEntryData actualData = converter.convertRecord(actualSchema, srcDataFileReader.next(), workUnitState).iterator().next();
entries.add(actualData);
}
final JsonSerializer<JdbcEntryDatum> datumSer = new JsonSerializer<JdbcEntryDatum>() {
@Override
public JsonElement serialize(JdbcEntryDatum datum, Type typeOfSrc, JsonSerializationContext context) {
JsonObject jso = new JsonObject();
if (datum.getVal() == null) {
jso.add(datum.getColumnName(), null);
return jso;
}
if (datum.getVal() instanceof Date) {
jso.addProperty(datum.getColumnName(), ((Date) datum.getVal()).getTime());
} else if (datum.getVal() instanceof Timestamp) {
jso.addProperty(datum.getColumnName(), ((Timestamp) datum.getVal()).getTime());
} else if (datum.getVal() instanceof Time) {
jso.addProperty(datum.getColumnName(), ((Time) datum.getVal()).getTime());
} else {
jso.addProperty(datum.getColumnName(), datum.getVal().toString());
}
return jso;
}
};
JsonSerializer<JdbcEntryData> serializer = new JsonSerializer<JdbcEntryData>() {
@Override
public JsonElement serialize(JdbcEntryData src, Type typeOfSrc, JsonSerializationContext context) {
JsonArray arr = new JsonArray();
for (JdbcEntryDatum datum : src) {
arr.add(datumSer.serialize(datum, datum.getClass(), context));
}
return arr;
}
};
Gson gson = new GsonBuilder().registerTypeAdapter(JdbcEntryData.class, serializer).serializeNulls().create();
JsonElement actualSerialized = gson.toJsonTree(entries);
JsonElement expectedSerialized = new JsonParser().parse(new InputStreamReader(getClass().getResourceAsStream("/converter/pickfields_nested_with_union.json")));
Assert.assertEquals(actualSerialized, expectedSerialized);
}
converter.close();
}
use of com.google.gson.JsonSerializer in project BuildCraft by BuildCraft.
the class JsonUtil method registerNbtSerializersDeserializers.
public static GsonBuilder registerNbtSerializersDeserializers(GsonBuilder gsonBuilder) {
return gsonBuilder.registerTypeAdapterFactory(new TypeAdapterFactory() {
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
return type.getRawType() == NBTBase.class ? new TypeAdapter<T>() {
@Override
public void write(JsonWriter out, T value) throws IOException {
// noinspection unchecked, RedundantCast
Streams.write(((JsonSerializer<T>) (JsonSerializer<NBTBase>) (src, typeOfSrc, context) -> {
if (src == NBTUtilBC.NBT_NULL) {
return JsonNull.INSTANCE;
}
switch(src.getId()) {
case Constants.NBT.TAG_BYTE:
return context.serialize(src, NBTTagByte.class);
case Constants.NBT.TAG_SHORT:
return context.serialize(src, NBTTagShort.class);
case Constants.NBT.TAG_INT:
return context.serialize(src, NBTTagInt.class);
case Constants.NBT.TAG_LONG:
return context.serialize(src, NBTTagLong.class);
case Constants.NBT.TAG_FLOAT:
return context.serialize(src, NBTTagFloat.class);
case Constants.NBT.TAG_DOUBLE:
return context.serialize(src, NBTTagDouble.class);
case Constants.NBT.TAG_BYTE_ARRAY:
return context.serialize(src, NBTTagByteArray.class);
case Constants.NBT.TAG_STRING:
return context.serialize(src, NBTTagString.class);
case Constants.NBT.TAG_LIST:
return context.serialize(src, NBTTagList.class);
case Constants.NBT.TAG_COMPOUND:
return context.serialize(src, NBTTagCompound.class);
case Constants.NBT.TAG_INT_ARRAY:
return context.serialize(src, NBTTagIntArray.class);
default:
throw new IllegalArgumentException(src.toString());
}
}).serialize(value, type.getType(), new JsonSerializationContext() {
@Override
public JsonElement serialize(Object src) {
return gson.toJsonTree(src);
}
@Override
public JsonElement serialize(Object src, Type typeOfSrc) {
return gson.toJsonTree(src, typeOfSrc);
}
}), out);
}
@Override
public T read(JsonReader in) throws IOException {
return ((JsonDeserializer<T>) (json, typeOfT, context) -> {
if (json.isJsonNull()) {
// noinspection unchecked
return (T) NBTUtilBC.NBT_NULL;
}
if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isNumber()) {
Number number = json.getAsJsonPrimitive().getAsNumber();
if (number instanceof BigInteger || number instanceof Long || number instanceof Integer || number instanceof Short || number instanceof Byte) {
return context.deserialize(json, NBTTagLong.class);
} else {
return context.deserialize(json, NBTTagDouble.class);
}
}
if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isBoolean()) {
return context.deserialize(new JsonPrimitive(json.getAsJsonPrimitive().getAsBoolean() ? (byte) 1 : (byte) 0), NBTTagByte.class);
}
if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isString()) {
return context.deserialize(json, NBTTagString.class);
}
if (json.isJsonArray()) {
return context.deserialize(json, NBTTagList.class);
}
if (json.isJsonObject()) {
return context.deserialize(json, NBTTagCompound.class);
}
throw new IllegalArgumentException(json.toString());
}).deserialize(Streams.parse(in), type.getType(), gson::fromJson);
}
} : null;
}
}).registerTypeAdapter(NBTTagByte.class, (JsonSerializer<NBTTagByte>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getByte())).registerTypeAdapter(NBTTagByte.class, (JsonDeserializer<NBTTagByte>) (json, typeOfT, context) -> new NBTTagByte(json.getAsJsonPrimitive().getAsByte())).registerTypeAdapter(NBTTagShort.class, (JsonSerializer<NBTTagShort>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getShort())).registerTypeAdapter(NBTTagShort.class, (JsonDeserializer<NBTTagShort>) (json, typeOfT, context) -> new NBTTagShort(json.getAsJsonPrimitive().getAsShort())).registerTypeAdapter(NBTTagInt.class, (JsonSerializer<NBTTagInt>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getInt())).registerTypeAdapter(NBTTagInt.class, (JsonDeserializer<NBTTagInt>) (json, typeOfT, context) -> new NBTTagInt(json.getAsJsonPrimitive().getAsInt())).registerTypeAdapter(NBTTagLong.class, (JsonSerializer<NBTTagLong>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getLong())).registerTypeAdapter(NBTTagLong.class, (JsonDeserializer<NBTTagLong>) (json, typeOfT, context) -> new NBTTagLong(json.getAsJsonPrimitive().getAsLong())).registerTypeAdapter(NBTTagFloat.class, (JsonSerializer<NBTTagFloat>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getFloat())).registerTypeAdapter(NBTTagFloat.class, (JsonDeserializer<NBTTagFloat>) (json, typeOfT, context) -> new NBTTagFloat(json.getAsJsonPrimitive().getAsFloat())).registerTypeAdapter(NBTTagDouble.class, (JsonSerializer<NBTTagDouble>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getDouble())).registerTypeAdapter(NBTTagDouble.class, (JsonDeserializer<NBTTagDouble>) (json, typeOfT, context) -> new NBTTagDouble(json.getAsJsonPrimitive().getAsDouble())).registerTypeAdapter(NBTTagByteArray.class, (JsonSerializer<NBTTagByteArray>) (src, typeOfSrc, context) -> {
JsonArray jsonArray = new JsonArray();
for (byte element : src.getByteArray()) {
jsonArray.add(new JsonPrimitive(element));
}
return jsonArray;
}).registerTypeAdapter(NBTTagByteArray.class, (JsonDeserializer<NBTTagByteArray>) (json, typeOfT, context) -> new NBTTagByteArray(ArrayUtils.toPrimitive(StreamSupport.stream(json.getAsJsonArray().spliterator(), false).map(JsonElement::getAsByte).toArray(Byte[]::new)))).registerTypeAdapter(NBTTagString.class, (JsonSerializer<NBTTagString>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getString())).registerTypeAdapter(NBTTagString.class, (JsonDeserializer<NBTTagString>) (json, typeOfT, context) -> new NBTTagString(json.getAsJsonPrimitive().getAsString())).registerTypeAdapter(NBTTagList.class, (JsonSerializer<NBTTagList>) (src, typeOfSrc, context) -> {
JsonArray jsonArray = new JsonArray();
for (int i = 0; i < src.tagCount(); i++) {
NBTBase element = src.get(i);
jsonArray.add(context.serialize(element, NBTBase.class));
}
return jsonArray;
}).registerTypeAdapter(NBTTagList.class, (JsonDeserializer<NBTTagList>) (json, typeOfT, context) -> {
NBTTagList nbtTagList = new NBTTagList();
StreamSupport.stream(json.getAsJsonArray().spliterator(), false).map(element -> context.<NBTBase>deserialize(element, NBTBase.class)).forEach(nbtTagList::appendTag);
return nbtTagList;
}).registerTypeAdapter(NBTTagCompound.class, (JsonSerializer<NBTTagCompound>) (src, typeOfSrc, context) -> {
JsonObject jsonObject = new JsonObject();
for (String key : src.getKeySet()) {
jsonObject.add(key, context.serialize(src.getTag(key), NBTBase.class));
}
return jsonObject;
}).registerTypeAdapter(NBTTagCompound.class, (JsonDeserializer<NBTTagCompound>) (json, typeOfT, context) -> {
NBTTagCompound nbtTagCompound = new NBTTagCompound();
for (Map.Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) {
nbtTagCompound.setTag(entry.getKey(), context.deserialize(entry.getValue(), NBTBase.class));
}
return nbtTagCompound;
}).registerTypeAdapter(NBTTagIntArray.class, (JsonSerializer<NBTTagIntArray>) (src, typeOfSrc, context) -> {
JsonArray jsonArray = new JsonArray();
for (int element : src.getIntArray()) {
jsonArray.add(new JsonPrimitive(element));
}
return jsonArray;
}).registerTypeAdapter(NBTTagIntArray.class, (JsonDeserializer<NBTTagIntArray>) (json, typeOfT, context) -> new NBTTagIntArray(StreamSupport.stream(json.getAsJsonArray().spliterator(), false).mapToInt(JsonElement::getAsByte).toArray()));
}
use of com.google.gson.JsonSerializer 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.JsonSerializer in project 91Pop by DanteAndroid.
the class ApiException method handleException.
public static ApiException handleException(Throwable e) {
// 使用RxCache之后返回的是包裹的CompositeException,一般包含2个异常,rxcache异常和原本的异常
Logger.t(TAG).d("开始解析错误------");
if (e instanceof CompositeException) {
CompositeException compositeException = (CompositeException) e;
for (Throwable throwable : compositeException.getExceptions()) {
if (!(throwable instanceof RxCacheException)) {
e = throwable;
Logger.t(TAG).d("其他异常:" + throwable.getMessage());
} else {
Logger.t(TAG).d("RxCache 异常");
}
}
}
ApiException ex;
if (e instanceof HttpException) {
HttpException httpException = (HttpException) e;
ex = new ApiException(httpException, httpException.code());
ex.message = httpException.getMessage();
return ex;
} else if (e instanceof JsonParseException || e instanceof JSONException || e instanceof JsonSerializer || e instanceof NotSerializableException || e instanceof ParseException) {
ex = new ApiException(e, Error.PARSE_ERROR);
ex.message = "数据解析错误";
return ex;
} else if (e instanceof ClassCastException) {
ex = new ApiException(e, Error.CAST_ERROR);
ex.message = "类型转换错误";
return ex;
} else if (e instanceof ConnectException) {
ex = new ApiException(e, Error.NETWORD_ERROR);
ex.message = "连接失败";
return ex;
} else if (e instanceof javax.net.ssl.SSLHandshakeException) {
ex = new ApiException(e, Error.SSL_ERROR);
ex.message = "证书验证失败";
return ex;
} else if (e instanceof ConnectTimeoutException) {
ex = new ApiException(e, Error.TIMEOUT_ERROR);
ex.message = "网络连接超时";
return ex;
} else if (e instanceof java.net.SocketTimeoutException) {
ex = new ApiException(e, Error.TIMEOUT_ERROR);
ex.message = "网络连接超时";
return ex;
} else if (e instanceof UnknownHostException) {
ex = new ApiException(e, Error.UNKNOWNHOST_ERROR);
ex.message = "无法解析该域名";
return ex;
} else if (e instanceof NullPointerException) {
ex = new ApiException(e, Error.NULLPOINTER_EXCEPTION);
ex.message = "NullPointerException";
return ex;
} else if (e instanceof VideoException) {
ex = new ApiException(e, Error.PARSE_VIDEO_URL_ERROR);
ex.message = e.getMessage();
return ex;
} else if (e instanceof FavoriteException) {
ex = new ApiException(e, Error.FAVORITE_VIDEO_ERROR);
ex.message = e.getMessage();
return ex;
} else if (e instanceof DaoException) {
ex = new ApiException(e, Error.GREEN_DAO_ERROR);
ex.message = "数据库错误";
return ex;
} else if (e instanceof MessageException) {
ex = new ApiException(e, Error.COMMON_MESSAGE_ERROR);
ex.message = e.getMessage();
return ex;
} else {
ex = new ApiException(e, Error.UNKNOWN);
ex.message = "未知错误:" + e.getMessage();
return ex;
}
}
Aggregations