Skip to main content

MessagePack (de)serializer.

Project description

Build Status Documentation Status

What’s this

MessagePack is an efficient binary serialization format. It lets you exchange data among multiple languages like JSON. But it’s faster and smaller. This package provides CPython bindings for reading and writing MessagePack data.

Very important notes for existing users

PyPI package name

TL;DR: When upgrading from msgpack-0.4 or earlier, don’t do pip install -U msgpack-python. Do pip uninstall msgpack-python; pip install msgpack instead.

Package name on PyPI was changed to msgpack from 0.5. I upload transitional package (msgpack-python 0.5 which depending on msgpack) for smooth transition from msgpack-python to msgpack.

Sadly, this doesn’t work for upgrade install. After pip install -U msgpack-python, msgpack is removed and import msgpack fail.

Deprecating encoding option

encoding and unicode_errors options are deprecated.

In case of packer, use UTF-8 always. Storing other than UTF-8 is not recommended.

For backward compatibility, you can use use_bin_type=False and pack bytes object into msgpack raw type.

In case of unpacker, there is new raw option. It is True by default for backward compatibility, but it is changed to False in near future. You can use raw=False instead of encoding='utf-8'.

Planned backward incompatible changes

When msgpack 1.0, I planning these breaking changes:

  • packer and unpacker: Remove encoding and unicode_errors option.

  • packer: Change default of use_bin_type option from False to True.

  • unpacker: Change default of raw option from True to False.

  • unpacker: Reduce all max_xxx_len options for typical usage.

  • unpacker: Remove write_bytes option from all methods.

To avoid these breaking changes breaks your application, please:

  • Don’t use deprecated options.

  • Pass use_bin_type and raw options explicitly.

  • If your application handle large (>1MB) data, specify max_xxx_len options too.

Install

$ pip install msgpack

PyPy

msgpack provides a pure Python implementation. PyPy can use this.

Windows

When you can’t use a binary distribution, you need to install Visual Studio or Windows SDK on Windows. Without extension, using pure Python implementation on CPython runs slowly.

For Python 2.7, Microsoft Visual C++ Compiler for Python 2.7 is recommended solution.

For Python 3.5, Microsoft Visual Studio 2015 Community Edition or Express Edition can be used to build extension module.

How to use

One-shot pack & unpack

Use packb for packing and unpackb for unpacking. msgpack provides dumps and loads as an alias for compatibility with json and pickle.

pack and dump packs to a file-like object. unpack and load unpacks from a file-like object.

>>> import msgpack
>>> msgpack.packb([1, 2, 3], use_bin_type=True)
'\x93\x01\x02\x03'
>>> msgpack.unpackb(_, raw=False)
[1, 2, 3]

unpack unpacks msgpack’s array to Python’s list, but can also unpack to tuple:

>>> msgpack.unpackb(b'\x93\x01\x02\x03', use_list=False, raw=False)
(1, 2, 3)

You should always specify the use_list keyword argument for backward compatibility. See performance issues relating to use_list option below.

Read the docstring for other options.

Streaming unpacking

Unpacker is a “streaming unpacker”. It unpacks multiple objects from one stream (or from bytes provided through its feed method).

import msgpack
from io import BytesIO

buf = BytesIO()
for i in range(100):
   buf.write(msgpack.packb(range(i), use_bin_type=True))

buf.seek(0)

unpacker = msgpack.Unpacker(buf, raw=False)
for unpacked in unpacker:
    print(unpacked)

Packing/unpacking of custom data type

It is also possible to pack/unpack custom data types. Here is an example for datetime.datetime.

import datetime
import msgpack

useful_dict = {
    "id": 1,
    "created": datetime.datetime.now(),
}

def decode_datetime(obj):
    if b'__datetime__' in obj:
        obj = datetime.datetime.strptime(obj["as_str"], "%Y%m%dT%H:%M:%S.%f")
    return obj

def encode_datetime(obj):
    if isinstance(obj, datetime.datetime):
        return {'__datetime__': True, 'as_str': obj.strftime("%Y%m%dT%H:%M:%S.%f")}
    return obj


packed_dict = msgpack.packb(useful_dict, default=encode_datetime, use_bin_type=True)
this_dict_again = msgpack.unpackb(packed_dict, object_hook=decode_datetime, raw=False)

