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
Classes
Provides a way to encode all objects of a given class or its |
|
Serializes objects to a string with a prefix. |
|
Serialises objects that can be built with keyword arguments. |
|
Organises the different serializer implementations under a unifiying |
|
Classes inheriting from this base are serialised using the |
Functions
|
|
|
|
|
|
|
Module Contents
- core.custom.custom_json.AnySerializer: TypeAlias = 'PrefixSerializer[_T] | DictionarySerializer[_T]'[source]
- class core.custom.custom_json.Serializer(target: type[_T])[source]
-
Provides a way to encode all objects of a given class or its subclasses to and from json.
- 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!
- 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
- 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]
- register(serializer: PrefixSerializer[Any] | DictionarySerializer[Any]) None [source]
- serializer_for_string(string: str) PrefixSerializer[Any] | None [source]
- serializer_for_dict(dictionary: dict[str, Any]) DictionarySerializer[Any] | None [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
- classmethod serializers() Serializers [source]