FranzGames / google-gson

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

Fail if a JSON object has a duplicated property name #386

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Instantiate a JSONReader(Reader in) where the specified input stream 
includes an object where a given object in the structure has a repeated 
property name.
  - This can be done directly, or indirectly via, say, Gson.fromJson()
2. Examine the object as parsed from the JSON.

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

The observed result is that of the multiple specifications of the value for a 
given property name, only the last actually ends up applying.

Per http://www.ietf.org/rfc/rfc4627.txt, there's some implementation 
flexibility on what the expected result should be:

2.2.  Objects

   An object structure is represented as a pair of curly brackets
   surrounding zero or more name/value pairs (or members).  A name is a
   string.  A single colon comes after each name, separating the name
   from the value.  A single comma separates a value from a following
   name.  The names within an object SHOULD be unique.

--

So the implementation doesn't seem to be _incorrect_ with respect to the RFC in 
how it handles this situation, which SHOULD not arise, when it happens. 
However, it would be nice if an issue with repeated names in the object caused 
an exception to be thrown or otherwise brought the issue to the user's 
attention. At least optionally. Silently letting the latest-specified version 
of a name win is a bit surprising.

The issue is particularly acute if the JSON data comes from an outside source 
not directly under the programmer's control.

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

v2_0_RC1, Linux

Original issue reported on code.google.com by hil...@google.com on 6 Dec 2011 at 4:55

GoogleCodeExporter commented 9 years ago
This is a great idea but it comes at a high cost in performance. We'd need to 
track all the names seen at all nesting levels.

Original comment by limpbizkit on 29 Dec 2011 at 5:32