cbor2.decoder
- class cbor2.decoder.CBORDecoder(fp, tag_hook=None, object_hook=None, str_errors='strict')
The CBORDecoder class implements a fully featured CBOR decoder with several extensions for handling shared references, big integers, rational numbers and so on. Typically the class is not used directly, but the
load()
andloads()
functions are called to indirectly construct and use the class.When the class is constructed manually, the main entry points are
decode()
anddecode_from_bytes()
.- Parameters:
tag_hook – callable that takes 2 arguments: the decoder instance, and the
CBORTag
to be decoded. This callback is invoked for any tags for which there is no built-in decoder. The return value is substituted for theCBORTag
object in the deserialized outputobject_hook – callable that takes 2 arguments: the decoder instance, and a dictionary. This callback is invoked for each deserialized
dict
object. The return value is substituted for the dict in the deserialized output.
- decode()
Decode the next value from the stream.
- Raises:
CBORDecodeError – if there is any problem decoding the stream
- decode_from_bytes(buf)
Wrap the given bytestring as a file and call
decode()
with it as the argument.This method was intended to be used from the
tag_hook
hook when an object needs to be decoded separately from the rest but while still taking advantage of the shared value registry.
- property immutable
Used by decoders to check if the calling context requires an immutable type. Object_hook or tag_hook should raise an exception if this flag is set unless the result can be safely used as a dict key.
- read(amount)
Read bytes from the data stream.
- Parameters:
amount (int) – the number of bytes to read
Set the shareable value for the last encountered shared value marker, if any. If the current shared index is
None
, nothing is done.- Parameters:
value – the shared value
- Returns:
the shared value to permit chaining
- cbor2.decoder.load(fp, **kwargs)
Deserialize an object from an open file.
- Parameters:
fp – the input file (any file-like object)
kwargs – keyword arguments passed to
CBORDecoder
- Returns:
the deserialized object
- cbor2.decoder.loads(s, **kwargs)
Deserialize an object from a bytestring.
- Parameters:
s (bytes) – the bytestring to deserialize
kwargs – keyword arguments passed to
CBORDecoder
- Returns:
the deserialized object