Issue 540 in google-gson: fromJson returns null for empty string

79 views
Skip to first unread message

googl...@googlecode.com

unread,
Oct 30, 2013, 12:39:40 PM10/30/13
to google-gson...@googlegroups.com
Status: New
Owner: ----
Labels: Type-Defect Priority-Medium

New issue 540 by dmarti...@swiftkey.net: fromJson returns null for empty
string
http://code.google.com/p/google-gson/issues/detail?id=540

What steps will reproduce the problem?

public void testCase() {
String json = "";
final Map<String, String> cachedMap = new Gson().fromJson(json, new
TypeToken<Map<String, String>>() {}.getType());
assertNotNull(cachedMap);
}

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

It probably should throw an exception (JsonParseException or maybe a Syntax
one).

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

2.2.4, Ubuntu 12.10 and Android 4.3

Please provide any additional information below.


--
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,
Jan 9, 2014, 6:10:42 AM1/9/14
to google-gson...@googlegroups.com

Comment #1 on issue 540 by jul...@julianrhindconsulting.com: fromJson
Hi

I just encountered this problem - but it's wider than the poster above
suggests

From the documentation

public <T> T fromJson(Reader json, Class<T> classOfT)
Returns:
an object of type T from the string
Throws:
JsonIOException - if there was a problem reading from the Reader
JsonSyntaxException - if json is not a valid representation for an object
of type

However - it doesn't throw any exceptions no matter what you throw at it -
and you don't need to define it in try block - it of course doesn't
complain if you do

The actual behaviour is that it simply returns null if the object cannot be
parsed for any reason

This is actually a nice behaviour - however it's not what the documentation
says - I would suggest the best way forward is to just correct the
documentation

Regards

Julian

Julian Rhind

googl...@googlecode.com

unread,
Jan 9, 2014, 5:28:36 PM1/9/14
to google-gson...@googlegroups.com

Comment #2 on issue 540 by yancheng...@gmail.com: fromJson returns null for
empty string
http://code.google.com/p/google-gson/issues/detail?id=540

Might be duplicated :
https://code.google.com/p/google-gson/issues/detail?id=457
Reply all
Reply to author
Forward
0 new messages