Unable to deserialize a null property into a JsonObject
See original GitHub issueRepro:
import com.google.gson.Gson;
import com.google.gson.JsonObject;
public class GsonDeserialization {
public static void main(String... args) {
final Gson gson = new Gson();
// succeeds
final MyObject mo1 = gson.fromJson("{ \"name\": null, \"otherProperties\": {} }", MyObject.class);
// throws
final MyObject mo2 = gson.fromJson("{ \"name\": \"Alex\", \"otherProperties\": null }", MyObject.class);
}
public static class MyObject {
public String name;
public JsonObject otherProperties;
}
}
Exception in thread "main" com.google.gson.JsonSyntaxException: Expected a com.google.gson.JsonObject but was com.google.gson.JsonNull
at com.google.gson.internal.bind.TypeAdapters$35$1.read(TypeAdapters.java:896)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
at com.google.gson.Gson.fromJson(Gson.java:888)
at com.google.gson.Gson.fromJson(Gson.java:853)
at com.google.gson.Gson.fromJson(Gson.java:802)
at com.google.gson.Gson.fromJson(Gson.java:774)
at com.andrew.scratch.GsonDeserialization.main(GsonDeserialization.java:13)
I would expect the second example to deserialize into an object with a (java) null otherProperties
member, but instead it throws a JsonSyntaxException.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Unable to deserialize a null property into a JsonObject #1684
To my understanding, JsonElement implies the null object design pattern implementation, where null is never permitted in favor of JsonNull , and ...
Read more >Deserialize with gson and null values - Stack Overflow
I fixed it by changing the type of subEntry from JsonObject to JsonElement which is a parent class of both JsonObject and JsonNull...
Read more >Deserialize null JsonString - Google Groups
Hi gson folks. I am having a problem to deserialize a json field that is null. Imagine the following json object: { "key"...
Read more >Migrate from Newtonsoft.Json to System.Text.Json - .NET
Newtonsoft.Json can serialize and deserialize fields as well as properties. In System.Text.Json, use the JsonSerializerOptions.IncludeFields ...
Read more >Deserialization with System.Text.Json - Marc Roussy
JsonSerializer · Have a POCO that matches the JSON data, or it's easy to create one. · Need to use most of the...
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
@Marcono1234 maybe, but I’m not 100% confident about that and perhaps there should be a comment from the core team to get their design choice and insight first. I just think that having that described in the documentation would tell how to use it properly (prefer
JsonElement
overJsonObject
is there can be nulls) and how to deal with “real” nulls (no property in the JSON document, so the holder can benull
and notJsonNull.INSTANCE
as expected + explaining that it can be fixed with a custom post-processing interceptor and why applying such an interceptor may cost a little performance) regardless how cumbersome it might be.This issue is really interesting anyway. 😃
Putting it in the documentation is a good idea either way! Do you want to create a pull request?