core.custom.custom_json

Sigh. Here we go again, another json implementation with support for:

  • date

  • datetime

  • time

Because nobody else does all of these. And if they do (like standardjson), they don’t support decoding…

Attributes

AnySerializer

_T

_ST

default_serializers

Classes

Serializer

Provides a way to encode all objects of a given class or its

PrefixSerializer

Serializes objects to a string with a prefix.

DictionarySerializer

Serialises objects that can be built with keyword arguments.

Serializers

Organises the different serializer implementations under a unifiying

Serializable

Classes inheriting from this base are serialised using the

Functions

dumps(…)

loads(→ Any)

dump(→ None)

load(→ Any)

Module Contents

core.custom.custom_json.AnySerializer: TypeAlias = 'PrefixSerializer[_T] | DictionarySerializer[_T]'[source]
core.custom.custom_json._T[source]
core.custom.custom_json._ST[source]
class core.custom.custom_json.Serializer(target: type[_T])[source]

Bases: Generic[_T, _ST]

Provides a way to encode all objects of a given class or its subclasses to and from json.

target[source]
abstract encode(obj: _T) _ST[source]
abstract decode(value: _ST) _T[source]
class core.custom.custom_json.PrefixSerializer(target: type[_T], prefix: str, encode: Callable[[_T], str], decode: Callable[[str], _T])[source]

Bases: Serializer[_T, str]

Serializes objects to a string with a prefix.

Resulting json values take the form of __prefix__@<value>, where <value> is the encoded value and __prefix__@ is the prefix that is used to differentiate between normal strings and encoded strings.

Note that the part after the prefix is user-supplied and possibly unsafe. So something like an ‘eval’ should be out of the question!

prefix_format = '__{}__@{}'[source]
prefix_expression[source]
prefix_characters[source]
prefix[source]
prefix_length = 0[source]
_encode[source]
_decode[source]
encode(obj: _T) str[source]
decode(string: str) _T[source]
class core.custom.custom_json.DictionarySerializer(target: type[_T], keys: Iterable[str])[source]

Bases: Serializer[_T, onegov.core.types.JSONObject_ro]

Serialises objects that can be built with keyword arguments.

For example:

class Point:

    def __init__(self, x, y):
        self.x = x
        self.y = y

Can be serialised using:

DictionarySerializer(Point, ('x', 'y'))

Which results in something like this in JSON:

{'x': 1, 'y': 2}

As the internal __dict__ represenation is of no concern, __slots__ may be used:

class Point:

__slots__ = (‘x’, ‘y’)

def __init__(self, x, y):

self.x = x self.y = y

keys[source]
encode(obj: _T) onegov.core.types.JSONObject_ro[source]
decode(dictionary: onegov.core.types.JSONObject_ro) _T[source]
class core.custom.custom_json.Serializers[source]

Organises the different serializer implementations under a unifiying interface. This allows the actual encoder/decoder to call a single class without having to worry how the various serializers need to be looked up and called.

by_prefix: dict[str, PrefixSerializer[Any]][source]
by_keys: dict[frozenset[str], DictionarySerializer[Any]][source]
known_key_lengths: set[int][source]
property registered: Iterator[AnySerializer[Any]][source]
register(serializer: PrefixSerializer[Any] | DictionarySerializer[Any]) None[source]
serializer_for(value: object) AnySerializer[Any] | None[source]
serializer_for_string(string: str) PrefixSerializer[Any] | None[source]
serializer_for_dict(dictionary: dict[str, Any]) DictionarySerializer[Any] | None[source]
serializer_for_class(cls: type[_T]) AnySerializer[_T] | None[source]
encode(value: object) onegov.core.types.JSON_ro[source]
decode(value: Any) Any[source]
core.custom.custom_json.default_serializers[source]
class core.custom.custom_json.Serializable[source]

Classes inheriting from this base are serialised using the DictionarySerializer class.

The keys that should be used need to be specified as follows:

class Point(Serializable, keys=('x', 'y')):

    def __init__(self, x, y):
        self.x = x
        self.y = y
serialized_keys: ClassVar[Collection[str]][source]
classmethod serializers() Serializers[source]
classmethod __init_subclass__(keys: Collection[str], **kwargs: Any)[source]
core.custom.custom_json.dumps(obj: None, **extra: Any) None[source]
core.custom.custom_json.dumps(obj: Any, **extra: Any) str
core.custom.custom_json.loads(txt: str | bytes | bytearray | None, **extra: Any) Any[source]
core.custom.custom_json.dump(data: Any, fp: SupportsWrite[str], **extra: Any) None[source]
core.custom.custom_json.load(fp: SupportsRead[str | bytes], **extra: Any) Any[source]