Of Code and Me

Somewhere to write down all the stuff I'm going to forget and then need

Getting Json.Net and Gson json libraries to play nicely with dates using ISO formatting January 28, 2011

Filed under: C#,Java,Web,WebServices — Rupert Bates @ 2:38 pm

I am working on something at the moment where I have a service written in .net which serves json created using the Json.Net library. This is then consumed by a client app written in Java which uses the Gson library to deserialize that json into Java classes. This was all working fine except for dates which caused parse errors in Gson.

The solution I am using is:

use ISO time format when serializing with Json.Net

    string json = JsonConvert.SerializeObject(entry, new IsoDateTimeConverter());    

When Deserializing with Gson set the date format as follows:

        GsonBuilder b = new GsonBuilder();
        b.setDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        Gson gson = b.create();
        gson.fromJson(json, MyObjectType.class);
Advertisements
 

One Response to “Getting Json.Net and Gson json libraries to play nicely with dates using ISO formatting”

  1. Murat Says:

    You saved my time sir, thanks 🙂


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s