2

My json input looks like:

{ user: "sample-user", date : 1225864800 }

And my DateDeserializer class is:

private class DateDeserializer implements JsonDeserializer<Date>
{
    public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException
    {
        System.out.println("Deserializer...");
        return new Date(json.getAsJsonPrimitive().getAsLong());
    }
}

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Date.class, new DateDeserializer());
gson.fromJson(new FileReader("input.json"), MyType.class);

Even after setting up everything, I'm getting

java.text.ParseException: Unparseable Date: "1225864800"

Am I doing anything wrong here? Please help.

Kevindra
  • 1,682
  • 3
  • 24
  • 45
  • 1
    It's Epoch time stamp. [link](http://en.wikipedia.org/wiki/Epoch_(reference_date)) – Kevindra Jan 07 '13 at 14:10
  • May be this will help - http://stackoverflow.com/questions/5489083/how-can-i-make-jackson-deserialize-a-long-to-a-date-object – Avinash T. Jan 07 '13 at 14:17
  • 2
    Please post your actual code, the actual JSON, and the actual stack trace. The code you have here shouldn't be throwing that exception, and the JSON you show isn't valid JSON. – Brian Roach Jan 07 '13 at 19:53

2 Answers2

0

You're trying to deserialize the entire Json structure as a Date, not just the date field. You'd need something like:

  User user = jsonElement.getAsJsonObject().get( "user" ).getAsString();
  Date date = new Date(jsonElement.getAsJsonObject().get( "date" ).getAsLong());
Chris Kessel
  • 5,583
  • 4
  • 36
  • 55
0

You could write an adapter for the whole MyType class so that you can keep standard date deserialization on other places (if any), and limit your specific deserialization only inside MyType.

To explain better what I mean, here's the code you can copy&paste&run:

package stackoverflow.questions.q14197557;

import java.lang.reflect.Type;
import java.util.Date;

import com.google.gson.*;

public class Q14197557 {

    public static class MyType {

        String user;
        Date date;
        @Override
        public String toString() {
            return "MySample [user=" + user + ", date=" + date + "]";
        }

    }

    public static class MySampleDeserializer implements JsonDeserializer<MyType> {
        public MyType deserialize(JsonElement json, Type typeOfT,
                JsonDeserializationContext context) throws JsonParseException {

           if (json == null)
               return null;
           else {
               MyType s = new MyType();
               s.user = json.getAsJsonObject().get("user").getAsString();
               s.date = new Date(json.getAsJsonObject().get("date").getAsLong());
               return s;

           }

        }
    }

    public static void main(String[] args) {

        String json = "{ user: \"sample-user\", date : 1225864800 }";
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeAdapter(MyType.class, new MySampleDeserializer());
        MyType s = gsonBuilder.create().fromJson(json, MyType.class);

        System.out.println("My Type: " + s);
    }

}
giampaolo
  • 6,906
  • 5
  • 45
  • 73