Pikkola Msgpack, tool No. 73

The bytes behind the JSON.

Paste a MessagePack or CBOR payload as hex or base64 and read it two ways at once: a tree where every value carries its type tag and its byte range, and the JSON the payload stands for. Write it back from JSON, see exactly how many bytes each format saves, and take the file with you.

2
formats
5
samples
0
uploads

The studio

Read it, write it, weigh it

Three benches over the same value. The reader opens bytes, the writer turns JSON back into bytes, and the scale puts JSON, MessagePack and CBOR side by side. Nothing is sent anywhere.

Binary input

    Nothing leaves the tab

    No request, no account, no storage. The Content-Security-Policy sets connect-src to none, so the page could not upload a payload even if it tried. A payload can hold a session token, and this one stays in the text box.

    A reader that remembers where everything sits

    A decoder that hands back a plain object throws the offsets away. This one keeps them, so a value can be pointed at in the hex dump and a field can be traced from its first byte to its last.

    01

    The tree is the file

    Every row names its format byte (fixmap, uint32, float16, tag, indefinite text) and the range it occupies. Expand a map and each key keeps its own range beside its value, which is what makes a byte map possible at all.

    02

    JSON cannot hold bytes, so the wrappers do

    A byte string, an extension, a tag, an undefined and an integer past 2^53 have no JSON spelling. They come out as an object with one known key, and the writer reads the same shape back, so decoding and encoding again gives the same bytes.

    03

    Each format writes the shortest form it has

    A small integer takes one byte in both formats. Text under 32 bytes is a fixstr in MessagePack and one header byte in CBOR. A float that is exact in half precision is written in two bytes; one that is not goes up to single or double precision.

    Two formats, one idea

    Both put a type and a length in front of the payload and both grow a byte at a time. The tables are the whole vocabulary the reader understands.

    MessagePack formats
    First byteMeaning
    positive fixint, 0x00 to 0x7fthe integer itself, one byte
    negative fixint, 0xe0 to 0xffthe integer itself, one byte, below zero
    uint8 to uint64, int8 to int64signed and unsigned integers up to 8 bytes
    float32, float64, 0xca and 0xcbIEEE 754 in 4 or 8 bytes
    fixstr, 0xa0 to 0xbftext of up to 31 bytes
    str8, str16, str32, array16, array32, map16, map32the long forms, for anything past the fixed range
    fixmap, 0x80 to 0x8f, fixarray, 0x90 to 0x9fmap or array of up to 15 entries
    nil, false, true, 0xc0 to 0xc3null and the two booleans
    bin8, bin16, bin32, 0xc4 to 0xc6raw bytes with a 1, 2 or 4 byte length
    ext8, ext16, ext32, fixext1 to fixext16an application type byte and its payload
    0xc1reserved, never used
    CBOR major types
    First byteMeaning
    major 0, unsigned integerthe value itself, up to 8 bytes
    major 1, negative integerminus one minus the value
    major 2, byte stringraw bytes, optionally in indefinite chunks
    major 3, text stringUTF-8 text, optionally in indefinite chunks
    major 4, arrayitems in order, optionally until a break
    major 5, mapkey and value pairs, any key type
    major 6, taga number that says what the next value means
    major 7, simple and floatfalse, true, null, undefined, half, single, double, break

    Questions

    Is the payload uploaded?

    No. Both readers and both writers are functions in JavaScript files on this domain, and the Content-Security-Policy sets connect-src to none, so the page could not upload a byte even if it tried. It also works with the network off after the first load.

    Which formats does it read?

    MessagePack as specified at github.com/msgpack, and CBOR as defined in RFC 8949, including indefinite lengths, tags, half precision floats and 64 bit integers. It does not read the schema based formats: Protobuf needs a schema, and this tool has none.

    Why does detection sometimes pick the wrong format?

    Both formats start with a byte that says type and length, and the same byte can mean different things in each. 0x82 is a two entry map in MessagePack and a two item array in CBOR. Detection reads the whole payload both ways and leans on the markers only one format writes, but when the bytes are truly ambiguous you can pick the format yourself.

    What are the wrappers in the JSON?

    A byte string is {"$bin": "base64"}, a MessagePack extension is {"$ext": {"type": 2, "data": "base64"}}, a CBOR tag is {"$tag": {"tag": 1, "value": 1764115200}}, an integer past 2^53 is {"$int": "9007199254740993"}, and CBOR undefined is {"$undefined": true}. The writer reads each of them back, so nothing is lost between the two.

    Are the sizes honest?

    They are the bytes each encoder actually wrote, and JSON is measured as UTF-8 text with the indentation shown. MessagePack and CBOR usually win on numbers and short repeated keys, but a payload of long unique strings saves almost nothing: text is copied byte for byte in all three.

    Does it replace a schema or a validator?

    No. It reads bytes, not meaning. A field called hp is an integer here; whether 87 is a legal hit point is the application's business. Use it to see what a payload holds, to check an encoder you are writing, or to compare the cost of two encodings.