Repository navigation
emit \uXXXX escapes instead of \0 and \v in _escape_ch - #129
Conversation
JSON5 (like Javascript but unlike JSON) allows `\0` and `\v` as escape sequences in strings. The encoder used them for NUL and vertical tab, which made `dumps(quote_keys=True, trailing_commas=False)` (documented to match `json.dumps()`) and the `--as-json` flag produce output such as `"\0"` and `"\v"` that the json module and other strict JSON parsers reject with "Invalid \escape". Let those two characters fall through to the generic `\uXXXX` form instead, so they are written as `\u0000` and `\u000b`, which is what the json module writes and is equally valid JSON5. The NUL-before-digit special case in `_encode_quoted_str` is no longer needed since `\u0000` cannot be mistaken for an octal escape, so it is removed.
|
Okay, having looked at this now ... Given that the docs explicitly say that However, it's perhaps a bit unfortunately if you actually like \0 and \v and were fine with getting them, since now you can't. What this suggests to me is that there should really be an But that's a patch for another day. This looks fine. |
|
Also, thanks for the full extended description :). |

_escape_chwrites NUL and vertical tab as\0and\v. Those escapes exist in JSON5 (via ECMAScript) but not in JSON, where the only single-character escapes are\",\\,\/,\b,\f,\n,\rand\t.dumps(quote_keys=True, trailing_commas=False)is documented to produce the same output asjson.dumps(), and--as-jsonin the CLI uses those settings, but for any string containing U+0000 or U+000B the result is"\0"or"\v", whichjson.loads()and other strict JSON parsers reject withInvalid \escape. I noticed it while comparingdumps()against the json module for every BMP code point; these two were the only characters whose output could not be read back as JSON. Letting them fall through to the generic\uXXXXform gives the same six-character\uXXXXescapes for NUL and VT (\u000bfor VT), which is what the json module writes and is equally valid JSON5, soloads()round-trips are unchanged. That also makes the NUL-before-digit special case in_encode_quoted_strfrom #121 unnecessary, since a\uXXXXescape for NUL is never ambiguous, so it is removed.