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.

Unable to deserialize a null property into a JsonObject

See original GitHub issue

Repro:

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:open
  • Created 3 years ago
  • Reactions:2
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
lyubomyr-shaydarivcommented, May 4, 2020

@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 over JsonObject is there can be nulls) and how to deal with “real” nulls (no property in the JSON document, so the holder can be null and not JsonNull.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. 😃

0reactions
Marcono1234commented, May 3, 2020

The more I think of this issue, the more I like the idea of just putting this pitfall into the documentation only.

Putting it in the documentation is a good idea either way! Do you want to create a pull request?

Read more comments on GitHub >

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

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