MessagePack Codec

Codec for serialising and de-serialising data in MessagePack format. Supports both array and objects for mapping into resources or collections of resources.

The MessagePack codec uses the msgpack-python module.

Methods

odin.codecs.msgpack_codec.load(fp, resource=None, encoding='UTF8', full_clean=True, default_to_not_supplied=False)[source]

Load a from a MessagePack encoded file.

See loads() for more details of the loading operation.

Parameters:
  • fp – a file pointer to read MessagePack data from.
  • resource – A resource instance or a resource name to use as the base for creating a resource.
  • encoding – Encoding to use when loading file
  • full_clean – Do a full clean of the object as part of the loading process.
Returns:

A resource object or object graph of resources loaded from file.

odin.codecs.msgpack_codec.loads(s, resource=None, encoding='UTF8', full_clean=True, default_to_not_supplied=False)[source]

Load from a MessagePack encoded string/bytes.

If a resource value is supplied it is used as the base resource for the supplied MessagePack data. I one is not supplied a resource type field $ is used to obtain the type represented by the dictionary. A ValidationError will be raised if either of these values are supplied and not compatible. It is valid for a type to be supplied in the file to be a child object from within the inheritance tree.

Parameters:
  • s – String to load and parse.
  • resource – A resource instance or a resource name to use as the base for creating a resource.
  • full_clean – Do a full clean of the object as part of the loading process.
  • encoding – Encoding to use when loading file
Returns:

A resource object or object graph of resources parsed from supplied string.

odin.codecs.msgpack_codec.dump(resource, fp, cls=<class 'odin.codecs.msgpack_codec.OdinPacker'>, include_virtual_fields=True, **kwargs)[source]

Dump to a MessagePack encoded file.

Parameters:
  • resource – The root resource to dump to a MessagePack encoded file.
  • cls – Encoder to use serializing to a string; default is the OdinEncoder.
  • fp – The file pointer that represents the output file.
odin.codecs.msgpack_codec.dumps(resource, cls=<class 'odin.codecs.msgpack_codec.OdinPacker'>, include_virtual_fields=True, **kwargs)[source]

Dump to a MessagePack encoded string.

Parameters:
  • resource – The root resource to dump to a MessagePack encoded file.
  • cls – Encoder to use serializing to a string; default is the OdinEncoder.
Returns:

MessagePack encoded string.

Customising Encoding

Serialisation of Odin resources is handled by a customised msgpack.Packer. Additional data types can be appended to the odin.codecs.msgpack_codec.TYPE_SERIALIZERS dictionary.

Example usage

Loading a resource from a file:

from odin.codecs import msgpack_codec

with open('my_resource.msgp') as f:
    resource = msgpack_codec.load(f)