Serialize Calendar (java.util.Calendar)

5,728 views
Skip to first unread message

Viktor Eriksson

unread,
Mar 2, 2011, 10:30:27 PM3/2/11
to google-gson
I have an object which has a variable of type Calendar.
Default behaviour seem to be:
"createdTimestamp":{"year":2011,"month":2,"dayOfMonth":3,"hourOfDay":
3,"minute":17,"second":25}

But i would like to make my own custom serializer for Calendar so I
did the following.

private static class MyCalendarSerializer implements
JsonSerializer<Calendar>{

public JsonElement serialize(Calendar arg0, Type arg1,
JsonSerializationContext arg2) {
DateFormat sdf = DateFormat.getDateTimeInstance(DateFormat.SHORT,
DateFormat.SHORT);
return new JsonPrimitive(sdf.format(arg0.getTime()));
}
}

Gson gson = new GsonBuilder().registerTypeAdapter(Calendar.class, new
MyCalendarSerializer()).create();
return gson.toJson(av);

But this does not work, I have made other serializers which work but I
can't get this one to work.
What am I doing wrong ?

Viktor Eriksson

unread,
Mar 3, 2011, 12:55:30 PM3/3/11
to google-gson
GregorianCalendar !

inde...@gmail.com

unread,
Mar 14, 2011, 3:39:57 PM3/14/11
to google-gson
Which version of Gson are you using?
We added support for Calendar in March 2009. Check:
http://code.google.com/p/google-gson/issues/detail?id=106

Raji

unread,
Oct 4, 2011, 9:22:19 PM10/4/11
to googl...@googlegroups.com
I ran into an issue with the default Calendar Serialization in version 1.7.1. The serialization looses milliseconds!! (Lots of libraries miss the fact that the internal representation of Calendar is in milliseconds!!!)

So I had to write custom serialization. The registration is less intuitive. You have to do this:

GsonBuilder gsonB = new GsonBuilder();
gsonB.registerTypeAdapter(Calendar.class, new CalendarSerializer());
gsonB.registerTypeAdapter(GregorianCalendar.class, new CalendarSerializer());

When serializing, the instance class of the field is GregorianCalendar and while de-serializing, the target class has Calendar as the field.

Here is the CalendarSerializer (Note: I loose the timezone)

public class CalendarSerializer implements JsonSerializer<Calendar>, JsonDeserializer<Calendar>{

@Override
public JsonElement serialize(Calendar src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.getTimeInMillis());
}

@Override
public Calendar deserialize(JsonElement json, Type typeOfT,  JsonDeserializationContext context) throws JsonParseException {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(json.getAsJsonPrimitive().getAsLong());
return cal;
}
}

Andrea

unread,
May 23, 2017, 12:52:13 AM5/23/17
to google-gson, eriksso...@gmail.com
Looking around I found that using .registerTypeHierarchyAdapter() rather than .registerTypeHierarchy() solves the problem.

I prefer to serialize directly GregorianCalendar.

To Gson developers: could you please document in the user guide to be careful to type hierarchies? And when to use .registerTypeHierarchyAdapter() ? It would help!

Thanks.
Reply all
Reply to author
Forward
0 new messages