{"id":13501958,"url":"https://github.com/msgpack/msgpack-python","last_synced_at":"2025-05-13T15:09:11.566Z","repository":{"id":796593,"uuid":"2242705","full_name":"msgpack/msgpack-python","owner":"msgpack","description":"MessagePack serializer implementation for Python msgpack.org[Python]","archived":false,"fork":false,"pushed_at":"2024-10-08T09:04:56.000Z","size":3070,"stargazers_count":1970,"open_issues_count":6,"forks_count":225,"subscribers_count":46,"default_branch":"main","last_synced_at":"2025-05-03T03:08:35.544Z","etag":null,"topics":["msgpack","python"],"latest_commit_sha":null,"homepage":"https://msgpack.org/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/msgpack.png","metadata":{"files":{"readme":"README.md","changelog":"ChangeLog.rst","contributing":null,"funding":null,"license":"COPYING","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2011-08-21T08:24:41.000Z","updated_at":"2025-05-02T09:36:36.000Z","dependencies_parsed_at":"2023-11-15T12:30:02.632Z","dependency_job_id":"9545ce93-e962-4d7e-8a82-600b59057053","html_url":"https://github.com/msgpack/msgpack-python","commit_stats":{"total_commits":780,"total_committers":84,"mean_commits":9.285714285714286,"dds":0.5705128205128205,"last_synced_commit":"0eeabfb453844b441a4a77097b3d5aa0cb6645b6"},"previous_names":[],"tags_count":49,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/msgpack%2Fmsgpack-python","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/msgpack%2Fmsgpack-python/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/msgpack%2Fmsgpack-python/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/msgpack%2Fmsgpack-python/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/msgpack","download_url":"https://codeload.github.com/msgpack/msgpack-python/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253969238,"owners_count":21992262,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["msgpack","python"],"created_at":"2024-07-31T22:01:56.622Z","updated_at":"2025-05-13T15:09:06.542Z","avatar_url":"https://github.com/msgpack.png","language":"Python","readme":"# MessagePack for Python\n\n[![Build Status](https://github.com/msgpack/msgpack-python/actions/workflows/wheel.yml/badge.svg)](https://github.com/msgpack/msgpack-python/actions/workflows/wheel.yml)\n[![Documentation Status](https://readthedocs.org/projects/msgpack-python/badge/?version=latest)](https://msgpack-python.readthedocs.io/en/latest/?badge=latest)\n\n## What's this\n\n[MessagePack](https://msgpack.org/) is an efficient binary serialization format.\nIt lets you exchange data among multiple languages like JSON.\nBut it's faster and smaller.\nThis package provides CPython bindings for reading and writing MessagePack data.\n\n## Install\n\n```\n$ pip install msgpack\n```\n\n### Pure Python implementation\n\nThe extension module in msgpack (`msgpack._cmsgpack`) does not support PyPy.\n\nBut msgpack provides a pure Python implementation (`msgpack.fallback`) for PyPy.\n\n\n### Windows\n\nWhen you can't use a binary distribution, you need to install Visual Studio\nor Windows SDK on Windows.\nWithout extension, using pure Python implementation on CPython runs slowly.\n\n\n## How to use\n\n### One-shot pack \u0026 unpack\n\nUse `packb` for packing and `unpackb` for unpacking.\nmsgpack provides `dumps` and `loads` as an alias for compatibility with\n`json` and `pickle`.\n\n`pack` and `dump` packs to a file-like object.\n`unpack` and `load` unpacks from a file-like object.\n\n```pycon\n\u003e\u003e\u003e import msgpack\n\u003e\u003e\u003e msgpack.packb([1, 2, 3])\n'\\x93\\x01\\x02\\x03'\n\u003e\u003e\u003e msgpack.unpackb(_)\n[1, 2, 3]\n```\n\nRead the docstring for options.\n\n\n### Streaming unpacking\n\n`Unpacker` is a \"streaming unpacker\". It unpacks multiple objects from one\nstream (or from bytes provided through its `feed` method).\n\n```py\nimport msgpack\nfrom io import BytesIO\n\nbuf = BytesIO()\nfor i in range(100):\n   buf.write(msgpack.packb(i))\n\nbuf.seek(0)\n\nunpacker = msgpack.Unpacker(buf)\nfor unpacked in unpacker:\n    print(unpacked)\n```\n\n\n### Packing/unpacking of custom data type\n\nIt is also possible to pack/unpack custom data types. Here is an example for\n`datetime.datetime`.\n\n```py\nimport datetime\nimport msgpack\n\nuseful_dict = {\n    \"id\": 1,\n    \"created\": datetime.datetime.now(),\n}\n\ndef decode_datetime(obj):\n    if '__datetime__' in obj:\n        obj = datetime.datetime.strptime(obj[\"as_str\"], \"%Y%m%dT%H:%M:%S.%f\")\n    return obj\n\ndef encode_datetime(obj):\n    if isinstance(obj, datetime.datetime):\n        return {'__datetime__': True, 'as_str': obj.strftime(\"%Y%m%dT%H:%M:%S.%f\")}\n    return obj\n\n\npacked_dict = msgpack.packb(useful_dict, default=encode_datetime)\nthis_dict_again = msgpack.unpackb(packed_dict, object_hook=decode_datetime)\n```\n\n`Unpacker`'s `object_hook` callback receives a dict; the\n`object_pairs_hook` callback may instead be used to receive a list of\nkey-value pairs.\n\nNOTE: msgpack can encode datetime with tzinfo into standard ext type for now.\nSee `datetime` option in `Packer` docstring.\n\n\n### Extended types\n\nIt is also possible to pack/unpack custom data types using the **ext** type.\n\n```pycon\n\u003e\u003e\u003e import msgpack\n\u003e\u003e\u003e import array\n\u003e\u003e\u003e def default(obj):\n...     if isinstance(obj, array.array) and obj.typecode == 'd':\n...         return msgpack.ExtType(42, obj.tostring())\n...     raise TypeError(\"Unknown type: %r\" % (obj,))\n...\n\u003e\u003e\u003e def ext_hook(code, data):\n...     if code == 42:\n...         a = array.array('d')\n...         a.fromstring(data)\n...         return a\n...     return ExtType(code, data)\n...\n\u003e\u003e\u003e data = array.array('d', [1.2, 3.4])\n\u003e\u003e\u003e packed = msgpack.packb(data, default=default)\n\u003e\u003e\u003e unpacked = msgpack.unpackb(packed, ext_hook=ext_hook)\n\u003e\u003e\u003e data == unpacked\nTrue\n```\n\n\n### Advanced unpacking control\n\nAs an alternative to iteration, `Unpacker` objects provide `unpack`,\n`skip`, `read_array_header` and `read_map_header` methods. The former two\nread an entire message from the stream, respectively de-serialising and returning\nthe result, or ignoring it. The latter two methods return the number of elements\nin the upcoming container, so that each element in an array, or key-value pair\nin a map, can be unpacked or skipped individually.\n\n\n## Notes\n\n### string and binary type in old msgpack spec\n\nEarly versions of msgpack didn't distinguish string and binary types.\nThe type for representing both string and binary types was named **raw**.\n\nYou can pack into and unpack from this old spec using `use_bin_type=False`\nand `raw=True` options.\n\n```pycon\n\u003e\u003e\u003e import msgpack\n\u003e\u003e\u003e msgpack.unpackb(msgpack.packb([b'spam', 'eggs'], use_bin_type=False), raw=True)\n[b'spam', b'eggs']\n\u003e\u003e\u003e msgpack.unpackb(msgpack.packb([b'spam', 'eggs'], use_bin_type=True), raw=False)\n[b'spam', 'eggs']\n```\n\n### ext type\n\nTo use the **ext** type, pass `msgpack.ExtType` object to packer.\n\n```pycon\n\u003e\u003e\u003e import msgpack\n\u003e\u003e\u003e packed = msgpack.packb(msgpack.ExtType(42, b'xyzzy'))\n\u003e\u003e\u003e msgpack.unpackb(packed)\nExtType(code=42, data='xyzzy')\n```\n\nYou can use it with `default` and `ext_hook`. See below.\n\n\n### Security\n\nTo unpacking data received from unreliable source, msgpack provides\ntwo security options.\n\n`max_buffer_size` (default: `100*1024*1024`) limits the internal buffer size.\nIt is used to limit the preallocated list size too.\n\n`strict_map_key` (default: `True`) limits the type of map keys to bytes and str.\nWhile msgpack spec doesn't limit the types of the map keys,\nthere is a risk of the hashdos.\nIf you need to support other types for map keys, use `strict_map_key=False`.\n\n\n### Performance tips\n\nCPython's GC starts when growing allocated object.\nThis means unpacking may cause useless GC.\nYou can use `gc.disable()` when unpacking large message.\n\nList is the default sequence type of Python.\nBut tuple is lighter than list.\nYou can use `use_list=False` while unpacking when performance is important.\n\n\n## Major breaking changes in the history\n\n### msgpack 0.5\n\nPackage name on PyPI was changed from `msgpack-python` to `msgpack` from 0.5.\n\nWhen upgrading from msgpack-0.4 or earlier, do `pip uninstall msgpack-python` before\n`pip install -U msgpack`.\n\n\n### msgpack 1.0\n\n* Python 2 support\n\n  * The extension module does not support Python 2 anymore.\n    The pure Python implementation (`msgpack.fallback`) is used for Python 2.\n  \n  * msgpack 1.0.6 drops official support of Python 2.7, as pip and\n    GitHub Action (setup-python) no longer support Python 2.7.\n\n* Packer\n\n  * Packer uses `use_bin_type=True` by default.\n    Bytes are encoded in bin type in msgpack.\n  * The `encoding` option is removed.  UTF-8 is used always.\n\n* Unpacker\n\n  * Unpacker uses `raw=False` by default.  It assumes str types are valid UTF-8 string\n    and decode them to Python str (unicode) object.\n  * `encoding` option is removed.  You can use `raw=True` to support old format (e.g. unpack into bytes, not str).\n  * Default value of `max_buffer_size` is changed from 0 to 100 MiB to avoid DoS attack.\n    You need to pass `max_buffer_size=0` if you have large but safe data.\n  * Default value of `strict_map_key` is changed to True to avoid hashdos.\n    You need to pass `strict_map_key=False` if you have data which contain map keys\n    which type is not bytes or str.\n","funding_links":[],"categories":["Serialization","Python","Data Format \u0026 I/O","Language specific","Data Serialization Formats","Data Serialization"],"sub_categories":["For Python","Python"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmsgpack%2Fmsgpack-python","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmsgpack%2Fmsgpack-python","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmsgpack%2Fmsgpack-python/lists"}