FranzGames / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

DefaultDateTypeAdapter throws JsonSyntaxException on deserializing an empty string #477

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
When you have JSON containing a field (mapped as Date) which is set to the 
empty string, e.g. {..., "myDateField":"", ...} the DefaultDateTypeAdapter 
throws an JsonSyntaxException in the method "deserializeToDate" because an 
emtpy string value can not be parsed by DateFormat. 

Would it not make sense in the "deserialize" method to check for an emtpy 
string before calling "deserializeToDate" and if an empty string is found to 
return null?

    public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        if (!(json instanceof JsonPrimitive)) {
            throw new JsonParseException("The date should be a string value");
        }
        String value = json.getAsString();
        if (value==null || "".equals(value)) {
            return null;
        }
        Date date = deserializeToDate(json);
...

Original issue reported on code.google.com by ronald.p...@googlemail.com on 9 Oct 2012 at 9:42

GoogleCodeExporter commented 9 years ago
Null isn't an empty string. You can write your own TypeAdapter if you want this 
behavior.

Original comment by limpbizkit on 4 Feb 2013 at 3:32