Unpacker’s object_hook callback receives a dict; the object_pairs_hook callback may instead be used to receive a list of key-value pairs.

Extended types

It is also possible to pack/unpack custom data types using the ext type.

>>> import msgpack
>>> import array
>>> def default(obj):
...     if isinstance(obj, array.array) and obj.typecode == 'd':
...         return msgpack.ExtType(42, obj.tostring())
...     raise TypeError("Unknown type: %r" % (obj,))
...
>>> def ext_hook(code, data):
...     if code == 42:
...         a = array.array('d')
...         a.fromstring(data)
...         return a
...     return ExtType(code, data)
...
>>> data = array.array('d', [1.2, 3.4])
>>> packed = msgpack.packb(data, default=default, use_bin_type=True)
>>> unpacked = msgpack.unpackb(packed, ext_hook=ext_hook, raw=False)
>>> data == unpacked
True

Advanced unpacking control

As an alternative to iteration, Unpacker objects provide unpack, skip, read_array_header and read_map_header methods. The former two read an entire message from the stream, respectively de-serialising and returning the result, or ignoring it. The latter two methods return the number of elements in the upcoming container, so that each element in an array, or key-value pair in a map, can be unpacked or skipped individually.

Each of these methods may optionally write the packed data it reads to a callback function:

from io import BytesIO

def distribute(unpacker, get_worker):
    nelems = unpacker.read_map_header()
    for i in range(nelems):
        # Select a worker for the given key
        key = unpacker.unpack()
        worker = get_worker(key)

        # Send the value as a packed message to worker
        bytestream = BytesIO()
        unpacker.skip(bytestream.write)
        worker.send(bytestream.getvalue())

Notes

string and binary type

Early versions of msgpack didn’t distinguish string and binary types (like Python 1). The type for representing both string and binary types was named raw.

For backward compatibility reasons, msgpack-python will still default all strings to byte strings, unless you specify the use_bin_type=True option in the packer. If you do so, it will use a non-standard type called bin to serialize byte arrays, and raw becomes to mean str. If you want to distinguish bin and raw in the unpacker, specify raw=False.

Note that Python 2 defaults to byte-arrays over Unicode strings:

>>> import msgpack
>>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs']))
['spam', 'eggs']
>>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs'], use_bin_type=True),
                    raw=False)
['spam', u'eggs']

This is the same code in Python 3 (same behaviour, but Python 3 has a different default):

>>> import msgpack
>>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs']))
[b'spam', b'eggs']
>>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs'], use_bin_type=True),
                    raw=False)
[b'spam', 'eggs']

ext type

To use the ext type, pass msgpack.ExtType object to packer.

>>> import msgpack
>>> packed = msgpack.packb(msgpack.ExtType(42, b'xyzzy'))
>>> msgpack.unpackb(packed)
ExtType(code=42, data='xyzzy')

You can use it with default and ext_hook. See below.

Note about performance

GC

CPython’s GC starts when growing allocated object. This means unpacking may cause useless GC. You can use gc.disable() when unpacking large message.

use_list option

List is the default sequence type of Python. But tuple is lighter than list. You can use use_list=False while unpacking when performance is important.

Python’s dict can’t use list as key and MessagePack allows array for key of mapping. use_list=False allows unpacking such message. Another way to unpacking such object is using object_pairs_hook.

Development

Test

MessagePack uses pytest for testing. Run test with following command:

$ make test

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

msgpack-0.5.4.tar.gz (142.8 kB view details)

Uploaded Source

Built Distributions

msgpack-0.5.4-cp36-cp36m-win_amd64.whl (86.9 kB view details)

Uploaded CPython 3.6m Windows x86-64

msgpack-0.5.4-cp36-cp36m-win32.whl (78.2 kB view details)

Uploaded CPython 3.6m Windows x86

msgpack-0.5.4-cp36-cp36m-manylinux1_x86_64.whl (321.0 kB view details)

Uploaded CPython 3.6m

msgpack-0.5.4-cp36-cp36m-manylinux1_i686.whl (305.2 kB view details)

Uploaded CPython 3.6m

msgpack-0.5.4-cp35-cp35m-win_amd64.whl (86.3 kB view details)

Uploaded CPython 3.5m Windows x86-64

msgpack-0.5.4-cp35-cp35m-win32.whl (77.4 kB view details)

