io.omk_json.canonical

canonical

The canonical text of a JSON value, and a strict parser, as the format specification’s §9 and §4.1–4.9 define them.

dumps is written from the specification rather than borrowed from json.dumps, which differs in two places that byte-identical round trips depend on: it writes floats as Python’s repr does (1e+16, where ECMAScript writes 10000000000000000), and it accepts NaN and lone surrogates. loads is json.loads with the checks the specification adds: no duplicate keys, no NaN or Infinity, no lone surrogates.

>>> print(dumps({"b": [1, "2/3"], "@id": None, "a": {}}), end="")
{
  "@id": null,
  "a": {},
  "b": [
    1,
    "2/3"
  ]
}
>>> loads('{"a": 1, "a": 2}')
Traceback (most recent call last):
...
openmusickit.errors.OmkJsonError: Duplicate key 'a' in an object.

Functions

Name Description
dumps The canonical text of a JSON value (dicts with string keys, lists,
format_string A string as RFC 8785 writes it: only ", \ and control characters escaped.
format_float A finite float as ECMAScript’s Number.prototype.toString writes it:
loads Parses JSON text, refusing what the specification refuses: