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.

Deserialization with Gson.fromJson() should *ignore* missing fields

See original GitHub issue
What steps will reproduce the problem?

1. Create a simple *non-static* class which initializes default values for some 
fields:

    public class Test {

        int x = -1;
        String s = "Hello";
    }

2. Create new Gson and call `fromJson()` with missing fields:

    Test t = new Gson().fromJson("{}", Test.class);
    System.out.println(String.format("x=[%s] s=[%s]", t.x, t.s));

>> Results: x=[0] s=[null]

    Test t = new Gson().fromJson("{\"x\": 1}", Test.class);
    System.out.println(String.format("x=[%s] s=[%s]", t.x, t.s));

>> Results: x=[1] s=[null]

What is the expected output? What do you see instead?

As the test cases, I would like to expect default value for each field to be 
the one that the class initializes. For instance with the first test case, `x` 
should be `-1` and `s` should be "Hello".

What version of the product are you using? On what operating system?

 - Gson 2.2.3
 - OSes:
   + Fedora 18, 64 bit with OpenJDK 1.7.0_19;
   + Android API 4 and API 17;

Thanks,
Hai

Original issue reported on code.google.com by haibison...@gmail.com on 5 May 2013 at 8:23

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:36
  • Comments:33 (1 by maintainers)

github_iconTop GitHub Comments

19reactions
a1573595commented, Oct 16, 2020

Still a problem.

12reactions
transilvladcommented, Jul 10, 2017

Any update on this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Gson optional and required fields - json - Stack Overflow
As you note, Gson has no facility to define a "required field" and you'll just get null in your deserialized object if something...
Read more >
Gson – Exclude or Ignore Fields - HowToDoInJava
Gson allows us to exclude or ignore the fields from Java classes which we want not to be included in serialization and deserialization....
Read more >
Exclude Fields from Serialization in Gson - Baeldung
Gson serializer will ignore every field declared as transient: String jsonString = new Gson().toJson(source); assertEquals(expectedResult, ...
Read more >
GSON library ignores new/missing fields - Bala's Blog
I have recently came across GSON library and found out that it ignores new/missing fields in the JSON response while doing unmarshalling by ......
Read more >
Java Gson - JSON serialization and deserialization ... - ZetCode
Gson by default does not serialize fields with null values to JSON. If a field in a Java object is null , Gson...
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