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.

Can't deserialized EnumMap with override toString()

See original GitHub issue

Gson version

2.9.0

Java / Android version

JDK 17.0.2

Used tools

  • Maven; version: 3.8.5

Description

When serialize EnumMap to Json, it has strange behavior that calls toString() instead of name() of enum key unlike when serialize the normal enum type do.

Expected behavior

{"single_fruit":"APPLE","multi_fruit":{"APPLE":3,"BANANA":2}}

Actual behavior

{"single_fruit":"APPLE","multi_fruit":{"Golden Apple":3,"Green Banana":2}}

Reproduction steps

See this simple program below.

import com.google.gson.Gson;

import java.util.EnumMap;
import java.util.ResourceBundle;


public class Main {

    public enum Fruit {
        APPLE,
        BANANA;
        private final ResourceBundle bundle = ResourceBundle.getBundle("Main");

        @Override
        public String toString() {
            return bundle.getString(this.name());
        }
    }
    public static class FruitData {
        public Fruit single_fruit;
        public EnumMap<Fruit, Integer> multi_fruit;
    }
    public static void main(String[] args) {
        Gson jsonParser = new Gson();
        FruitData f = new FruitData();
        f.single_fruit = Fruit.APPLE;
        f.multi_fruit = new EnumMap<>(Fruit.class);
        f.multi_fruit.put(Fruit.APPLE, 3);
        f.multi_fruit.put(Fruit.BANANA, 2);

        System.out.println(jsonParser.toJson(f));

        // it should be successful
        // System.out.println(jsonParser.fromJson(jsonParser.toJson(f), FruitData.class));
    }
}

Main.properties

APPLE=Golden Apple
BANANA=Green Banana

Exception stack trace

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Object.getClass()" because "key" is null
	at java.base/java.util.EnumMap.typeCheck(EnumMap.java:736)
	at java.base/java.util.EnumMap.put(EnumMap.java:264)
	at java.base/java.util.EnumMap.put(EnumMap.java:78)
	at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:188)
	at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:145)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:130)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:221)
	at com.google.gson.Gson.fromJson(Gson.java:991)
	at com.google.gson.Gson.fromJson(Gson.java:956)
	at com.google.gson.Gson.fromJson(Gson.java:905)
	at com.google.gson.Gson.fromJson(Gson.java:876)
	at Main.main(Main.java:35)

TODO List

  • Update README/User Guide to add enableComplexMapKeySerialization() things. (Fixed by #2138 )

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
Evydecommented, Jun 27, 2022

Have created #2138 now which also documents Map serialization and deserialization in general. You can see the rendered version here: https://github.com/Marcono1234/gson/blob/marcono1234/map-serialization-user-guide/UserGuide.md#TOC-Maps-Examples

I made this a separate PR and did not include it in any other PR to make it easier for the maintainers to review.

I reviewed this changed README and was extremely satisfied with it. Thanks for your working. This issue will be closed when this PR been merged.

1reaction
Marcono1234commented, Jun 27, 2022

Have created #2138 now which also documents Map serialization and deserialization in general. You can see the rendered version here: https://github.com/Marcono1234/gson/blob/marcono1234/map-serialization-user-guide/UserGuide.md#TOC-Maps-Examples

I made this a separate PR and did not include it in any other PR to make it easier for the maintainers to review.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to deserialize an EnumMap - Stack Overflow
Here's an implementation that works. But.. it's kinda janky. public JsonDeserializer<EnumMap<FRUIT, String>> deserializeFruitMap(){ return ...
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 >
Enum Mappings with Hibernate - The Complete Guide
You just can't rename a value without migrating your database. ... Hibernate's EnumType, you just need to override 1 method to set the...
Read more >
Using @JsonFormat to serialize Java Enum As Object - LogicBig
OBJECT to serialize (but not deserialize) enums as JSON Objects (as if ... employeeType = employeeType; } @Override public String toString() ...
Read more >
Solutions to Exercises - Springer Link
The answer is false: a class cannot inherit constructors. 9. Overriding a method ... You should override toString() to provide a concise but...
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