About

Tool No. 73 of Pikkola

JSON is readable and wasteful. MessagePack and CBOR keep the same shapes, a map and an array and a string, and drop what a parser can infer: the quotes, the commas, the repeated key names and the decimal spelling of a number. What they gain in bytes they lose in legibility, which is why a payload that is three times smaller is also three times harder to look at. This page puts the legibility back.

What it does

Why the offsets matter

A decoder that returns a plain object throws away the only thing that makes a binary payload debuggable: where each value sits. When a payload arrives truncated, or a length field disagrees with what follows, the useful question is not what the value is but which byte said so. Keeping the range of every value, keys included, lets the page answer that. It is also what makes the byte map possible: a table of every value in the payload, in the order the bytes appear, with its type, its length and its path.

The reader keeps the format byte as a name rather than a number. A row says fixmap or uint32 or float16 or tag or indefinite text, because that is the vocabulary of the specification and the thing to search for when a payload does not decode.

JSON cannot hold a byte string

MessagePack has a byte string and CBOR has one too, and JSON has nothing like it. It also has no extension type, no tag, no undefined, and no integer past 2^53 that survives a JavaScript number. So the page writes those values as an object with one known key: $bin for base64 bytes, $ext for a MessagePack extension, $tag for a CBOR tag, $int for a large integer, $undefined for CBOR undefined, $float for NaN or an infinity, and $entries for a map whose keys are not all text. The writer reads the same shapes back, which is why decoding a payload and encoding it again gives the same bytes rather than an approximation.

A payload that genuinely has a field named $bin would be read as a byte string. That is the price of a wrapper JSON can express, and it is written on the page where the writer explains the wrappers.

The shortest form each format allows

Both formats spend a byte on a type and a length when the value is small, and grow to wider headers when it is not. The writer always takes the smallest header that fits: a positive integer under 128 is one byte, text under 32 bytes is a fixstr in MessagePack and a single header byte in CBOR, and a float that reads back exactly from half precision is written in two bytes instead of eight.

CBOR adds a canonical order, where map keys are sorted by their encoded length and then byte by byte. Turning it on makes two encoders that follow RFC 8949 produce identical bytes for the same value, which is what a signature over a payload needs. MessagePack has no canonical form, so the writer keeps the key order of the JSON.

What it does not do

Why it is free

Pikkola builds small tools with one job each and keeps them free, with no account and no lock on the result. A static page that works in the browser costs almost nothing to serve, so there is no reason to charge for it or to watch who uses it.

Contact

A payload the reader could not follow, a format byte named wrongly, a Korean or Japanese sentence that reads badly: hello@pikkola.com. The hex of the first bytes that failed is the most useful thing to send.

Back to the studio