question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 9 years ago
  • Reactions:3
  • Comments:13

github_iconTop GitHub Comments

72reactions
inder123commented, Dec 13, 2018

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?

14reactions
inder123commented, Jan 8, 2016

You can always install your custom type adapter for your enum that enforces this. Gson isn’t going to support this, sorry.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found