Skip to content

parsers

deserialize_json_column(value)

Deserializes JSON text of a database column with orjson.

Text orjson rejects (NaN, lone surrogates) falls back to the standard library json module.

Parameters:

Name Type Description Default
value str

JSON text read from the database

required

Returns:

Type Description
Any

deserialized value

Source code in ormar/fields/parsers.py
Python
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def deserialize_json_column(value: str) -> Any:
    """
    Deserializes JSON text of a database column with orjson.

    Text orjson rejects (``NaN``, lone surrogates) falls back to the standard
    library json module.

    :param value: JSON text read from the database
    :type value: str
    :return: deserialized value
    :rtype: Any
    """
    try:
        return json.loads(value)
    except json.JSONDecodeError:
        return std_json.loads(value)

serialize_json_column(value)

Serializes a value of a JSON column for the database with orjson.

Values orjson cannot serialize (integers over 64 bits) fall back to the standard library json module.

Parameters:

Name Type Description Default
value Any

value of the JSON column

required

Returns:

Type Description
str

JSON text of the value

Source code in ormar/fields/parsers.py
Python
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def serialize_json_column(value: Any) -> str:
    """
    Serializes a value of a JSON column for the database with orjson.

    Values orjson cannot serialize (integers over 64 bits) fall back to the
    standard library json module.

    :param value: value of the JSON column
    :type value: Any
    :return: JSON text of the value
    :rtype: str
    """
    try:
        return json.dumps(value, option=json.OPT_NON_STR_KEYS).decode()
    except TypeError:
        return std_json.dumps(value)