Google Gson [1] is very useful library for parsing JSON to our domain object. It supports mapping JSON to such formats as Long, String, Integer, Boolean etc. The problem is when you have Timestamp [2] in your domain object. You are forced to write Adapter [3] which will help you to make it happen.
package com.garciapl.parser; import com.google.gson.*; import com.garciapl.model.dto.ExampleDTO; import java.lang.reflect.Type; import java.sql.Timestamp; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class TimestampAdapter implements JsonDeserializer{ private final DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); @Override public ExampleDTO deserialize(JsonElement json, Type type, JsonDeserializationContext jsonDeserializationContext) { if (json.isJsonObject()) { JsonObject jsonObject = (JsonObject) json; String startDate = jsonObject.has("startDate") ? jsonObject.get("startDate").getAsString() : null; String endDate = jsonObject.has("endDate") ? jsonObject.get("endDate").getAsString() : null; Timestamp startDateStamp = null; Timestamp endDateStamp = null; if (startDate != null && endDate != null) { startDateStamp = createTimestamp(startDate); endDateStamp = createTimestamp(endDate); } return new ExampleDTO(startDateStamp, endDateStamp); } return new ExampleDTO(); } private Timestamp createTimestamp(String json) { try { Date date = format.parse(json); return new Timestamp(date.getTime()); } catch (ParseException e) { throw new JsonParseException(e); } } }
Below you can find how to use this above adapter in GsonBuilder :
package com.garciapl.parser; import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.garciapl.model.dto.ExampleDTO; import com.garciapl.util.Localization; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import java.lang.reflect.Modifier; import java.lang.reflect.Type; @Component public class JsonParser { private final static Logger logger = LoggerFactory.getLogger(JsonParser.class); private Gson gson; public JsonParser() { gson = new GsonBuilder() .serializeNulls() .disableHtmlEscaping() .setDateFormat(Localization.BASE_DATE_FORMAT) .registerTypeAdapter(ExampleDTO.class, new TimestampAdapter()) .excludeFieldsWithModifiers(Modifier.TRANSIENT) .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES) .setPrettyPrinting() .create(); } publicT fromJson(String json, Class destinationClass) { if (json == null || json.isEmpty()) { try { return destinationClass.newInstance(); } catch (InstantiationException e) { logger.error("JsonParser exception : ", e); return null; } catch (IllegalAccessException e) { logger.error("JsonParser exception : ", e); return null; } } else { return gson.fromJson(json, destinationClass); } } }
Reference: [1] https://github.com/google/gson [2] Timestamp Java Doc [3] Gson TypeAdapter
Gson Timestamp deserialization
2015-09-26T12:38:00+01:00
GarciaPL
Java|Spring|