io.omk_json.primitives

primitives

The primitive types of the format specification (§4): each as a pair of functions, one writing a Python value as JSON data and one reading it back.

A reader is strict: a value in any form but the canonical one ("2/4" for a rational, an uppercase UUID, an enumeration’s display value) is outside the specification and is refused with OmkJsonError.

>>> from fractions import Fraction
>>> encode_rational(Fraction(-7, 3)), decode_rational("-7/3")
('-7/3', Fraction(-7, 3))
>>> encode_rational(Fraction(4, 2)), decode_rational(2)
(2, 2)

Functions

Name Description
encode_rational A rational as the specification writes it: an int if whole, otherwise "n/d".
decode_rational An int for a whole number, otherwise a Fraction.
decode_integer A JSON integer. (A boolean is not one, although Python’s bool is an int.)
decode_string A JSON string.
decode_boolean A JSON boolean.
encode_uuid A UUID in the hyphenated, lowercase form.
decode_uuid A UUID from its hyphenated, lowercase form; any version.
encode_enum An enumeration value: the member’s name in lowercase (§4.5),
decode_enum The member of enum whose lowercase name is data.