1- ======================
2- MessagePack for Python
3- ======================
1+ # MessagePack for Python
42
5- .. image :: https://travis-ci.org/msgpack/msgpack-python.svg?branch=master
6- :target: https://travis-ci.org/msgpack/msgpack-python
7- :alt: Build Status
3+ [ ![ Build Status] ( https://travis-ci.org/msgpack/msgpack-python.svg?branch=master )] ( https://travis-ci.org/msgpack/msgpack-python )
4+ [ ![ Documentation Status] ( https://readthedocs.org/projects/msgpack-python/badge/?version=latest )] ( https://msgpack-python.readthedocs.io/en/latest/?badge=latest )
85
9- .. image :: https://readthedocs.org/projects/msgpack-python/badge/?version=latest
10- :target: https://msgpack-python.readthedocs.io/en/latest/?badge=latest
11- :alt: Documentation Status
12-
13-
14- What's this
15- -----------
6+ ## What's this
167
178` MessagePack <https://msgpack.org/> ` _ is an efficient binary serialization format.
189It lets you exchange data among multiple languages like JSON.
1910But it's faster and smaller.
2011This package provides CPython bindings for reading and writing MessagePack data.
2112
2213
23- Very important notes for existing users
24- ---------------------------------------
14+ ## Very important notes for existing users
2515
26- PyPI package name
27- ^^^^^^^^^^^^^^^^^
16+ ### PyPI package name
2817
2918TL;DR: When upgrading from msgpack-0.4 or earlier, don't do ` pip install -U msgpack-python ` .
3019Do ` pip uninstall msgpack-python; pip install msgpack ` instead.
@@ -37,8 +26,7 @@ Sadly, this doesn't work for upgrade install. After `pip install -U msgpack-pyt
3726msgpack is removed, and ` import msgpack ` fail.
3827
3928
40- Compatibility with the old format
41- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
29+ ### Compatibility with the old format
4230
4331You can use `` use_bin_type=False `` option to pack `` bytes ``
4432object into raw type in the old msgpack spec, instead of bin type in new msgpack spec.
@@ -49,8 +37,7 @@ It unpacks str (raw) type in msgpack into Python bytes.
4937See note below for detail.
5038
5139
52- Major breaking changes in msgpack 1.0
53- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
40+ ### Major breaking changes in msgpack 1.0
5441
5542* Python 2
5643
@@ -75,16 +62,13 @@ Major breaking changes in msgpack 1.0
7562 which type is not bytes or str.
7663
7764
78- Install
79- -------
65+ ## Install
8066
81- ::
8267
8368 $ pip install msgpack
8469
8570
86- Pure Python implementation
87- ^^^^^^^^^^^^^^^^^^^^^^^^^^
71+ ### Pure Python implementation
8872
8973The extension module in msgpack (`` msgpack._cmsgpack `` ) does not support
9074Python 2 and PyPy.
@@ -96,26 +80,20 @@ Since the [pip](https://pip.pypa.io/) uses the pure Python implementation,
9680Python 2 support will not be dropped in the foreseeable future.
9781
9882
99- Windows
100- ^^^^^^^
83+ ### Windows
10184
10285When you can't use a binary distribution, you need to install Visual Studio
10386or Windows SDK on Windows.
10487Without extension, using pure Python implementation on CPython runs slowly.
10588
10689
107- How to use
108- ----------
90+ ## How to use
10991
110- .. note ::
92+ NOTE: In examples below, I use `` raw=False `` and `` use_bin_type=True `` for users
93+ using msgpack < 1.0. These options are default from msgpack 1.0 so you can omit them.
11194
112- In examples below, I use ``raw=False `` and ``use_bin_type=True `` for users
113- using msgpack < 1.0.
114- These options are default from msgpack 1.0 so you can omit them.
11595
116-
117- One-shot pack & unpack
118- ^^^^^^^^^^^^^^^^^^^^^^
96+ ### One-shot pack & unpack
11997
12098Use `` packb `` for packing and `` unpackb `` for unpacking.
12199msgpack provides `` dumps `` and `` loads `` as an alias for compatibility with
@@ -124,35 +102,33 @@ msgpack provides ``dumps`` and ``loads`` as an alias for compatibility with
124102`` pack `` and `` dump `` packs to a file-like object.
125103`` unpack `` and `` load `` unpacks from a file-like object.
126104
127- .. code-block :: pycon
128-
105+ ``` pycon
129106 >>> import msgpack
130107 >>> msgpack.packb([1, 2, 3], use_bin_type=True)
131108 '\x93\x01\x02\x03'
132109 >>> msgpack.unpackb(_, raw=False)
133110 [1, 2, 3]
111+ ```
134112
135113`` unpack `` unpacks msgpack's array to Python's list, but can also unpack to tuple:
136114
137- .. code-block :: pycon
138-
115+ ``` pycon
139116 >>> msgpack.unpackb(b'\x93\x01\x02\x03', use_list=False, raw=False)
140117 (1, 2, 3)
118+ ```
141119
142120You should always specify the `` use_list `` keyword argument for backward compatibility.
143121See performance issues relating to ` use_list option ` _ below.
144122
145123Read the docstring for other options.
146124
147125
148- Streaming unpacking
149- ^^^^^^^^^^^^^^^^^^^
126+ ### Streaming unpacking
150127
151128`` Unpacker `` is a "streaming unpacker". It unpacks multiple objects from one
152129stream (or from bytes provided through its `` feed `` method).
153130
154- .. code-block :: python
155-
131+ ``` py
156132 import msgpack
157133 from io import BytesIO
158134
@@ -165,16 +141,15 @@ stream (or from bytes provided through its ``feed`` method).
165141 unpacker = msgpack.Unpacker(buf, raw = False )
166142 for unpacked in unpacker:
167143 print (unpacked)
144+ ```
168145
169146
170- Packing/unpacking of custom data type
171- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
147+ ### Packing/unpacking of custom data type
172148
173149It is also possible to pack/unpack custom data types. Here is an example for
174150`` datetime.datetime `` .
175151
176- .. code-block :: python
177-
152+ ``` py
178153 import datetime
179154 import msgpack
180155
@@ -196,19 +171,18 @@ It is also possible to pack/unpack custom data types. Here is an example for
196171
197172 packed_dict = msgpack.packb(useful_dict, default = encode_datetime, use_bin_type = True )
198173 this_dict_again = msgpack.unpackb(packed_dict, object_hook = decode_datetime, raw = False )
174+ ```
199175
200176`` Unpacker `` 's `` object_hook `` callback receives a dict; the
201177`` object_pairs_hook `` callback may instead be used to receive a list of
202178key-value pairs.
203179
204180
205- Extended types
206- ^^^^^^^^^^^^^^
181+ ### Extended types
207182
208183It is also possible to pack/unpack custom data types using the ** ext** type.
209184
210- .. code-block :: pycon
211-
185+ ``` pycon
212186 >>> import msgpack
213187 >>> import array
214188 >>> def default(obj):
@@ -228,10 +202,10 @@ It is also possible to pack/unpack custom data types using the **ext** type.
228202 >>> unpacked = msgpack.unpackb(packed, ext_hook=ext_hook, raw=False)
229203 >>> data == unpacked
230204 True
205+ ```
231206
232207
233- Advanced unpacking control
234- ^^^^^^^^^^^^^^^^^^^^^^^^^^
208+ ### Advanced unpacking control
235209
236210As an alternative to iteration, `` Unpacker `` objects provide `` unpack `` ,
237211`` skip `` , `` read_array_header `` and `` read_map_header `` methods. The former two
@@ -243,8 +217,7 @@ in a map, can be unpacked or skipped individually.
243217Each of these methods may optionally write the packed data it reads to a
244218callback function:
245219
246- .. code-block :: python
247-
220+ ``` py
248221 from io import BytesIO
249222
250223 def distribute (unpacker , get_worker ):
@@ -258,46 +231,41 @@ callback function:
258231 bytestream = BytesIO()
259232 unpacker.skip(bytestream.write)
260233 worker.send(bytestream.getvalue())
234+ ```
261235
236+ ## Notes
262237
263- Notes
264- -----
265-
266- string and binary type
267- ^^^^^^^^^^^^^^^^^^^^^^
238+ ### string and binary type
268239
269240Early versions of msgpack didn't distinguish string and binary types.
270241The type for representing both string and binary types was named ** raw** .
271242
272243You can pack into and unpack from this old spec using `` use_bin_type=False ``
273244and `` raw=True `` options.
274245
275- .. code-block :: pycon
276-
246+ ``` pycon
277247 >>> import msgpack
278248 >>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs'], use_bin_type=False), raw=True)
279249 [b'spam', b'eggs']
280250 >>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs'], use_bin_type=True), raw=False)
281251 [b'spam', 'eggs']
252+ ```
282253
283-
284- ext type
285- ^^^^^^^^
254+ ### ext type
286255
287256To use the ** ext** type, pass `` msgpack.ExtType `` object to packer.
288257
289- .. code-block :: pycon
290-
258+ ``` pycon
291259 >>> import msgpack
292260 >>> packed = msgpack.packb(msgpack.ExtType(42, b'xyzzy'))
293261 >>> msgpack.unpackb(packed)
294262 ExtType(code=42, data='xyzzy')
263+ ```
295264
296265You can use it with `` default `` and `` ext_hook `` . See below.
297266
298267
299- Security
300- ^^^^^^^^
268+ ### Security
301269
302270To unpacking data received from unreliable source, msgpack provides
303271two security options.
@@ -311,8 +279,7 @@ there is a risk of the hashdos.
311279If you need to support other types for map keys, use `` strict_map_key=False `` .
312280
313281
314- Performance tips
315- ^^^^^^^^^^^^^^^^
282+ ### Performance tips
316283
317284CPython's GC starts when growing allocated object.
318285This means unpacking may cause useless GC.
@@ -323,17 +290,13 @@ But tuple is lighter than list.
323290You can use `` use_list=False `` while unpacking when performance is important.
324291
325292
326- Development
327- -----------
293+ ## Development
328294
329- Test
330- ^^^^
295+ ### Test
331296
332297MessagePack uses ` pytest ` for testing.
333298Run test with following command:
334299
300+ ```
335301 $ make test
336-
337-
338- ..
339- vim: filetype=rst
302+ ```
0 commit comments