Issue 513 in google-gson: Deserialization with Gson.fromJson() should *ignore* missing fields

40 views
Skip to first unread message

googl...@googlecode.com

unread,
May 5, 2013, 4:23:38 PM5/5/13
to google-gson...@googlegroups.com
Status: New
Owner: ----
Labels: Type-Defect Priority-Medium

New issue 513 by haibison...@gmail.com: Deserialization with
Gson.fromJson() should *ignore* missing fields
http://code.google.com/p/google-gson/issues/detail?id=513

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

--
You received this message because this project is configured to send all
issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings

googl...@googlecode.com

unread,
Aug 3, 2013, 9:22:52 AM8/3/13
to google-gson...@googlegroups.com

Comment #1 on issue 513 by haripr...@gmail.com: Deserialization with
Gson.fromJson() should *ignore* missing fields
http://code.google.com/p/google-gson/issues/detail?id=513

Is this still an issue. Did u try it in 2.2.4?

Its working for me on Windows with GSON 2.2.4

googl...@googlecode.com

unread,
Aug 4, 2013, 7:35:14 AM8/4/13
to google-gson...@googlegroups.com

Comment #2 on issue 513 by haibison...@gmail.com: Deserialization with
Gson.fromJson() should *ignore* missing fields
http://code.google.com/p/google-gson/issues/detail?id=513

Hari,

Yes it is. I've just tested with Gson 2.2.4 on Fedora 19, 64 bit with
OpenJDK 64-Bit Server VM (build 23.7-b01, mixed mode).

googl...@googlecode.com

unread,
Nov 21, 2013, 8:12:32 PM11/21/13
to google-gson...@googlegroups.com

Comment #3 on issue 513 by krt...@gmail.com: Deserialization with
Gson.fromJson() should *ignore* missing fields
http://code.google.com/p/google-gson/issues/detail?id=513

Apparently, if you have a default constructor for the class, it will work
as you like.

public Test() { }

and that should be enough for the instance variables to be initialized as
declared.

googl...@googlecode.com

unread,
Nov 22, 2013, 2:28:39 AM11/22/13
to google-gson...@googlegroups.com

Comment #4 on issue 513 by haibison...@gmail.com: Deserialization with
Gson.fromJson() should *ignore* missing fields
http://code.google.com/p/google-gson/issues/detail?id=513

@krt...

Thanks for your help. Unfortunately it's not working. I still got "x=[0]
s=[null]". Gson v2.2.4.

googl...@googlecode.com

unread,
Jan 31, 2014, 2:16:34 AM1/31/14
to google-gson...@googlegroups.com

Comment #5 on issue 513 by kamui.ji...@gmail.com: Deserialization with
Gson.fromJson() should *ignore* missing fields
http://code.google.com/p/google-gson/issues/detail?id=513

Confirm, this is still not working.
No way to assign default values for fields.
If field is missing in JSON it is deserialized as null.
Gson v2.2.4

googl...@googlecode.com

unread,
Feb 1, 2014, 7:32:53 PM2/1/14
to google-gson...@googlegroups.com

Comment #6 on issue 513 by Maaarti...@gmail.com: Deserialization with
Gson.fromJson() should *ignore* missing fields
http://code.google.com/p/google-gson/issues/detail?id=513

This is most probably related to issue 495, see the links in the posting
from Jun 23, 2013.

googl...@googlecode.com

unread,
Jun 16, 2015, 3:54:44 AM6/16/15
to google-gson...@googlegroups.com

Comment #7 on issue 513 by xz4...@gmail.com: Deserialization with
Gson.fromJson() should *ignore* missing fields
https://code.google.com/p/google-gson/issues/detail?id=513

This issue not exist with version 2.3.1

googl...@googlecode.com

unread,
Jun 16, 2015, 3:55:45 AM6/16/15
to google-gson...@googlegroups.com

Comment #8 on issue 513 by xz4...@gmail.com: Deserialization with
Gson.fromJson() should *ignore* missing fields
https://code.google.com/p/google-gson/issues/detail?id=513

This issue doesn't exist with version 2.3.1.
Reply all
Reply to author
Forward
0 new messages