Enum serializes to null when nothing matches (instead of throwing IOException)
See original GitHub issue> 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
Issue Analytics
- State:
- Created 9 years ago
- Reactions:3
- Comments:13
Top Results From Across the Web
Jackson enum Serializing and DeSerializer - java
public void serialize(final JsonEnum enumValue, final JsonGenerator gen, final SerializerProvider serializer) throws IOException, JsonProcessingException ...
Read more >Java Enum Lookup by Name or Field Without Throwing ...
It's iterating over all enums until it finds the matching enum or returning null with a worst case of N where N is...
Read more >How To Serialize and Deserialize Enums with Jackson
In this quick tutorial, we'll learn how to control the way Java Enums are serialized and deserialized with Jackson 2.
Read more >DeserializationFeature (jackson-databind 2.7.0 API) - FasterXML
public enum DeserializationFeature extends Enum<DeserializationFeature> implements ... an exception ir thrown; if false, null value is used instead.
Read more >Java Object Serialization Specification: 2 - Object Output ...
Its methods control the traversal of objects to be serialized to save the ... public final void writeObject(Object obj) throws IOException; public void...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
How about something like this:
@SerializedName(defaultValue = TrafficLight.RED) public enum TrafficLight { RED, ORANGE, GREEN }
// defaultValue will also allowed to be null // if this annotation is not present, enum maintains current behavior.
Comments @JakeWharton @swankjesse @joel-leitch?
You can always install your custom type adapter for your enum that enforces this. Gson isn’t going to support this, sorry.