MessagePack

MessagePack is an excellent binary serialization format. It is very fast to serialize and deserialize, and it is space-efficient.

To get started, first import the package.

import msgpack

This library has a simple API, that nearly mirrors the API for json standard library for basic functionality.

To serialize a Python dict to a file, use the dump function.

# this code serializes a Python dictionary to a file
my_dict_to_serialize = {'a': 1, 'b': 2}
with open('my_new_serialized_data_file.msgpack', 'wb') as f:
    msgpack.dump(my_dict, f)

To deserialize a file, use the load function.

# this code deserializes the 'my_new_serialized_data_file.msgpack' file
with open('my_new_serialized_data_file.msgpack', 'rb') as f:
    my_deserialized_dict = msgpack.load(f)

To serialize a Python dict to a string, use the dumps function.

# this code serializes a Python dictionary to a string
my_dict_to_serialize = {'a': 1, 'b': 2}
my_serialized_string = msgpack.dumps(my_dict_to_serialize)

To deserialize a string, use the loads function.

# this code deserializes the 'my_serialized_string' string
my_deserialized_dict = msgpack.loads(my_serialized_string)

Resources

A set of documentation that describes how to use the msgpack Python package.

A detailed description of the MessagePack format.