How to override Gson default DateTypeAdapter? - java

I created a custom Date deserializer:
private static class DateJsonDeserializer implements JsonDeserializer<Date>{
#Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
for (String format : DATE_FORMATS) {
try {
SimpleDateFormat df = new SimpleDateFormat(format);
return df.parse(json.getAsString());
} catch (ParseException e) {
}
}
throw new JsonParseException("Unparseable date: \"" + json.getAsString()
+ "\". Supported formats: " + Arrays.toString(DATE_FORMATS));
}
}
I added this to my gsonBuilder:
gsonBuilder.registerTypeAdapter(Date.class, new DateJsonDeserializer());
But if I would like to parse a date I see the built in DateTypeAdapter class used, why? I would like to use my date deserializer.
I use retrofit 2.1.0, Gson 2.8.0, converter-gson 2.1.0

are you sure that Date.class is the correct class? Maybe it uses a subclass of Date.class ?

Related

Using GsonBuilder.setdateformat("dd-MM-yyyy HH:mm:ss") can I convert a date json Object to key value pair?

Currently I am using Gson to convert an object to json
Custom Adapter
public class MyDataMsgSerializer implements JsonSerializer<MyDataMsg>, JsonDeserializer<MyDataMsg>{
private static Gson gson;
static {
try {
gson = new GsonBuilder().registerTypeHierarchyAdapter(BsCal.class, new CalendarSerializer()).setDateFormat("dd-MM-yyyy HH:mm:ss").serializeNulls().create();
} catch (Exception e) {
}
}
#Override
public JsonElement serialize(MyDataMsgsrc, Type typeOfSrc, JsonSerializationContext context) {
JsonElement jsonSubscription = gson.toJsonTree(src, typeOfSrc);
return jsonSubscription;
}
#Override
public MyDataMsg deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return gson.fromJson(json, MyDataMsg.class);
}
}
private String convertObjToJson(MyDataMsg myDataMsg) {
Gson gson = new GsonBuilder().registerTypeHierarchyAdapter(BsCal.class, new CalendarSerializer()).setDateFormat("dd-MM-yyyy HH:mm:ss")
.registerTypeHierarchyAdapter(MyDataMsg.class, new MyDataMsgSerializer()).serializeNulls()
.create();
String jsonToString = gson.toJson(myDataMsg);
LOGGER.info("Converted Json Object {}", jsonToString);
return jsonToString;
}
output:
"myDataMsg": {
"emplId": "15163",
"mnthCd": "202211",
"empltype": "M",
"workingCd": "wfh",
"btchnm": "0499",
"shift": "1",
"shiftStTm": {
"dateTime": "2022-11-30 00:00:00”,
"timeInMs": 1669766400000
},
"actncd": "UPDATE",
"IsLead": null,
"nightSf": "2",
"overtm": null,
"totalOverTm": null,
"ExtraSal": "630"
}
Currently the 'shiftStTm' is being generated as below
"shiftStTm": {
"dateTime": "2022-11-30 00:00:00",
"timeInMs": 1669766400000
},
I want it to be generated as this (key value)
"shiftStTm":"2022-11-30 00:00:00"
could any one help me here to format it to key value using GsonBuilder??
I am using GsonBuilder to convert MyDataMsg to json In which the date is formatting as object instead of key vale, could you please help me to fix the issue?

Can't convert from string to Joda LocalTime with DefaultFormattingConversionService