Uploaded CPython 3.5m Windows x86

msgpack-0.5.4-cp35-cp35m-manylinux1_x86_64.whl (314.5 kB view details)

Uploaded CPython 3.5m

msgpack-0.5.4-cp35-cp35m-manylinux1_i686.whl (296.6 kB view details)

Uploaded CPython 3.5m

msgpack-0.5.4-cp27-cp27mu-manylinux1_x86_64.whl (300.5 kB view details)

Uploaded CPython 2.7mu

msgpack-0.5.4-cp27-cp27mu-manylinux1_i686.whl (285.0 kB view details)

Uploaded CPython 2.7mu

msgpack-0.5.4-cp27-cp27m-win_amd64.whl (85.4 kB view details)

Uploaded CPython 2.7m Windows x86-64

msgpack-0.5.4-cp27-cp27m-win32.whl (76.6 kB view details)

Uploaded CPython 2.7m Windows x86

msgpack-0.5.4-cp27-cp27m-manylinux1_x86_64.whl (300.5 kB view details)

Uploaded CPython 2.7m

msgpack-0.5.4-cp27-cp27m-manylinux1_i686.whl (285.0 kB view details)

Uploaded CPython 2.7m

File details

Details for the file msgpack-0.5.4.tar.gz.

File metadata

  • Download URL: msgpack-0.5.4.tar.gz
  • Upload date:
  • Size: 142.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for msgpack-0.5.4.tar.gz
Algorithm Hash digest
SHA256 00517e3d3a214b23ab8c892c85fbbb9a48130267a9dad6dce1bc1186855e938d
MD5 9912664ff4d99140bfe56897b616a9a0
BLAKE2b-256 05572312fa4ef99ac448c2fb6282e1d0a3a4efb3f2c0ff1a0a2ee24b7d755615

See more details on using hashes here.

File details

Details for the file msgpack-0.5.4-cp36-cp36m-win_amd64.whl.

File metadata

File hashes

Hashes for msgpack-0.5.4-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 9891fda7a91fff8b4e7cffbfc78bbe8e102cfd0902dc1b8afb75ff548c81b354
MD5 e47155fac8f82da8afe34e14e72667e4
BLAKE2b-256 49d49de82110a830f4bcffe2c755261473d01694260b57cef8eda823856491f9

See more details on using hashes here.

File details

Details for the file msgpack-0.5.4-cp36-cp36m-win32.whl.

File metadata

File hashes

Hashes for msgpack-0.5.4-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 0fa6ccf088284d2a77a90bddc74ff9d87e706cd3faacb101ec96bc940559c7d2
MD5 08dfde78a01669c691dc2d84074916b7
BLAKE2b-256 5334a9225ec811bfed97a20fb07ec568b76f24f78ac222c2ab1cea45e08e7ba2

See more details on using hashes here.

File details

Details for the file msgpack-0.5.4-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for msgpack-0.5.4-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 9006836dd4e583bd5657e7cd4ff18d74168220badf133802df8258cb23a521a7
MD5 9da3b3bfdeeb6ab11f902806692c83ab
BLAKE2b-256 a4f32667a732ac0b4827e7d42153f2e8c6ebbdf1a703baa9859f0ed5456dee8b

See more details on using hashes here.

File details

Details for the file msgpack-0.5.4-cp36-cp36m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for msgpack-0.5.4-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 8a9b29b7171c05a58f0d08048ebcdb25bcc8bfc489c9357bf31be89ff08ce618
MD5 ef6f4d6ccda9f353c70507e3251ccc12
BLAKE2b-256 35157e3b04d967a363a833eee2699d927b1045bf616e168b6d4cd6f49612e966

See more details on using hashes here.

File details

Details for the file msgpack-0.5.4-cp35-cp35m-win_amd64.whl.

File metadata

File hashes

Hashes for msgpack-0.5.4-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 bb5382aa9027e41c9e3e91be26a7811938acfba2bff3f6039f2eadc889dc4313
MD5 f4116e0b7b949e5b8955b0a7059c2eae
BLAKE2b-256 6d4eb06ee53add9c5717d49e5433e22f688f65e3659291b6bc2d0e4c91d13b02

See more details on using hashes here.

File details

Details for the file msgpack-0.5.4-cp35-cp35m-win32.whl.

