Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fromJson returns null for empty string #540

Closed
GoogleCodeExporter opened this issue Mar 19, 2015 · 4 comments
Closed

fromJson returns null for empty string #540

GoogleCodeExporter opened this issue Mar 19, 2015 · 4 comments

Comments

@GoogleCodeExporter
Copy link

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.

Original issue reported on code.google.com by da...@swiftkey.com on 30 Oct 2013 at 4:39

@GoogleCodeExporter
Copy link
Author

[deleted comment]

@GoogleCodeExporter
Copy link
Author

Might be duplicated : https://code.google.com/p/google-gson/issues/detail?id=457

Original comment by yancheng...@gmail.com on 9 Jan 2014 at 10:28

@darktiny
Copy link

Is there a plan to fix this issue?

@JakeWharton
Copy link
Contributor

No. This is a side-effect of historical and bad behavior of the Gson object setting leniency by default. Changing this would be backwards-incompatible.

You can look up a TypeAdapter and use it directly to get Gson to behave sanely.

Gson gson = new Gson();
TypeAdapter<String> adapter = gson.getAdapter(String.class);
try {
  String json = "";
  String result = adapter.fromJson(json);
  System.out.println("SUCCEEDED " + result);
} catch (IOException e) {
  System.out.println("FAILED " + e.getClass().getSimpleName() + ": " + e.getMessage());
}
FAILED EOFException: End of input at line 1 column 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants