Skip to content

Add an option to throw on duplicate object keys #48

@HeteropodaVenatoria

Description

@HeteropodaVenatoria

Why this feature is better to be included in comment-json? Please describe the scenario
In most libraries, duplicate JSON object keys are generally not considered as an instant exception during parsing, and the later value just silently overwrite the former value with the same key. comment-json does the same thing, however this common practice causes surprising behavior here because object key also links to comments, resulting to inconsistent handling of duplicate keys:

const dup = `{
  "a": 0,  // comment on first a
  "b": 1, // some other content
  "a": 2
}`
stringify(parse(dup), null, 2)
// {
//   "a": 2, // comment on first a
//   "b": 1 // some other content
// }

So it might be necessary to treat duplicate keys as a SytaxError here for the ambiguity (which is not possible for compatibility) or add an allowDuplicateKey option in parse so the user could choose to get a warning on problematic JSON data, since it's really hard to detect duplicate keys outside of the parsing cycle.

Are you willing to submit a pull request to implement this rule?
Yeah I'm very happy to implement this along side its docs, tests, .etc

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions