Redis Dict Type Management
Type management module.
- class redis_dict.type_management.RedisDictJSONDecoder(*args, **kwargs)[source]
Bases:
JSONDecoderJSON decoder leveraging RedisDict existing type conversion system.
Works with RedisDictJSONEncoder to reconstruct Python objects from JSON using RedisDict decoding_registry.
Still needs work but allows for more types than without.
- __init__(*args, **kwargs)[source]
Overwrite the __init__ method from JSON decoder.
- Parameters:
*args (
Any) – Positional arguments for initialization.**kwargs (
Any) – Keyword arguments for initialization.
- class redis_dict.type_management.RedisDictJSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]
Bases:
JSONEncoderExtends JSON encoding capabilities by reusing RedisDict type conversion.
Uses existing decoding_registry to know which types to handle specially and encoding_registry (falls back to str) for converting to JSON-compatible formats.
Example
The encoded format looks like:
{ "__type__": "TypeName", "value": <encoded value> }
Notes
Uses decoding_registry (containing all supported types) to check if type needs special handling. For encoding, defaults to str() if no encoder exists in encoding_registry.
- default(o)[source]
Overwrite default from json encoder.
- Parameters:
o (
Any) – Object to be serialized.- Raises:
TypeError – If the object o cannot be serialized.
- Returns:
Serialized value.
- Return type:
Any
- iterencode(o, _one_shot=False)[source]
Overwrite iterencode from json encoder, to tag tuples, sets and dicts JSON can’t represent.
- Parameters:
o (
Any) – Object to be serialized._one_shot (
bool) – Passed on to json.JSONEncoder.iterencode.
- Returns:
The encoded JSON chunks.
- Return type:
Iterator[str]
- redis_dict.type_management.decode_json(s)[source]
Decode a JSON string to a Python object using the existing decoding registry.
- Parameters:
s (
str) – The JSON string to be decoded.- Returns:
The decoded Python object.
- Return type:
Any
- redis_dict.type_management.encode_json(obj)[source]
Encode a Python object to a JSON string using the existing encoding registry.
- Parameters:
obj (
Any) – The Python object to be encoded.- Returns:
The JSON-encoded string representation of the object.
- Return type:
str