Grox18 / google-gson

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

Enum serializes to null when nothing matches (instead of throwing IOException) #608

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
> What steps will reproduce the problem?

1. Implement a POJO that has an enum
2. Attempt to deserialize the POJO using a bad string for the enum's value
3. See that the enum's value is set to null

> What is the expected output? What do you see instead?

I expect to see an exception (probably IOException?), but instead the value is 
initialized to null.

> What version of the product are you using? On what operating system?

GSON 2.3, Android 4.4.2

> Please provide any additional information below.

Here's the relevant part of TypeAdapters.EnumTypeAdapter

    public T read(JsonReader in) throws IOException {
      if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
      }
      return nameToConstant.get(in.nextString());
    }

`nameToConstant.get()` gives null when nothing is found.

Original issue reported on code.google.com by me@benjam.info on 12 Nov 2014 at 2:17

GoogleCodeExporter commented 9 years ago
It is debatable what the correct behavior is. Gson in general ignores 
additional fields or input.

You can easily write a custom type adapter for your enum to assert the correct 
behavior.

Original comment by inder123 on 12 Nov 2014 at 10:16