Deserialization with Gson.fromJson() should *ignore* missing fields
See original GitHub issueWhat 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:
- Created 9 years ago
- Reactions:36
- Comments:33 (1 by maintainers)
Top 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 >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
Still a problem.
Any update on this?