Am unable to convert string to Joda LocalTime with DefaultFormattingConversionService.
If I pass time as "12:00" it says time is too short, but if I pass it as "12:00:00", it says it is malformed.
import org.joda.time.LocalTime;
import org.springframework.format.support.DefaultFormattingConversionService;
public class SpringLocalTimeFormatterTry {
public static void main(String[] args) {
DefaultFormattingConversionService service = new DefaultFormattingConversionService();
try {
System.out.println(service.convert("12:00", LocalTime.class));
}
catch (Exception e) {
System.out.println(e.getMessage());
}
try {
System.out.println(service.convert("12:00:00", LocalTime.class));
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
How to use it correctly or fix?
The vanilla settings of DefaultFormattingConversionService use platform default locale, which, I assume from the error, are the same as mine, ie. English. That means, that for time you need to add the AM/PM indicator. This works for me:
System.out.println(service.convert("10:12 am", LocalTime.class));
>> 10:12:00.000
To handle your desired time format, you can add an extra converter:
service.addConverter(new Converter<String, LocalTime>() {
#Override
public LocalTime convert(String source) {
return LocalTime.parse(source);
}
});
Then, both examples pass:
System.out.println(service.convert("12:00", LocalTime.class));
>> 12:00:00.000
System.out.println(service.convert("12:00:00", LocalTime.class));
>> 12:00:00.000
You can skip registering the default converters by creating the service with
new DefaultFormattingConversionService(false);
Finally, I assume in the production code you are actually getting the ConversionService from the ConversionServiceFactoryBean, so you can configure that as follows:
#Bean
public ConversionServiceFactoryBean conversionService() {
ConversionServiceFactoryBean conversionServiceFactoryBean = new ConversionServiceFactoryBean();
Set<Converter<?, ?>> myConverters = new HashSet<>();
myConverters.add(new Converter<String, LocalTime>() {
#Override
public LocalTime convert(String source) {
return LocalTime.parse(source);
}
});
conversionServiceFactoryBean.setConverters(myConverters);
return conversionServiceFactoryBean;
}
Try this:
DateTimeFormatter dtf = DateTimeFormat.forPattern("HH:mm:ss");
LocalTime localTime = dtf.parseLocalTime("12:00:00");
System.out.println("Time"+localTime);

How to store Date field as ISODate() using jackson in MongoDb

I am trying to persist a java object having java.util.Date field in mongo collection using fasterxml jackson.
The problem is the default nature of objectMapper is to store Date as NumberLong type.
For e.g , a createdTime field of java.util.Date type gets stored as below:
"createdTime" : NumberLong("1427728445176")
I want to store it in ISODate format which is available in mongo Shell.
Now, i know there is way to format object mapper to store Date in a String dateformat.
But I am ONLY looking for ISODate() format.
For e.g
"createdTime" : ISODate("2015-01-20T16:39:42.132Z")
Is there a way to do that ?
Please advise gurus .
Thanks in advance for help.
What you need is the Jackson Joda Module. If you import that into your classpath, you can do the following on your mapper to write it as your desired Timestamp:
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
mapper.writeValueAsString(date);
You can replace date in the code sample above with your POJO as necessary.
Edit:
It looks like what you really want is a custom serializer. That would look something like this:
public class IsoDateSerializer extends JsonSerializer<DateTime> {
#Override
public void serialize(DateTime value, JsonGenerator jgen, SerializerProvider provider) {
String isoDate = ISODateTimeFormat.dateTime().print(value);
jgen.writeRaw("ISODATE(\"" + isoDate + "\")");
}
Then you'll either register it on the mapper for all DateTime types
mapper.addSerializer(DateTime.class, new IsoDateSerializer());
or specify it on the function using annotations
#JsonSerializer(using = IsoDateSerializer.class)
public DateTime createdTime;
I was able to serialize the date string to ISODate format. I wrote a customer date serializer like below.
public void serialize(Date date, JsonGenerator jgen, SerializerProvider provider) throws IOException {
String dateValue = getISODateString(date);
String text = "{ \"$date\" : \""+ dateValue +"\"}";
jgen.writeRawValue(text);
}
Based on request from user #mmx73, I am adding code for customer Date DeSeriaizer.
public class IsoDateDeSerializer extends JsonDeserializer<Date> {
#Override
public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
throws IOException, JsonProcessingException {
ObjectCodec oc = jsonParser.getCodec();
JsonNode node = oc.readTree(jsonParser);
String dateValue = node.get("$date").asText();
//DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Date date = null;
try {
date = df.parse(dateValue);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}
None of these answers accomplished what I wanted. I was having trouble because when I serialized the JSON string to MongoDB, it was stored as a String. A nicely formatted string, but a string none the less.
I use the com.fasterxml.jackson.databind.ObjectMapper to convert my objects to/from JSON and I wanted to continue using this class. I have the following method:
public enum JsonIntent {NONE, MONGODB};
public static ObjectMapper getMapper(final JsonIntent intent) {
ObjectMapper mapper = new ObjectMapper();
// Setting to true saves the date as NumberLong("1463597707000")
// Setting to false saves the data as "2016-05-18T19:30:52.000+0000"
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
mapper.registerModule(new JodaModule());
if (intent == JsonIntent.MONGODB) {
// If you want a date stored in MONGO as a date, then you must store it in a way that MONGO
// is able to deal with it.
SimpleModule testModule = new SimpleModule("MyModule", new Version(1, 0, 0, null, null, null));
testModule.addSerializer(Date.class, new StdSerializer<Date>(Date.class) {
private static final long serialVersionUID = 1L;
#Override
public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
try {
if (value == null) {
jgen.writeNull();
} else {
jgen.writeStartObject();
jgen.writeFieldName("$date");
String isoDate = ISODateTimeFormat.dateTime().print(new DateTime(value));
jgen.writeString(isoDate);
jgen.writeEndObject();
}
} catch (Exception ex) {
Logger.getLogger(JsonUtil.class.getName()).log(Level.SEVERE, "Couldn't format timestamp " + value + ", writing 'null'", ex);
jgen.writeNull();
}
}
});
testModule.addDeserializer(Date.class, new StdDeserializer<Date>(Date.class) {
private static final long serialVersionUID = 1L;
#Override
public Date deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JsonProcessingException {
JsonNode tree = jp.readValueAsTree();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
try {
return ISODateTimeFormat.dateTime().parseDateTime(tree.get("$date").textValue()).toDate();
} catch (Throwable t) {
throw new IOException(t.getMessage(), t);
}
}
});
mapper.registerModule(testModule);
}
return mapper;
}
Now, I can run the following test code:
BObjectMapper mapper = getMapper(JsonUtil.JsonIntent.NONE);
Date d1 = new Date();
String v = mapper.writeValueAsString(d1);
System.out.println("Joda Mapping: " + v);
Date d2 = mapper.readValue(v, Date.class);
System.out.println("Decoded Joda: " + d2);
mapper = getMapper(JsonUtil.JsonIntent.MONGODB);
v = mapper.writeValueAsString(d1);
System.out.println("Mongo Mapping: " + v);
d2 = mapper.readValue(v, Date.class);
System.out.println("Decoded Mongo: " + d2);
The output is as follows:
Joda Mapping: "2016-06-13T14:58:11.937+0000"
Decoded Joda: Mon Jun 13 10:58:11 EDT 2016
Mongo Mapping: {"$date":"2016-06-13T10:58:11.937-04:00"}
Decoded Mongo: Mon Jun 13 10:58:11 EDT 2016
Note that the JSON that will be sent to MONGODB defines the value containing a field named "$date". This tells MongoDB that this is a date object it seems.
When I look at Mongo, I see the following:
"importDate" : ISODate("2016-05-18T18:55:07Z")
Now, I can access the field as a date rather than as a string.
To add an encoded JSON string to Mongo, my code is as follows:
MongoDatabase db = getDatabase();
Document d = Document.parse(json);
db.getCollection(bucket).insertOne(d);
In this case, "json" is the encoded JSON string. Because it is coming from a JSON string, it has no way of knowing the types unless it infers this, which is why we needed the "$date" portion. The "bucket" is just a string indicating which table to use.
As a side note, I found out that if I pull a BSON object from Mongo and convert it to a JSON string by calling doc.toJson() (where doc is of type org.bison.Document as returned from a query), the date object is stored with a long value rather than a formatted text string. I did not check to see if I could push data into mongo after formatting in this way, but, you can modify the deserializer shown above to support this as follows:
testModule.addDeserializer(Date.class, new StdDeserializer<Date>(Date.class) {
private static final long serialVersionUID = 1L;
#Override
public Date deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JsonProcessingException {
JsonNode tree = jp.readValueAsTree();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
try {
// Mongo will return something that looks more like:
// {$date:<long integer for milliseconds>}
// so handle that as well.
JsonNode dateNode = tree.get("$date");
if (dateNode != null) {
String textValue = dateNode.textValue();
if (!Util.IsNullOrEmpty(textValue)) {
return ISODateTimeFormat.dateTime().parseDateTime(textValue).toDate();
}
return Util.MillisToDate(dateNode.asLong());
}
return null;
} catch (Throwable t) {
Util.LogIt("Exception: " + t.getMessage());
throw new IOException(t.getMessage(), t);
}
}
});
You can convert milliseconds to a Date or DateTime as follows:
/**
* Convert milliseconds to a date time. If zero or negative, just return
* null.
*
* #param milliseconds
* #return
*/
public static Date MillisToDate(final long milliseconds) {
if (milliseconds < 1) {
return null;
}
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliseconds);
return calendar.getTime();
}
public static DateTime MillisToDateTime(final long milliseconds) {
if (milliseconds < 1) {
return null;
}
return new DateTime(milliseconds);
}
In case you get some message like
com.fasterxml.jackson.core.JsonGenerationException: Can not write a field name, expecting a value.
Make sure you use writeRawValue instead within the accepted answer. That ends the field correctly, otherwise the next field to be serialize may throw this error.
You can solve this issue by reading/writing bson instead of json. Here is a test class:
package com.nagra.jongo.mapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import de.undercouch.bson4jackson.BsonFactory;
import de.undercouch.bson4jackson.deserializers.BsonDateDeserializer;
import de.undercouch.bson4jackson.serializers.BsonDateSerializer;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.Date;
/**
* Uses Bson4Jackson 2.9.0
*
* <!-- https://mvnrepository.com/artifact/de.undercouch/bson4jackson -->
* <dependency>
* <groupId>de.undercouch</groupId>
* <artifactId>bson4jackson</artifactId>
* <version>2.9.2</version>
* </dependency>
*/
public class ObjectMapperTest {
private ObjectMapper mapper = new ObjectMapper(new BsonFactory());
private static class WrappedDate {
private Date Date = new Date(0);
public WrappedDate() {
}
public Date getDate() {
return Date;
}
public void setDate(Date Date) {
this.Date = Date;
}
}
#Before
public void setUp() {
SimpleModule module = new SimpleModule();
module.addSerializer(Date.class, new BsonDateSerializer());
module.addDeserializer(Date.class, new BsonDateDeserializer());
mapper.registerModule(module);
}
#Test
public void testDate() throws IOException {
WrappedDate date = new WrappedDate();
byte[] b = mapper.writeValueAsBytes(date);
WrappedDate i = mapper.readValue(b, WrappedDate.class);
Assert.assertEquals(date.getDate(), i.getDate());
System.out.println(i.getDate());
}}

Conversion of a small XML containfg different data values to POJO

I am currently writing a parser for a simple XML. The XML at its longest contains 18 lines. I am trying to parse it and convert it to a POJO. I know that I can use JAXB or similar libraries, but I felt that considering the size of the XML, that would be an overkill. Also, this is an exercise to think beyond libraries.
An example XML would be:
<machineinfo>
<processorCount>4</processorCount>
<boughtDate>2014-06-09 23:17:49.0</boughtDate>
<installationStatus>COMPLETE</installationStatus>
<machineType>BASIC</machineType>
<osVersion>1849AB48DOED</osVersion>
<serverName>fjv920dk</serverName>
<machineStatus>UP</machineStatus>
<statusPay1>NA</statusPay1>
<statusPay2>NA</statusPay2>
<errorCode>NO_ERROR</errorCode>
<exceptionCode>0</exceptionCode>
<isCloneable>true</isCloneable>
<latestVersion>1849AB48DOED</latestVersion>
<mastermachineName/>
<podName>8D2</podName>
<machineName>machine2</machineName>
</machineinfo>
My core conversion logic is as follows:
if (tagName.equalsIgnoreCase("processorCount")) {
machineInfo.setProcessorCount(new Integer(data).intValue());
} else if (tagName.equalsIgnoreCase("boughtDate")) {
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS");
DateTime dt = formatter.parseDateTime(data);
machineInfo.setBoughtDate(dt);
} else if (tagName.equalsIgnoreCase("installationStatus") {
machineInfo.setInstallationStatus(InstallationStatus.valueOf(data));
} else if (tagName.equalsIgnoreCase("installationStatus") {
machineInfo.setInstallationStatus(InstallationStatus.valueOf(data));
}
As you can see, this leads to multiple if conditions. I tried simplifying that by using a Map as follows:
Map <String, Object> map = new HashMap<String, Object>();
map.put("machineName", data);
map.put("machineType", data);
String mapValue = (String) map.get(tagName);
But how do I determine which setter in machineInfo should be invoked?
You should use Java Reflection or Java Beans API for that.
I like to invoke dynamic setters using PropertyDescriptor:
Method writerMethod = new PropertyDescriptor(fieldName, MachineInfo.class).getWriteMethod();
writerMethod.invoke(machineInstance, fieldValue);
Refer to http://docs.oracle.com/javase/7/docs/api/java/beans/PropertyDescriptor.html
Regards,
Bruno
I solved this by using the following snippet:
ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean();
convertUtilsBean.deregister(DateTime.class);
convertUtilsBean.register(new JodaDateTimeConverter(), DateTime.class);
BeanUtilsBean beanUtilsBean = new BeanUtilsBean(convertUtilsBean, new PropertyUtilsBean());
try {
beanUtilsBean.setProperty(machineInfo, qName, temp);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
And by creating the following converter:
class JodaDateTimeConverter implements Converter {
/* (non-Javadoc)
* #see org.apache.commons.beanutils.Converter#convert(java.lang.Class, java.lang.Object)
*/
#SuppressWarnings("unchecked")
public <T> T convert(Class<T> arg0, Object arg1) {
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS");
DateTime dt = formatter.parseDateTime((String) arg1);
return (T) dt;
}
}

Retrofit GSON serialize Date from json string into java.util.date

I am using the Retrofit library for my REST calls. Most of what I have done has been smooth as butter but for some reason I am having issues converting JSON timestamp strings into java.util.Date objects. The JSON that is coming in looks like this.
{
"date": "2013-07-16",
"created_at": "2013-07-16T22:52:36Z",
}
How can I tell Retrofit or Gson to convert these strings into java.util.Date objects?
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss")
.create();
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(API_BASE_URL)
.setConverter(new GsonConverter.create(gson))
.build();
Or the Kotlin equivalent:
val gson = GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create()
RestAdapter restAdapter = Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(T::class.java)
You can set your customized Gson parser to retrofit. More here: Retrofit Website
Look at Ondreju's response to see how to implement this in retrofit 2.
#gderaco's answer updated to retrofit 2.0:
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss")
.create();
Retrofit retrofitAdapter = new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
Here is how I did it:
Create DateTime class extending Date and then write a custom deserializer:
public class DateTime extends java.util.Date {
public DateTime(long readLong) {
super(readLong);
}
public DateTime(Date date) {
super(date.getTime());
}
}
Now for the deserializer part where we register both Date and DateTime converters:
public static Gson gsonWithDate(){
final GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
#Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
try {
return df.parse(json.getAsString());
} catch (final java.text.ParseException e) {
e.printStackTrace();
return null;
}
}
});
builder.registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
#Override
public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
try {
return new DateTime(df.parse(json.getAsString()));
} catch (final java.text.ParseException e) {
e.printStackTrace();
return null;
}
}
});
return builder.create();
}
And when you create your RestAdapter, do the following:
new RestAdapter.Builder().setConverter(gsonWithDate());
Your Foo should look like this:
class Foo {
Date date;
DateTime created_at;
}
Gson can handle only one datetime format (those specified in builder) plus the iso8601 if parsing with custom format is not possible. So, a solution could be to write your custom deserializer. To solve your problem I defined:
package stackoverflow.questions.q18473011;
import java.util.Date;
public class Foo {
Date date;
Date created_at;
public Foo(Date date, Date created_at){
this.date = date;
this.created_at = created_at;
}
#Override
public String toString() {
return "Foo [date=" + date + ", created_at=" + created_at + "]";
}
}
with this deserializer:
package stackoverflow.questions.q18473011;
import java.lang.reflect.Type;
import java.text.*;
import java.util.Date;
import com.google.gson.*;
public class FooDeserializer implements JsonDeserializer<Foo> {
public Foo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
String a = json.getAsJsonObject().get("date").getAsString();
String b = json.getAsJsonObject().get("created_at").getAsString();
SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdfDateWithTime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date date, created;
try {
date = sdfDate.parse(a);
created = sdfDateWithTime.parse(b);
} catch (ParseException e) {
throw new RuntimeException(e);
}
return new Foo(date, created);
}
}
Final step is to create a Gson instance with right adapter:
package stackoverflow.questions.q18473011;
import com.google.gson.*;
public class Question {
/**
* #param args
*/
public static void main(String[] args) {
String s = "{ \"date\": \"2013-07-16\", \"created_at\": \"2013-07-16T22:52:36Z\"}";
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Foo.class, new FooDeserializer());
Gson gson = builder.create();
Foo myObject = gson.fromJson(s, Foo.class);
System.out.println("Result: "+myObject);
}
}
My result:
Result: Foo [date=Tue Jul 16 00:00:00 CEST 2013, created_at=Tue Jul 16 22:52:36 CEST 2013]
Quite literally if you already have an Date object with the name "created_at" in the class you are creating then it is this easy:
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").create();
YourObject parsedObject1 = gson.fromJson(JsonStringYouGotSomehow, YourObject.class);
And you're done. no complicated overriding needed.
You can define two new classes like this:
import java.util.Date;
public class MyDate extends Date {
}
and
import java.util.Date;
public class CreatedAtDate extends Date {
}
Your POJO will be like this:
import MyDate;
import CreatedAtDate;
public class Foo {
MyDate date;
CreatedAtDate created_at;
}
Finally set your custom deserializer:
public class MyDateDeserializer implements JsonDeserializer<Date> {
public static final SimpleDateFormat sServerDateDateFormat = new SimpleDateFormat("yyyy-MM-dd");
#Override
public MyDate deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (json != null) {
final String jsonString = json.getAsString();
try {
return (MyDate) sServerDateDateFormat.parse(jsonString);
} catch (ParseException e) {
e.printStackTrace();
}
}
return null;
}
}
and
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(MyDate.class, new MyDateDeserializer());
This doesn't answer directly the question asked, but is in my opinion the "state of the art" if the coder has the full freedom of choice on how to solve the problem.
First of all, it's not best solution to use java.util.Date. Reason is that those classes had no ideal behaviour in some corner cases so where superseeded by the Java Instant class etc. check the answer of Basil Bourque in this S.O. question: Creating Date objects in Kotlin for API level less than or equal to 16
So I used the Instant class of ThreeTenABP, and
using Kotlin, on Android:
val gson = GsonBuilder().registerTypeAdapter(Instant::class.java,
JsonDeserializer<Instant> { json: JsonElement, _: Type?, _: JsonDeserializationContext? ->
ZonedDateTime.parse(
json.asJsonPrimitive.asString
).toInstant()
}
).create()
val retrofit = Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()

Categories