-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't deserialized EnumMap with override toString() #2131
Comments
Thanks for mentioning these issues. gson/gson/src/main/java/com/google/gson/internal/bind/MapTypeAdapterFactory.java Line 244 in 96ab171
Now I'm certainty this caused this problem: It should process EnumMap independently. After checking these issues, I also find a temporary method to solve this issue(Also use code above for demonstration): import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.EnumMap;
import java.util.ResourceBundle;
public class Main {
public enum Fruit {
@SerializedName("APPLE")
APPLE,
@SerializedName("BANANA")
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 GsonBuilder().enableComplexMapKeySerialization().create();
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 now
System.out.println(jsonParser.fromJson(jsonParser.toJson(f), FruitData.class));
}
} Diff from code above: import com.google.gson.GsonBuilder;
...
@SerializedName("APPLE")
APPLE,
@SerializedName("BANANA")
BANANA;
...
Gson jsonParser = new GsonBuilder().enableComplexMapKeySerialization().create();
... But this issue should be maintain open as this bug still exists after those issues & PRs. |
I think that is unrelated to your original issue since that code is used for 'complex map key serialization' (which you had not enabled initially). For non-'complex map key serialization' (the default) the problem is the usage of gson/gson/src/main/java/com/google/gson/internal/bind/MapTypeAdapterFactory.java Line 206 in 96ab171
Your workaround to use |
Got it, thanks. |
Yes might indeed be good to mention What do you think? If you want I can create a pull request with an initial draft for this. |
I think it is no necessary to open an independent PR to update README especially this only needs just a single sentence such as:
, you can do this in your next feature/fix PR. |
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. |
Gson version
2.9.0
Java / Android version
JDK 17.0.2
Used tools
Description
When serialize EnumMap to Json, it has strange behavior that calls
toString()
instead ofname()
of enum key unlike when serialize the normal enum type do.Expected behavior
Actual behavior
Reproduction steps
See this simple program below.
Main.properties
Exception stack trace
TODO List
enableComplexMapKeySerialization()
things. (Fixed by Document Map serialization in user guide #2138 )The text was updated successfully, but these errors were encountered: