Transcoders and serializers
couchbase.transcoder
- class couchbase.transcoder.JSONTranscoder(serializer: Serializer | None = None)
The default transcoder, which stores every value as JSON.
It encodes a str, list, tuple, dict, bool, int, float or None, and refuses bytes. It decodes a document flagged as JSON, or with no flags, and returns the raw bytes if that is not valid JSON. It refuses a document flagged as binary or as a string.
- Parameters:
serializer (
Serializer, optional) – The serializer that does the JSON encoding and decoding. Defaults toDefaultJsonSerializer.
- class couchbase.transcoder.LegacyTranscoder
A transcoder that picks the format from the value’s type.
It stores a str as a string, bytes as binary, a list, tuple, dict, bool, int, float or None as JSON, and pickles anything else. Decoding follows the flags, and returns the raw bytes for a JSON document that does not parse and for a format it does not know.
Decoding unpickles a document flagged as pickled, which can run arbitrary code, so read with it only documents written by code you trust.
- class couchbase.transcoder.RawBinaryTranscoder
A transcoder for binary data.
It stores bytes or a bytearray flagged as binary, and returns a document so flagged as bytes. It refuses any other value, and a document flagged as a string or as JSON.
- decode_value(value: bytes, flags: int | None) bytes
Decode stored bytes into a value.
- Parameters:
- Returns:
The decoded value.
- Return type:
Any
- Raises:
ValueFormatException – If the transcoder cannot decode the format the flags record.
- class couchbase.transcoder.RawJSONTranscoder
A transcoder for JSON that is already encoded.
It stores a str as UTF-8 and bytes as given, both flagged as JSON, and returns a JSON document’s bytes undecoded. It refuses any other value, and a document flagged as binary or as a string.
- decode_value(value: bytes, flags: int | None) str | bytes
Decode stored bytes into a value.
- Parameters:
- Returns:
The decoded value.
- Return type:
Any
- Raises:
ValueFormatException – If the transcoder cannot decode the format the flags record.
- class couchbase.transcoder.RawStringTranscoder
A transcoder for plain strings.
It stores a str as UTF-8 flagged as a string, and decodes a document so flagged back to a str. It refuses any other value, and a document flagged as binary or as JSON.
- class couchbase.transcoder.Transcoder
The interface a custom transcoder implements.
A transcoder turns a value into the bytes a document stores and the flags that record their format, and turns stored bytes and flags back into a value.
- abstract decode_value(value: bytes, flags: int | None) Any
Decode stored bytes into a value.
- Parameters:
- Returns:
The decoded value.
- Return type:
Any
- Raises:
ValueFormatException – If the transcoder cannot decode the format the flags record.
- abstract encode_value(value: _V_contra) tuple[bytes, int]
Encode a value for storage.
- Parameters:
value (Any) – The value to store.
- Returns:
The bytes to store and the flags recording their format.
- Return type:
- Raises:
ValueFormatException – If the transcoder cannot encode the value.
couchbase.serializer
- class couchbase.serializer.DefaultJsonSerializer
The serializer the SDK uses when none is given, built on the standard
jsonmodule.Warning
couchbase.options.DefaultJsonSerializerandcouchbase.transcoder.DefaultJsonSerializerare deprecated, and will be removed in a future release. ImportDefaultJsonSerializerfromcouchbase.serializer.
- class couchbase.serializer.Serializer
The interface a custom JSON serializer implements.
JSONTranscoderuses one to turn values into JSON bytes and back.