-
Notifications
You must be signed in to change notification settings - Fork 11
Description
Proposal
Even though JSON RFC 8259 states that "names within an object SHOULD be unique", it leaves duplicate object name behavior undefined. The RFC warns that only objects with unique names are guaranteed interoperability since "all software implementations receiving that object will agree on the name-value mappings", but it does not prohibit duplicate names.
I propose JSON5 explicitly defines object name duplicate behavior while guaranteeing full compatibility with JSON. Explicitly defining behavior increases system interoperability, removes the potential for bugs, and provides less surprises to users.
JSON5 should define the following four object name duplicate behaviors:
unique
last-value-wins
duplicate
undefined
unique
requires JSON5 implementations to fail on duplicate object names.
last-value-wins
requires JSON5 implementations to deduplicate and only report the last name/value pair.
duplicate
requires JSON5 implementations to permit duplicates and preserves name/value pairs.
undefined
permits JSON5 implementations to handle duplicate behavior in any way.
Further, JSON5 should
- Require all JSON5 implementations MUST support
unique
. - Suggest implementations and applications SHOULD use
unique
. - Suggest implementations and applications SHOULD note duplicate behavior through documentation or other means.
- Leave the method of behavior selection to implementations. For example, behavior selection may be implemented by a flag, global variable, or be implicit.
This proposal makes the distinction between applications and implementations. This proposal suggests that JSON5 implementations MUST define their behaviors and MUST support unique
, but applications may behave however they like. The behavior of particular applications may be noted in documentation, conveyed by API's, or simply not documented at all. This proposal is not suggesting that particular applications or API's must support unique
behavior. Also, this proposal is not suggesting standardizing a method for application behavior selection.
Related thoughts
Many JSON authority figures have expressed their desire for unique names. It's reasonable for JSON5, which makes minor improvements to JSON, to take this opportunity to implement this hope.
The behavior of Crockford's Java JSON implementation is to error on duplicate object names. Although not JSON5, Crockford's implementation already complies to this proposal since it supports unique
.
Also, Crockford suggested modifying the JSON RFC to require unique object names, although it was decided it was too late to do so:
The names within an object SHOULD be unique. If a key is duplicated, a parser SHOULD reject. If it does not reject, it MUST take only the last of the duplicated key pairs.
Disallowing duplicates conforms to the small I-JSON RFC. The author of I-JSON, Tim Bray, is also the author of JSON RFC 8259
There's also security problems and interoperability problems with duplicates. See the article, "An Exploration of JSON Interoperability Vulnerabilities"