MessagePack

MessagePack is an efficient binary serialization format. It lets you exchange data among multiple languages like JSON. But it's faster and smaller. Small integers are encoded into a single byte, and typical short strings require only one extra byte in addition to the strings themselves.

!!! abstract “References”

- [MessagePack website](https://msgpack.org)
- [MessagePack specification](https://github.com/msgpack/msgpack/blob/master/spec.md)

Serialization

The library uses the following mapping from JSON values types to MessagePack types according to the MessagePack specification:

JSON value typevalue/rangeMessagePack typefirst byte
nullnullnil0xC0
booleantruetrue0xC3
booleanfalsefalse0xC2
number_integer-9223372036854775808..-2147483649int640xD3
number_integer-2147483648..-32769int320xD2
number_integer-32768..-129int160xD1
number_integer-128..-33int80xD0
number_integer-32..-1negative fixint0xE0..0xFF
number_integer0..127positive fixint0x00..0x7F
number_integer128..255uint 80xCC
number_integer256..65535uint 160xCD
number_integer65536..4294967295uint 320xCE
number_integer4294967296..18446744073709551615uint 640xCF
number_unsigned0..127positive fixint0x00..0x7F
number_unsigned128..255uint 80xCC
number_unsigned256..65535uint 160xCD
number_unsigned65536..4294967295uint 320xCE
number_unsigned4294967296..18446744073709551615uint 640xCF
number_floatany value representable by a floatfloat 320xCA
number_floatany value NOT representable by a floatfloat 640xCB
stringlength: 0..31fixstr0xA0..0xBF
stringlength: 32..255str 80xD9
stringlength: 256..65535str 160xDA
stringlength: 65536..4294967295str 320xDB
arraysize: 0..15fixarray0x90..0x9F
arraysize: 16..65535array 160xDC
arraysize: 65536..4294967295array 320xDD
objectsize: 0..15fix map0x80..0x8F
objectsize: 16..65535map 160xDE
objectsize: 65536..4294967295map 320xDF
binarysize: 0..255bin 80xC4
binarysize: 256..65535bin 160xC5
binarysize: 65536..4294967295bin 320xC6

!!! success “Complete mapping”

The mapping is **complete** in the sense that any JSON value type can be converted to a MessagePack value.

Any MessagePack output created by `to_msgpack` can be successfully parsed by `from_msgpack`.

!!! warning “Size constraints”

The following values can **not** be converted to a MessagePack value:

  - strings with more than 4294967295 bytes
  - byte strings with more than 4294967295 bytes
  - arrays with more than 4294967295 elements
  - objects with more than 4294967295 elements

!!! info “NaN/infinity handling”

If NaN or Infinity are stored inside a JSON number, they are serialized properly. function which serializes NaN or Infinity to `null`.

??? example

```cpp
--8<-- "examples/to_msgpack.cpp"
```

Output:

```c
--8<-- "examples/to_msgpack.output"
```

Deserialization

The library maps MessagePack types to JSON value types as follows:

MessagePack typeJSON value typefirst byte
positive fixintnumber_unsigned0x00..0x7F
fixmapobject0x80..0x8F
fixarrayarray0x90..0x9F
fixstrstring0xA0..0xBF
nilnull0xC0
falsefalse0xC2
truetrue0xC3
float 32number_float0xCA
float 64number_float0xCB
uint 8number_unsigned0xCC
uint 16number_unsigned0xCD
uint 32number_unsigned0xCE
uint 64number_unsigned0xCF
int 8number_integer0xD0
int 16number_integer0xD1
int 32number_integer0xD2
int 64number_integer0xD3
str 8string0xD9
str 16string0xDA
str 32string0xDB
array 16array0xDC
array 32array0xDD
map 16object0xDE
map 32object0xDF
bin 8binary0xC4
bin 16binary0xC5
bin 32binary0xC6
ext 8binary0xC7
ext 16binary0xC8
ext 32binary0xC9
fixext 1binary0xD4
fixext 2binary0xD5
fixext 4binary0xD6
fixext 8binary0xD7
fixext 16binary0xD8
negative fixintnumber_integer0xE0-0xFF

!!! info

Any MessagePack output created by `to_msgpack` can be successfully parsed by `from_msgpack`.

??? example

```cpp
--8<-- "examples/from_msgpack.cpp"
```

Output:

```json
--8<-- "examples/from_msgpack.output"
```