Deserialize from Long to Date

3,324 views
Skip to first unread message

James

unread,
May 9, 2011, 6:08:17 PM5/9/11
to google-gson
Hi guys,

I have a Date member of a POJO and the json string I am deserializing
contains the Date represented as a long. At the moment I am getting
the error: java.text.ParseException: Unparseable date:
"1236918168000".

I have created a Date Deserializer and registered it as Type Adapter
but it seems to never get called.

Ran Berenfeld

unread,
May 11, 2011, 9:30:04 AM5/11/11
to googl...@googlegroups.com
I am able to serialize longs to dates and vice verca in my web page www.ichess.co.il application
here is my custom deserializing: and serializing :

builder.registerTypeAdapter(java.util.Date.class, new JsonSerializer<java.util.Date>() {
@Override
public JsonElement serialize(java.util.Date src, Type typeOfSrc, JsonSerializationContext context) {
Utils.debug("serializing : " + src);
return new JsonPrimitive(src.getTime());
}
});

builder.registerTypeAdapter(java.util.Date.class, new JsonDeserializer<java.util.Date>() {
@Override
public java.util.Date deserialize(com.google.gson.JsonElement p1, java.lang.reflect.Type p2,
com.google.gson.JsonDeserializationContext p3) {
Utils.debug("deserializing : " + p1);
return new java.util.Date(p1.getAsLong());
}
});



--
You received this message because you are subscribed to the Google Groups "google-gson" group.
To post to this group, send email to googl...@googlegroups.com.
To unsubscribe from this group, send email to google-gson...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-gson?hl=en.


James

unread,
May 11, 2011, 5:06:36 PM5/11/11
to google-gson
Hi Ran, thanks for the reply. I registered the Date deserializer
exactly the same same way (but I did not use an anonymous inner
class). I placed a break point on the deserialize method and it was
never called. I will get a snippet, but its pretty much exactly the
same.

On May 11, 2:30 pm, Ran Berenfeld <berenfeld...@gmail.com> wrote:
> I am able to serialize longs to dates and vice verca in my web pagewww.ichess.co.ilapplication
> here is my custom deserializing: and serializing :
>
> builder.registerTypeAdapter(java.util.Date.class, new
> JsonSerializer<java.util.Date>() {
> @Override
> public JsonElement serialize(java.util.Date src, Type typeOfSrc,
> JsonSerializationContext context) {
> Utils.debug("serializing : " + src);
> return new JsonPrimitive(src.getTime());
>
> }
> });
>
> builder.registerTypeAdapter(java.util.Date.class, new
> JsonDeserializer<java.util.Date>() {
> @Override
> public java.util.Date deserialize(com.google.gson.JsonElement p1,
> java.lang.reflect.Type p2,
> com.google.gson.JsonDeserializationContext p3) {
> Utils.debug("deserializing : " + p1);
> return new java.util.Date(p1.getAsLong());
>
>
>
>
>
>
>
> }
> });

Ran Berenfeld

unread,
May 12, 2011, 5:41:47 AM5/12/11
to googl...@googlegroups.com
James
well just to be sure - when the data is send from the javascript to the server, the date have to be changed
to long using getTime() function, right ?

James

unread,
May 12, 2011, 2:03:08 PM5/12/11
to google-gson
This is to deserialize a json response from the web server. The Date
object on the server is transformed into its json representation using
getTime() yes and then sent out to the client.

inde...@gmail.com

unread,
May 15, 2011, 2:55:44 PM5/15/11
to googl...@googlegroups.com
In the Type that you are deserializing into, do you have a subclass of Date? If so, your date deserializer will not get called as it is registered to handle for Date.class only. 

To fix this, you should use the Gson 1.7.1 method, registerTypeHierarchyAdapter.
Reply all
Reply to author
Forward
0 new messages