mtmurdock
mtmurdock

Reputation: 13062

Gson: JsonSyntaxException on date

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?

Upvotes: 14

Views: 28949

Answers (4)

Houssin Boulla
Houssin Boulla

Reputation: 2859

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

Upvotes: 3

Jozef Benikovsk&#253;
Jozef Benikovsk&#253;

Reputation: 1141

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.

Upvotes: 14

Abiram
Abiram

Reputation: 171

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();

Upvotes: 5

mtmurdock
mtmurdock

Reputation: 13062

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).

Upvotes: 19

Related Questions