FranzGames / google-gson

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

Can't register a custom Date serializer #352

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create a DateAdapter
public class DateAdapter implements JsonSerializer<Date>, 
JsonDeserializer<Date> {

    @Override
    public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
        if(src == null) {
            return new JsonNull();
        }
        return new JsonPrimitive(src.getTime());
    }

    @Override
    public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        if(json.isJsonNull()) {
            return null;
        }
        if(!json.isJsonPrimitive() || !json.getAsJsonPrimitive().isNumber()) {
            throw new JsonParseException("Invalid type for Date, must be a numeric timestamp!");
        }

        return new Date(json.getAsLong());
    }

}

2. Register it using GsonBuilder.registerTypeAdapter
Gson gson = new GsonBuilder()
        .registerTypeAdapter(Date.class,new DateAdapter())
                .create();

3. Execute test cases

    public void testDateDeserialization() throws Exception {
        assertEquals(null, gson.fromJson("null", Date.class));
        assertEquals(new Date(10), gson.fromJson("10", Date.class));
    }

    public void testDateSerialization() throws Exception {
        assertEquals(null, gson.toJson(null, Date.class));
        assertEquals("10", gson.toJson(new Date(10), Date.class));
    }

What is the expected output? What do you see instead?
 These test cases were passing when using gson-1.4, upgraded to gson-1.7.1 and they started failing.

What version of the product are you using? On what operating system?
  Upgrading from gson-1.4 to gson-1.7.1 introduced the issue.

Please provide any additional information below.
  Since Date does not contain a timezone, I find transmitting it as a long (milliseconds since epoch UTC) much less error prone, if I could enable this behavior again I would really appreciate that. Thanks.

Original issue reported on code.google.com by tedj...@gmail.com on 3 Aug 2011 at 9:17

GoogleCodeExporter commented 9 years ago
Sorry, seems to be an issue with my build process, that test started passing 
again. Failed on both my machine and the build server, but it was spurious, 
sorry for the noise.

Original comment by tedj...@gmail.com on 3 Aug 2011 at 9:23

GoogleCodeExporter commented 9 years ago
I pulled it together in one test case, it's registerTypeHierarchyAdapter which 
can't be used for Date. I'ld unpost the bug if I could, but I can't, so here is 
a test case that exercises it.

Original comment by tedj...@gmail.com on 3 Aug 2011 at 9:36

Attachments:

GoogleCodeExporter commented 9 years ago
So registerTypeHierarchyAdapter isn't working on Date.class? Sounds like it 
could be a real problem.

Original comment by limpbizkit on 4 Aug 2011 at 6:43

GoogleCodeExporter commented 9 years ago
No, you can't registerTypeHiearchyAdapter on Date.class. Try the test case. I'm 
not sure what intended behavior would be, but  because there is an implicit 
adapter added specifically for Date.class in the GsonBuilder regardless of 
whether or not you call .setDateFormat, you can't register a hiearchy adapter. 
This may be an issue if you use javax.sql.Date.

Original comment by tedj...@gmail.com on 5 Aug 2011 at 3:09

GoogleCodeExporter commented 9 years ago
it's a problem. you can just not serialize date to a long value

Original comment by chenzhikong@gmail.com on 5 Sep 2011 at 3:43

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
This issue was closed by revision r1097.

Original comment by limpbizkit on 29 Dec 2011 at 7:27

GoogleCodeExporter commented 9 years ago
Fixed in Gson 2.1.

Original comment by limpbizkit on 29 Dec 2011 at 7:27

GoogleCodeExporter commented 9 years ago
Still seeing this in Gson 2.2.4.

Original comment by Guy.Padd...@gmail.com on 28 Sep 2013 at 8:55

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
I'm still observing that in Gson 2.3.

Original comment by tobo...@gmail.com on 17 Dec 2014 at 12:49