Overview
Serialization plays an important role in the KVDB
library. It allows you to store almost any type of object when using any set/get
methods without having to worry about serializing and deserializing prior.
The kvdb
library provides several default serializers and compression algorithms to choose from, and provides extensibility to add custom serializers and compression algorithms.
Default Supported Serializers:
A session can be configured to use a serializer when serializing and deserializing data in-flight. This allows you to store almost any type of object when using any set/get
methods without having to worry about serializing and deserializing prior.
Even with json
, almost all data types and objects can be serialized by utilizing an intelligent deterministic storing of the object's metadata on serialization and then reconstructing the object on deserialization. This is done by storing the object's type and any additional metadata required to reconstruct the object.
Serialization has first-class support for pydantic
models and dataclasses
.
This can be done by passing a serializer to the get_session()
method.
It is currently not recommended to mix serialization with sub-dependent libraries that may do encoding and decoding prior to passing the data to KV Store, as it can lead to serialization errors.
The following is a list of the arguments that can be passed to the get_session()
method:
-
serializer
: The serializer to use when serializing and deserializing data. Defaults toNone
. Set toauto
, which defaults tojson
to automatically use a serializer based on the data type. Each type supports sub-libraries which will be used if they are installed. Additionally, you can pass a custom kwarg to try to import the serializer. Supported serializers (and sub-libraries based on priority):json
: The JSON serializer. Kwarg:jsonlib
- [x]
simdjson
- [x]
orjson
- [x]
ujson
- [x]
json
msgpack
: The MessagePack serializer.pickle
: The pickle serializer. Kwarg:picklelib
- [x]
cloudpickle
- [x]
dill
- [x]
pickle
- [x]
-
serializer_kwargs
: The keyword arguments to pass to the serializer when serializing and deserializing data. Defaults toNone
. compression
: The compression algorithm to use when compressing and decompressing data. Defaults toNone
. Supported compression algorithms:zstd
lz4
gzip
zlib
compression_level
: The compression level to use when compressing data. Defaults toNone
, which will use the default compression level for the compression algorithm.compression_enabled
: Whether or not compression should be enabled. Defaults toNone
. IfTrue
andcompression
isNone
, then it will be determined based on which compression algorithms are available. IfFalse
, then compression will be disabled.
Serialization Classes
The following is the base class that serializers from:
Bases: abc.ABC
The Base Serializer Class
Initializes the serializer
METHOD | DESCRIPTION |
---|---|
adecode |
Decodes the value asynchronously |
adumps |
Dumps the value asynchronously |
aencode |
Encodes the value asynchronously |
aloads |
Loads the value asynchronously |
compress_value |
Compresses the value |
copy |
Copies the serializer |
create_hash |
Creates a hash for the object |
decode |
Decodes the value |
decode_value |
Decodes the value |
decompress_value |
Decompresses the value |
deprecated_decompress_value |
Attempts to decompress the value using the deprecated compressor |
dumps |
Dumps the value |
encode |
Encodes the value |
encode_value |
Encodes the value |
fetch_object_classname |
Fetches the object classname |
loads |
Loads the value |
set_default_lib |
Sets the default library |
ATTRIBUTE | DESCRIPTION |
---|---|
compression_enabled |
Returns if compression is enabled
TYPE:
|
compression_level |
Returns the compression level
TYPE:
|
Source code in kvdb/io/serializers/base.py
adecode
async
adumps
async
aencode
async
aloads
async
compress_value
Compresses the value
Source code in kvdb/io/serializers/base.py
copy
copy(**kwargs) -> kvdb.io.serializers.base.BaseSerializer
Copies the serializer
Source code in kvdb/io/serializers/base.py
create_hash
decode
Decodes the value
Source code in kvdb/io/serializers/base.py
decode_value
decompress_value
Decompresses the value
Source code in kvdb/io/serializers/base.py
deprecated_decompress_value
deprecated_decompress_value(
value: typing.Union[str, bytes], **kwargs
) -> typing.Optional[typing.Union[str, bytes]]
Attempts to decompress the value using the deprecated compressor
Source code in kvdb/io/serializers/base.py
dumps
Dumps the value
Source code in kvdb/io/serializers/base.py
encode
encode_value
fetch_object_classname
loads
Loads the value