asphalt.serialization.serializers.cbor

class asphalt.serialization.serializers.cbor.CBORSerializer(encoder_options=None, decoder_options=None, custom_type_codec=None)

Bases: CustomizableSerializer

Serializes objects using CBOR (Concise Binary Object Representation).

To use this serializer backend, the cbor2 library must be installed. A convenient way to do this is to install asphalt-serialization with the cbor extra:

$ pip install asphalt-serialization[cbor]
Parameters:
  • encoder_options – keyword arguments passed to cbor2.dumps()

  • decoder_options – keyword arguments passed to cbor2.loads()

  • custom_type_codec – wrapper to use to wrap custom types after marshalling, or None to return marshalled objects as-is

deserialize(payload)

Deserialize bytes into a Python object.

Return type:

Any

property mimetype: str

Return the MIME type for this serialization format.

serialize(obj)

Serialize a Python object into bytes.

Return type:

bytes

class asphalt.serialization.serializers.cbor.CBORTypeCodec(type_tag=4554, **kwargs)

Bases: DefaultCustomTypeCodec[CBORSerializer]

Default custom type codec implementation for CBORSerializer.

Wraps marshalled state in either CBORTag objects (the default) or dicts (with type_tag=None).

Parameters:

type_tag (int | None) – CBOR tag number to use, or None to use JSON compatible dict-based wrapping

Note

Custom wrapping hooks are ignored when CBORTags are used.

register_object_decoder_hook(serializer)

Register a callback on the serializer for unmarshalling previously marshalled objects.

Parameters:

serializer (CBORSerializer) – the serializer instance to use

Return type:

None

register_object_encoder_hook(serializer)

Register a custom encoder callback on the serializer.

This callback would be called when the serializer encounters an object it cannot natively serialize. What the callback returns is specific to each serializer type.

Parameters:

serializer (CBORSerializer) – the serializer instance to use

Return type:

None