File metadata

File hashes

Hashes for msgpack-0.5.4-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 21d6dbd90932a009ca36699090d79b1948f6d5e7baa04044f79adeafaa1175cc
MD5 dc5b54d40cfff62c0c00fe513000086d
BLAKE2b-256 8026384e472cddc92f86881af55fb97f2052a4f9ce57f86f00b115c242057188

See more details on using hashes here.

File details

Details for the file msgpack-0.5.4-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for msgpack-0.5.4-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 138d27a258a090ddfcc06e4dff3f349ca8ad42b360a24142e7b0f8400104870e
MD5 e0760f0e59e4a0b080e1524eeb681257
BLAKE2b-256 f1bd126131764d8f79d8152f62c5ba95e999a17bcac200d85c6ae9fdfbd188ac

See more details on using hashes here.

File details

Details for the file msgpack-0.5.4-cp35-cp35m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for msgpack-0.5.4-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 49606278c8983f00ecfd34b15f561902514063b43256d1e6d49e94400fcb6294
MD5 d15cac49bef526b6f1af194daa1e4d2a
BLAKE2b-256 b071ca9ba9bef335f7e740d2cda3f42d33495d4049f64e30949e08177518cd73

See more details on using hashes here.

File details

Details for the file msgpack-0.5.4-cp27-cp27mu-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for msgpack-0.5.4-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 40040137edc9af0be27481b8843ab1e99b8869236a63613a2ea6ebdae2eb30a5
MD5 930b99ffb75c0a6bb12ea698a725d302
BLAKE2b-256 fd55fa2ba34b11d143e2462f307475df561237ad9dd3748bc86de5fc54b2a1cb

See more details on using hashes here.

File details

Details for the file msgpack-0.5.4-cp27-cp27mu-manylinux1_i686.whl.

File metadata

File hashes

Hashes for msgpack-0.5.4-cp27-cp27mu-manylinux1_i686.whl
Algorithm Hash digest
SHA256 f1d4208cf601493b85cdda89078740dcc3ff533cbffa5fd75a4e5c4059b51659
MD5 65189cd0365d5fc70604d1fc5bf375d6
BLAKE2b-256 48f5bed8896800c49fde134c98b071625b856c1b237736fa36a9092dc5556a94

See more details on using hashes here.

File details

Details for the file msgpack-0.5.4-cp27-cp27m-win_amd64.whl.

File metadata

File hashes

Hashes for msgpack-0.5.4-cp27-cp27m-win_amd64.whl
Algorithm Hash digest
SHA256 a36b908717d2eec82a49ce27993b69599e4fdcfefca7af16cf9c7dba26adfff2
MD5 b1ac573407121267fe846aa10abdffcc
BLAKE2b-256 f0e2479f0e44f569d40652848f481758fe04dcfb455285584bc985383fb39a36

See more details on using hashes here.

File details

Details for the file msgpack-0.5.4-cp27-cp27m-win32.whl.

File metadata

File hashes

Hashes for msgpack-0.5.4-cp27-cp27m-win32.whl
Algorithm Hash digest
SHA256 e878f9cdbfa337fa8a1400682b33791e08c7d32646c5878296776ee1e0dbb3e5
MD5 166d8a05480e939e23c137d1e11e3855
BLAKE2b-256 d7c4c89009aa9fb2946c19508afa9d3303205659d9c5ffcbb1aee685a5188321

See more details on using hashes here.

File details

Details for the file msgpack-0.5.4-cp27-cp27m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for msgpack-0.5.4-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d252b816603a8f120b01b7e165287a74644f2fd0371bb4666837a22c548f3eac
MD5 ebc3d7059ab9eddacf40c14cb4805310
BLAKE2b-256 86187ec62b304a7e7936569a103f134fac71d66faef052c9cef57786aed25728

See more details on using hashes here.

File details

Details for the file msgpack-0.5.4-cp27-cp27m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for msgpack-0.5.4-cp27-cp27m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 4c71e29ee9c8290be0e686fce87cecb71e7c87e515abe8431868bc3076988f3a
MD5 0afb83d839ed6c5f497197586e7f8b85
BLAKE2b-256 7f04d56a0c92eef3fbbce64893a4ad02664bca9c5e162ae468911036f7b5c236

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page