timetoshare.serialization#

The three serializations of a TimeToShare manifest: writing, reading, naming.

A manifest is a JSON value, exchanged as text in canonical YAML ("yaml"), pretty JSON ("json") or RFC 8785 JSON ("jcs"), each specified to the byte by the README’s “Serializations”. dumps() writes all three, loads() reads any text, and serialization_of() names the serialization of a text without validating it.

Every writer sorts the keys of every object by Unicode code point (RFC 8785 JSON by UTF-16 code units), writes a manifest in canonical order with its own-axis values respelled (canonical_order()), and refuses a value it cannot write: a NaN or an infinity, a string or key holding a lone surrogate, a key whose canonical YAML form is longer than 1024 UTF-16 code units, a value nested deeper than 256 levels, and in RFC 8785 JSON an integer beyond ±(2^53 − 1) or an integral float of magnitude from 2^53 up to but excluding 10^21.

The depth of a value is 0 for a scalar and, for an object or an array, 1 more than the greatest depth of its members (1 for an empty one). The readers refuse a text whose value would be deeper than 256, at the first character of the first collection at depth 257, so that every value written can be read and the place of the refusal depends on the text alone. A text holding several faults is refused for the first in document order, where its token stands; a YAML text is first searched for a character outside YAML’s printable set.

timetoshare.serialization.SERIALIZATIONS: tuple[Literal['yaml', 'json', 'jcs'], ...] = ('yaml', 'json', 'jcs')#

Every serialization’s name: canonical YAML, pretty JSON, RFC 8785 JSON.

timetoshare.serialization.Serialization#

The name of a serialization.

alias of Literal[‘yaml’, ‘json’, ‘jcs’]

timetoshare.serialization.dumps(value, serialization='yaml')[source]#

Return the text of a manifest, or of any JSON value, in one serialization.

"yaml" writes canonical YAML, "json" pretty JSON and "jcs" RFC 8785 JSON. A value that is an object whose timelines member is an array is a manifest: it is put in canonical order and its own-axis values respelled first (canonical_order()). Every other value keeps the order of its arrays and the kind of its numbers. The form, complete or minimal, is the one given.

Raises:
  • ValueError – If the serialization refuses the value (a value nested deeper than 256 levels, checked first, a NaN or an infinity, a string or key holding a lone surrogate, a key whose canonical YAML form is longer than 1024 UTF-16 code units, and in "jcs" an integer beyond ±(2^53 − 1) or an integral float of magnitude from 2^53 up to but excluding 10^21), if a manifest cannot be put in canonical order, or if serialization names no serialization.

  • TypeError – If the value is not JSON, whose kinds are dict with string keys, list, str, int, float, bool and None.

timetoshare.serialization.loads(text)[source]#

Read a text in any serialization, JSON or YAML.

One U+FEFF at position 0 is removed first. The text is read as JSON when its first character other than a space, a tab, a line feed or a carriage return is { or [, or when it is a JSON text; every other text is read as YAML. Integers stay exact at any size and floats stay floats. It does not validate.

Raises:
  • ValueError – With the line and column of the first fault in document order, both counted from one, the column in code points (in the text after a leading U+FEFF is removed), for a text the reading rules refuse: a JSON text that is not one JSON value, an error of the YAML parser, more than one YAML document, a character outside YAML’s printable set (found before any other fault), a key that repeats in its object, a key that is not a string, an anchor, an alias or a tag, a non-finite number (NaN, Infinity, .inf, .nan and a number that rounds to an infinity), a lone surrogate in a string or a key, written as itself or through an escape, and collections nested deeper than 256 levels, placed at the first collection at depth 257.

  • TypeError – If text is not a str.

timetoshare.serialization.serialization_of(text)[source]#

Name the serialization of a text without validating it; never raises.

"jcs" for a text read as JSON that is byte for byte the RFC 8785 JSON of its value; "json" for every other text read as JSON (one that cannot be read, one whose value the RFC 8785 writer refuses, and one that begins with a byte order mark included); "yaml" for every other text.

timetoshare.serialization.spell(value)[source]#

Spell a value as a message quotes it.

A finite number is spelled as canonical YAML and pretty JSON spell it, an array as its items separated by , `` within brackets (``[32, 1]), and anything else as compact JSON. The JavaScript package spells values the same way.