14

I am trying to use Gson to deserialize a json array, but am currently getting a JsonSyntaxException. The json string was created by a .NET MVC3 web service using JsonResult (meaning, I am not manually creating the json, it is being created by a library which I know to work on several other platforms).

This is the json:

[{"PostID":1,"StudentID":39,"StudentName":"Joe Blow",
"Text":"Test message.","CreateDate":"\/Date(1350178408267)\/",
"ModDate":"\/Date(1350178408267)\/","CommentCount":0}]

This is the code:

public class Post {
   public int PostID;
   public int StudentID;
   public String StudentName;
   public String Text;
   public Date CreateDate;
   public Date ModDate;

   public Post() { }
}

Type listOfPosts = new TypeToken<ArrayList<Post>>(){}.getType();
ArrayList<Post> posts = new Gson().fromJson(json, listOfPosts);

The exception says that the date format is invalid:

com.google.gson.JsonSyntaxException: /Date(1350178408267)/

Anyone know what is going on?

mtmurdock
  • 12,756
  • 21
  • 65
  • 108

4 Answers4

19

I found an answer here but I found it strange that there isn't an easier way. Several other json libraries I've used support the .NET json format natively. I was surprised when Gson didn't handle it. There must be a better way. If anyone knows of one, please post it here. All the same, this was my solution:

I created a custom JsonDeserializer and registered it for the Date type. By doing so, Gson will use my deserializer for the Date type instead of its default. The same can be done for any other type if you want to serialize/deserialize it in a custom way.

public class JsonDateDeserializer implements JsonDeserializer<Date> {
   public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
      String s = json.getAsJsonPrimitive().getAsString();
      long l = Long.parseLong(s.substring(6, s.length() - 2));
      Date d = new Date(l);
      return d; 
   } 
}

Then, when I am creating my Gson object:

Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonDateDeserializer()).create();

Now my gson object will be capable of parsing the .NET date format (millis since 1970).

Community
  • 1
  • 1
mtmurdock
  • 12,756
  • 21
  • 65
  • 108
  • 9
    This is the correct way to do it. Given that Microsoft made up their own date format that no-one else uses, I don't find it surprising that Gson doesn't support it out of the box. Why on earth don't they just use ISO-8601 like sane people? – GreyBeardedGeek Oct 14 '12 at 02:26
  • I'm not sure, but what really confused me was that I have used other Json parsing libraries (on other non-microsoft platforms) that handle it just fine. Gson was the first I've found that didn't handle it out of the box. – mtmurdock Oct 14 '12 at 02:34
  • 1
    @GreyBeardedGeek: There's no *sane ISO-8601* datetime format in JSON. There's no datetime format in JSON at all; what you mean is just a plain string. I don't remember the exact reason (and can't find it anymore) why they needed it and why they've chosen this way, but it was quite convincing. Maybe we should ask instead why there's no datetime format in JSON? And no, I'm really nothing like an M$ fan. – maaartinus Oct 15 '12 at 17:20
  • What if I have multiple date formats in my Json response where some of them are supported by Gson and some are not. Can I inspect format in deserialize method and call default implementation when formatted is supported while still do my custom deserialization when format throws exceptions? – Vlado Pandžić May 18 '17 at 10:52
14

Another solution is to use ISO 8601 format. This has to be configured on both Gson side as:

Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create();

as well as on the server side, e.g. for ASP.NET MVC in Global.asax.cs file, as follows:

JsonSerializerSettings serializerSettings = new JsonSerializerSettings();
serializerSettings.Converters.Add(new IsoDateTimeConverter());
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings = serializerSettings;

The advantage of the code above is that it handles both serialization and deserialization and thus allows two way transmission of dates/times.

Note: IsoDateTimeConverter class is part of the JSON.NET library.

Jozef Benikovský
  • 1,121
  • 10
  • 9
5

Serialize and Deserialize methoda. Register this as a Adapter for GSON

JsonSerializer<Date> ser = new JsonSerializer<Date>() {
@Override
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext 
         context) {
return src == null ? null : new JsonPrimitive(src.getTime());
}
};

JsonDeserializer<Date> deser = new JsonDeserializer<Date>() {
 @Override
 public Date deserialize(JsonElement json, Type typeOfT,
   JsonDeserializationContext context) throws JsonParseException {
return json == null ? null : new Date(json.getAsLong());
 }
 };

Gson gson = new GsonBuilder()
.registerTypeAdapter(Date.class, ser)
.registerTypeAdapter(Date.class, deser).create();
Abiram
  • 171
  • 1
  • 4
3

This solution works for me by using SqlDateTypeAdapter:

SqlDateTypeAdapter sqlAdapter = new SqlDateTypeAdapter();
Gson gson = new GsonBuilder()
   .registerTypeAdapter(java.sql.Date.class, sqlAdapter)
   .setDateFormat("yyyy-MM-dd")
   .create();

Ref: https://stackoverflow.com/a/30398307/7308789

Community
  • 1
  • 1
Houssin Boulla
  • 2,687
  • 1
  • 16
  • 22
  • This would not work in this instance, as OP is asking about a Date written as epoch, and your solution refers to date as yyyy-MM-dd. – AlexM Feb 04 '18 at 14:18