Deserisation fails with when using null numeric primitives as map keys
See original GitHub issueWhat did you do:
Ran:
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.HashMap;
import java.util.Map;
public class Test {
static class MyType {
public Map<Integer, Integer> map = new HashMap<>();
}
public static void main(String[] args) {
MyType instance = new MyType();
instance.map.put(null, 1);
Gson serialiser = new GsonBuilder()
.enableComplexMapKeySerialization()
.serializeNulls()
.setPrettyPrinting()
.create();
String data = serialiser.toJson(instance);
System.out.println(data);
try {
MyType newInstance = serialiser.fromJson(data, MyType.class);
System.out.println("Success");
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
}
What happened
Deserialisation fails:
{
"map": {
"null": 1
}
}
com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: For input string: "null"
at com.google.gson.internal.bind.TypeAdapters$7.read(TypeAdapters.java:228)
at com.google.gson.internal.bind.TypeAdapters$7.read(TypeAdapters.java:218)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:186)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:145)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
at com.google.gson.Gson.fromJson(Gson.java:927)
at com.google.gson.Gson.fromJson(Gson.java:892)
at com.google.gson.Gson.fromJson(Gson.java:841)
at com.google.gson.Gson.fromJson(Gson.java:813)
at Test.main(Test.java:27)
Caused by: java.lang.NumberFormatException: For input string: "null"
at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.base/java.lang.Double.parseDouble(Double.java:543)
at com.google.gson.stream.JsonReader.nextInt(JsonReader.java:1201)
at com.google.gson.internal.bind.TypeAdapters$7.read(TypeAdapters.java:226)
... 11 more
What did you expect to happen:
Either both serialisation and deserilisation to either succeed or fail (making the handling consistent between the two modes).
It seems that only one mode does not handle this case.
I know that null
is an invalid json map key, but perhaps the library should handle a special “null” key when the type is a nullable primitive.
It seems that this is also broken for strings, as you can serialise a null
, and get "null"
back after deserilisation.
Note
Strangely it works okay if you serialise a simple map (not embedded in an object).
Issue Analytics
- State:
- Created 5 years ago
- Comments:5
Top Results From Across the Web
Deserialization fails with when using null as map key
You can create TypeAdapter for Integer : Gson gson = new GsonBuilder() .serializeNulls() .serializeSpecialFloatingPointValues() .
Read more >DeserializationFeature (jackson-databind 2.6.0 API) - FasterXML
Feature that determines whether encountering of JSON null is an error when deserializing into Java primitive types (like 'int' or 'double').
Read more >Jackson - Working with Maps and nulls - Baeldung
How to serialize Maps with a null key or null values using Jackson.
Read more >DeserializationFeature (jackson-databind 2.4.5 API) - javadoc.io
Feature that determines whether encountering of JSON null is an error when deserializing into Java primitive types (like 'int' or 'double').
Read more >Java Map - Jenkov.com
to a Map as key or value, the primitive values will be auto-boxed before being ... Here is an example of using a...
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
What exactly would you be breaking? Preventing null key serialisation? What about deserialising “null” strings into nulls for boxed primitives (other than string I guess)
I also think it is an issue, here is the code:
Scenario 1:
The code runs well and got the result
Scenario 2:
The code runs failed, and get an exception:
So it seems Gson can only handle the “NULL” when it is the value of the map.