Skip to content

JSON6 Superset of JavaScript #46

@brettz9

Description

@brettz9

If not allowing parsing which errs upon use of the JSON6 (or changing JSON6 to become a strict subset of JS), I would find it helpful to have documented the JSON6 features which are not allowable in JavaScript.

I changed some tests around so as to see whether the tests would unexpectedly pass or fail within JavaScript. Some details below on how I identified the missing support if of interest*.

I see the following divergences**:

  1. In JSON6, old-style octal support has not been entirely removed (from source or tests), so eval complains about this for strict mode.
  2. ES does not fail with .123e2-45, .123e3-45
  3. Not allowed in ES: Literal newline inside string, e.g.,{a:'abc\ndef'} (ok with backticks)
  4. Not allowed in ES: Back-tick quoted keys
  5. Not allowed in ES: Keys with special symbols (hyphens, backslashes, ampersand, plus, asterisk, pipe)
  6. Not allowed in ES: There aren't tests for it, but multiple minus signs
  7. Not present in ES: Console warnings with an unfinished // comment

My suggested lines of action (besides documenting any remaining differences):

  1. No. 1 appears to be a bug to be fixed (per the current README).
  2. No. 2 would be a good feature to add.
  3. No. 3, 4, 6, and 7 do not add much value, imo, to justify deviating from JS, esp. with backticks available
  4. No. 5 adds some value, but imho, it would be better to keep JS compatibility
  5. Restrict use of unescaped ${...} within backticks so that moving JSON6 to JS could not suddenly turn into variable substitution.

No. 3-5 could even be proposed to JS for them to be supported in a future ES version, though they might want to reserve the characters for potential use or deliberately err with those characters to prevent typos.


* I created an "drop-unused-stream-reviver" branch (not meant for merging) to find these (running npm run mocha-lite or nyc run nyc-lite). This branch:

  1. Switches the lite tests (i.e., tests not in the nested folders for benchmarking but which nevertheless have 100% coverage) slightly to use eval to confirm they work in JS (except in the case of a reviver argument (whose tests I've confirmed are only necessary as far as coverage in checking the reviver)
  2. Drops the stream and reviver tests and code so can check coverage related to eval. (I also have a no-stream-coverage branch which shows a few lines still uncovered by the non-stream tests and which I'm not 100% sure are stream related, but they seem to be)

**Note that there were also two tests which were expected to fail but would not fail in JS as they have a different meaning there and should understandably not be allowed:

  1. { a : 'no quote' [1] } (because of array index)
  2. .123-45 (because of being subtraction)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions