Introduction
MessagePack is a binary form of the same values JSON holds: maps, arrays, strings, numbers, booleans and nil, plus raw binary data. Sadayuki Furuhashi created it in 2008. Small values take a single byte, so encoded data is usually much smaller than JSON and faster to read. Unlike Protocol Buffers it needs no schema: every value says what type it is.
Structure
A MessagePack file has no header. It is one value, or a run of values one after another. Each value starts with a type byte; multi-byte numbers and lengths that follow it are big-endian.
| First byte | Type | Value |
|---|---|---|
00–7F | positive fixint | the byte itself, 0 to 127 |
80–8F | fixmap | a map of up to 15 key and value pairs |
90–9F | fixarray | an array of up to 15 values |
A0–BF | fixstr | a UTF-8 string of up to 31 bytes |
C0 | nil | no value |
C2, C3 | false, true | booleans |
C4–C6 | bin 8, 16, 32 | a length, then raw bytes |
C7–C9 | ext 8, 16, 32 | a length, a type number, then data |
CA, CB | float 32, 64 | IEEE 754 numbers |
CC–CF | uint 8 to 64 | unsigned integers |
D0–D3 | int 8 to 64 | signed integers |
D4–D8 | fixext 1 to 16 | a type number and 1, 2, 4, 8 or 16 bytes of data |
D9–DB | str 8, 16, 32 | a length, then a UTF-8 string |
DC, DD | array 16, 32 | a count, then that many values |
DE, DF | map 16, 32 | a count, then that many key and value pairs |
E0–FF | negative fixint | −32 to −1 |
The byte C1 is never used. Extension type −1 is reserved for timestamps.
The format codes page lists every
first byte.
In the explorer
Drop a .msgpack file on this site. The structure tree shows every value
as a node: scalars with their format byte, length and data, arrays and maps with their
count, elements and key and value pairs, extension values with their type number and the
meaning of it (a timestamp is decoded to a date, in all three forms), and strings that are
not valid UTF-8 are flagged. A file that holds several values one after another shows
each. The Values table lists the top-level values, the Formats table counts each format
byte used, and a JSON-like text shows the whole file as readable text (binary as
$bin, extensions as $ext, timestamps as dates). A cut file, a
length that runs past the end, an unused C1 byte or a timestamp of a wrong
size stops the reading at that value, with the reason, and the tree shows what was read
as a damaged value.
The JSON text stops at about a million characters and nests no deeper than 60 levels
(deeper values are shown as …); when it stops it says so, and the tree
holds every value. A long array or map is shown in the tree in groups of 5,000 elements
or pairs (“Elements 5,001 to 10,000”), opened on demand, so every item is reachable. A
file is scanned to its end whatever the number of top-level values; more than 20,000 of
them are shown in the tree in groups of 5,000, and the Values table pages through all of
them. Application-defined extension types (0 to 127) have the meaning their application
gives them, so the viewer shows the type number and the bytes, labelled as the
application’s own. The old raw 16 and raw 32 forms have the
same bytes as str 16 and str 32 and are shown as strings.
Links
Software
- Fluentd – log collector that uses MessagePack for its records (Windows, macOS, Linux)
Developers
- MessagePack libraries – implementations for more than fifty languages, listed on the official site
- msgpack – MessagePack reader and writer (Python)
- @msgpack/msgpack – MessagePack encoder and decoder (JavaScript)
- msgpack-c – MessagePack library (C, C++)
Reference
- MessagePack specification – the official description of every type byte