DonaldDu / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

Trailing slash in array caused null value in output #407

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
ArrayList<Integer> list = gson.fromJson("[1,2,3,]", new 
TypeToken<ArrayList<Integer>>() {}.getType());
either: fail("Expected trailing slash to give error");
or if super-lenient: assertEquals(3, list.size());

What is the expected output? What do you see instead?
Should fail according to the syntax diagram on http://json.org, or give an 
ArrayList<Integer> with three elements [1, 2, 3] if we want to emulate 
JavaScript's lenient behaviour.

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

Original issue reported on code.google.com by martinpr...@google.com on 8 Feb 2012 at 2:12

GoogleCodeExporter commented 9 years ago
By the way, the actual behaviour (how could I miss that...) is to return the 
list [1, 2, 3, null], where the null is the obvious problem.

Original comment by martinpr...@google.com on 8 Feb 2012 at 6:56

GoogleCodeExporter commented 9 years ago
Gson is emulating the original implementation of org.json; it might be the 
wrong choice but I'd prefer not to change it for backwards compatibility.

If you'd prefer for it to crash, use TypeAdapter rather than fromJson():
  TypeAdapter<List<Integer>> typeAdapter = gson.getAdapter(new TypeToken<List<Integer>>() {});
  List<Integer> list = typeAdapter.read("[1,2,3,]");

This will throw. The new TypeAdapter APIs are strict about ',' whereas 
Gson.fromJson is lenient.

Original comment by limpbizkit on 9 Feb 2012 at 12:59