19

I need to deserialize json which is an array of date/long values. Here is an example of the returned JSON:

[{"2011-04-30T00:00:00-07:00":100}, {"2011-04-29T00:00:00-07:00":200}]

Using GSON I am able to deserialize this to a List<Map<Date,String>>, but would like to be able to convert it to a List<MyCustomClass> similar to:

public class MyCustomClass() { 
    Date date;
    Long value;
}

I cannot seem to find a way to instruct GSON to map the key/value of the JSON map to the date/value fields in my custom class. Is there a way to do this, or is a list of maps the only route?

Rich Kroll
  • 3,995
  • 3
  • 23
  • 28
  • It wont be able to unless your document looks like `[{"date":"2011"2011-04-30T00:00:00-07:00","value":100}, {"date":"2011-04-29T00:00:00-07:00","value":200}]` – lobster1234 May 01 '11 at 02:59
  • @lobster: really? My answer says otherwise (and was posted before your comment) – Matt Ball May 01 '11 at 14:19
  • @Matt - My solution is on the input side, where if his document were the way I recommended, he could have just done `gson.fromJSON(inputJson, List)` without going through a lot of code. But I agree, your solution is more generic and fits his need right away. – lobster1234 May 01 '11 at 20:12

2 Answers2

26

You need to write a custom deserializer. You also need to use a time zone format that SimpleDateFormat can actually parse. Neither z nor Z will match -07:00, which is a weird mix of RFC 822 time zone format (-0700) or a "general time zone" (Mountain Standard Time or MST or GMT-07:00). Alternately, you can stick with the exact same time zone format, and use JodaTime's DateTimeFormat.

MyCustomClass.java

public class MyCustomClass
{
    Date date;
    Long value;

    public MyCustomClass (Date date, Long value)
    {
        this.date = date;
        this.value = value;
    }

    @Override
    public String toString()
    {
        return "{date: " + date + ", value: " + value + "}";
    }
}

MyCustomDeserializer.java

public class MyCustomDeserializer implements JsonDeserializer<MyCustomClass>
{
    private DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz");

    @Override
    public MyCustomClass deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext ctx) throws JsonParseException
    {
        JsonObject obj = json.getAsJsonObject();
        Entry<String, JsonElement> entry = obj.entrySet().iterator().next();
        if (entry == null) return null;
        Date date;
        try
        {
            date = df.parse(entry.getKey());
        }
        catch (ParseException e)
        {
            e.printStackTrace();
            date = null;
        }
        Long value = entry.getValue().getAsLong();
        return new MyCustomClass(date, value);
    }
}

GsonTest.java

public class GsonTest
{
    public static void main(String[] args)
    {
        // Note the time zone format tweak (removed the ':')
        String json = "[{\"2011-04-30T00:00:00-0700\":100}, {\"2011-04-29T00:00:00-0700\":200}]";

        Gson gson =
            new GsonBuilder()
            .registerTypeAdapter(MyCustomClass.class, new MyCustomDeserializer())
            .create();
        Type collectionType = new TypeToken<Collection<MyCustomClass>>(){}.getType();
        Collection<MyCustomClass> myCustomClasses = gson.fromJson(json, collectionType);
        System.out.println(myCustomClasses);
    }
}

All of the above code is on Github, feel free to clone (though you'll get code for answers to other questions as well).

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
0

Very similar to Matts answer but using Joda:

import java.lang.reflect.Type;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

public class DateTimeSerializer implements JsonDeserializer<DateTime> {

    private DateTimeFormatter parser = DateTimeFormat.forPattern("YYYY-MM-dd'T'HH:mm:ss.SS'Z'").withZoneUTC();

    @Override
    public DateTime deserialize(JsonElement json, Type typeOfT,
            JsonDeserializationContext ctx) throws JsonParseException {
        String dateTimeString = ctx.eserialize(json, String.class);
        return parser.parseDateTime(dateTimeString);
    }

}
reto
  • 16,189
  • 7
  • 53
  • 67