Gson: JsonSyntaxException on date

后端 未结 4 665
别那么骄傲
别那么骄傲 2020-12-10 13:08

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

相关标签:
4条回答
  • 2020-12-10 13:28

    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();
    
    0 讨论(0)
  • 2020-12-10 13:30

    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.

    0 讨论(0)
  • 2020-12-10 13:35

    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

    0 讨论(0)
  • 2020-12-10 13:43

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

    0 讨论(0)
提交回复
热议问题