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
- Reads a MessagePack or CBOR payload from hex, base64, a pasted string or a dropped file, and detects which format it is.
- Builds a tree where every value carries its format byte, its start offset and its end offset.
- Shows the hex dump beside the tree, so a byte can be traced to the value it belongs to and back.
- Writes the JSON the payload stands for, and reads that JSON back into bytes without losing a field.
- Encodes JSON to MessagePack and to CBOR, choosing the shortest form each format allows.
- Compares indented JSON, minified JSON, MessagePack and CBOR byte for byte, and breaks the binary down by type.
- Copies the JSON, the hex or the base64, and downloads .json, .msgpack or .cbor.
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
- It does not validate a schema. A field called hp is an integer here; whether 87 is a legal hit point belongs to the application.
- It does not know what a CBOR tag means beyond naming the registered ones. A tag is shown with its number and its value, not interpreted.
- It does not read MessagePack's timestamp extension as a date unless the payload uses the standard type -1, and even then the date is a convenience beside the raw bytes.
- Detection is a reading of the bytes, not a declaration. 0x82 is a two entry map in MessagePack and a two item array in CBOR, so an ambiguous payload can be read the wrong way. The format can be chosen by hand.
- It opens payloads up to 1 MiB, draws up to 4000 values in the tree and 8192 bytes in the dump, and keeps the rest in the byte map.
- It never fetches anything: no schema, no registry, no font or script from another domain, no analytics.
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.
소개
피콜라 73호기
JSON은 읽기 쉽고 낭비가 많습니다. MessagePack과 CBOR은 맵, 배열, 문자열이라는 같은 모양을 유지하면서 파서가 짐작할 수 있는 것을 덜어 냅니다. 따옴표, 쉼표, 반복되는 키 이름, 숫자의 소수 표기까지요. 바이트를 아낸 만큼 눈으로 읽기는 어려워집니다. 세 배 작아진 페이로드는 세 배 보기 어려운 법입니다. 이 페이지는 그 읽기를 돌려놓습니다.
하는 일
- MessagePack이나 CBOR 페이로드를 16진수, Base64, 붙여 넣은 문자열, 끌어다 놓은 파일에서 읽고 어느 형식인지 판별합니다.
- 값마다 형식 바이트, 시작 위치, 끝 위치를 붙인 트리를 만듭니다.
- 트리 옆에 16진수 덤프를 두어, 바이트에서 값을, 값에서 바이트를 오갈 수 있게 합니다.
- 페이로드가 뜻하는 JSON을 쓰고, 그 JSON을 필드 하나 잃지 않고 다시 바이트로 읽습니다.
- JSON을 MessagePack과 CBOR로 인코딩하며, 형식마다 허용하는 가장 짧은 모양을 고릅니다.
- 들여쓴 JSON, 최소화한 JSON, MessagePack, CBOR을 바이트 단위로 견주고 바이너리는 타입별로 나눠 보여 줍니다.
- JSON, 16진수, Base64를 복사하고 .json, .msgpack, .cbor로 내려받습니다.
위치가 왜 중요한가
평범한 객체를 돌려주는 디코더는 바이너리 페이로드를 디버깅 가능하게 만드는 유일한 것을 버립니다. 값이 어디에 있는지입니다. 페이로드가 잘려 도착했거나 길이 필드가 뒤따르는 내용과 맞지 않을 때 쓸모 있는 질문은 그 값이 무엇이냐가 아니라 어느 바이트가 그렇게 말했느냐입니다. 키까지 포함해 모든 값의 범위를 남기면 그 질문에 답할 수 있습니다. 바이트 맵도 그래서 가능해집니다. 페이로드의 모든 값을 바이트 순서대로, 타입과 길이와 경로와 함께 보여 주는 표입니다.
읽기는 형식 바이트를 숫자가 아니라 이름으로 남깁니다. 행에 fixmap, uint32, float16, tag, 길이 미정 텍스트라고 적습니다. 규격의 어휘이고, 페이로드가 풀리지 않을 때 찾아볼 이름이기 때문입니다.
JSON은 바이트 문자열을 담지 못합니다
MessagePack에도 CBOR에도 바이트 문자열이 있고, JSON에는 그런 것이 없습니다. 확장 타입도, 태그도, undefined도, 자바스크립트 숫자로 살아남는 2^53 초과 정수도 없습니다. 그래서 이 페이지는 그런 값을 정해진 키 하나를 가진 객체로 씁니다. base64 바이트는 $bin, MessagePack 확장은 $ext, CBOR 태그는 $tag, 큰 정수는 $int, CBOR undefined는 $undefined, NaN과 무한대는 $float, 키가 모두 문자열이 아닌 맵은 $entries입니다. 쓰기가 같은 모양을 다시 읽으므로, 페이로드를 디코딩했다가 인코딩하면 근사값이 아니라 같은 바이트가 나옵니다.
$bin이라는 필드를 진짜로 가진 페이로드는 바이트 문자열로 읽힙니다. JSON이 표현할 수 있는 래퍼의 대가이고, 쓰기 작업대에서 래퍼를 설명하는 자리에 적어 두었습니다.
형식마다 가장 짧은 모양
두 형식 모두 값이 작으면 타입과 길이에 한 바이트를 쓰고, 크면 더 넓은 헤더로 넘어갑니다. 쓰기는 들어맞는 가장 작은 헤더를 고릅니다. 128 미만의 양의 정수는 한 바이트, 32바이트 미만의 텍스트는 MessagePack에서 fixstr, CBOR에서 헤더 한 바이트, half 정밀도로 정확히 되읽히는 실수는 8바이트 대신 2바이트입니다.
CBOR에는 정규 순서가 있습니다. 맵의 키를 인코딩한 길이, 그다음 바이트 순으로 정렬합니다. 이 옵션을 켜면 RFC 8949를 따르는 두 인코더가 같은 값에 같은 바이트를 냅니다. 페이로드에 서명을 얹을 때 필요한 성질입니다. MessagePack에는 정규형이 없어서, 쓰기는 JSON의 키 순서를 지킵니다.
하지 않는 일
- 스키마를 검증하지 않습니다. hp라는 필드는 여기서 정수이고, 87이 합법적인 체력인지는 응용 프로그램의 몫입니다.
- CBOR 태그의 뜻은 등록된 것의 이름을 붙이는 데까지만 압니다. 태그는 번호와 값으로 보이고, 해석하지 않습니다.
- MessagePack 타임스탬프 확장은 표준 타입 -1일 때만 날짜로 보여 주고, 그때도 날짜는 원시 바이트 옆의 편의일 뿐입니다.
- 자동 감지는 바이트를 읽은 결과이지 선언이 아닙니다. 0x82는 MessagePack에서 항목 둘짜리 맵, CBOR에서 항목 둘짜리 배열이라 애매한 페이로드는 반대로 읽힐 수 있습니다. 형식은 직접 고를 수 있습니다.
- 1MiB까지 열고, 트리에는 4000개 값까지, 덤프에는 8192바이트까지 그리며 나머지는 바이트 맵에 남깁니다.
- 가져오는 것이 없습니다. 스키마도, 레지스트리도, 다른 도메인의 글꼴이나 스크립트도, 분석도 없습니다.
왜 무료인가
피콜라는 한 가지 일만 하는 작은 도구를 만들고 무료로 둡니다. 계정도, 결과에 거는 잠금도 없습니다. 브라우저에서 도는 정적 페이지는 서빙 비용이 거의 없으니, 값을 매기거나 누가 쓰는지 지켜볼 이유도 없습니다.
연락
읽지 못한 페이로드, 잘못 이름 붙인 형식 바이트, 읽기 나쁜 한국어나 일본어 문장: hello@pikkola.com. 실패한 첫 바이트들의 16진수를 보내 주시면 가장 도움이 됩니다.
概要
Pikkola の 73 番目の道具
JSON は読みやすく、無駄が多いです。MessagePack と CBOR はマップ、配列、文字列という同じ形を保ちながら、パーサが推測できるものを省きます。引用符、カンマ、繰り返すキー名、数値の小数表記まで。バイトを節約した分だけ目で読むのは難しくなります。三倍小さくなったペイロードは三倍見にくいものです。このページはその読みやすさを取り戻します。
すること
- MessagePack や CBOR のペイロードを 16 進数、Base64、貼り付けた文字列、落としたファイルから読み、どちらの形式かを判定します。
- 値ごとに形式バイト、開始位置、終了位置を付けたツリーを作ります。
- ツリーの隣に 16 進ダンプを置き、バイトから値へ、値からバイトへ行き来できるようにします。
- ペイロードが表す JSON を書き、その JSON を項目一つ失わずにバイトへ戻します。
- JSON を MessagePack と CBOR にエンコードし、形式ごとに許される最も短い形を選びます。
- 字下げした JSON、最小化した JSON、MessagePack、CBOR をバイト単位で比べ、バイナリは型ごとに分けて示します。
- JSON、16 進数、Base64 をコピーし、.json、.msgpack、.cbor で保存します。
位置がなぜ大事か
普通のオブジェクトを返すデコーダは、バイナリのペイロードを調べられるようにする唯一のものを捨てます。値がどこにあるかです。ペイロードが途中で切れて届いたり、長さの項目が続く内容と合わないとき、役に立つ問いはその値が何かではなく、どのバイトがそう言ったかです。キーも含めてすべての値の範囲を残せば、その問いに答えられます。バイトマップもそれで成り立ちます。ペイロードのすべての値をバイトの順に、型と長さとパスとともに並べる表です。
読みは形式バイトを数ではなく名前で残します。行に fixmap、uint32、float16、tag、長さ未定のテキストと書きます。規格の語彙であり、ペイロードが解けないときに探す名前だからです。
JSON はバイト列を持てません
MessagePack にも CBOR にもバイト列があり、JSON にはそれらしいものがありません。拡張型も、タグも、undefined も、JavaScript の数値として残る 2^53 超えの整数もありません。そこでこのページはそれらを決まったキー一つを持つオブジェクトとして書きます。base64 のバイトは $bin、MessagePack の拡張は $ext、CBOR のタグは $tag、大きな整数は $int、CBOR の undefined は $undefined、NaN と無限大は $float、キーがすべて文字列でないマップは $entries です。書きが同じ形を読み戻すので、ペイロードをデコードしてからエンコードすると近似ではなく同じバイトになります。
$bin という項目を本当に持つペイロードはバイト列として読まれます。JSON で書けるラッパーの代償で、書きの作業台でラッパーを説明する場所に記してあります。
形式ごとの最短の形
どちらの形式も、値が小さければ型と長さに 1 バイトを使い、大きければ広いヘッダーに移ります。書きは収まる最も小さいヘッダーを選びます。128 未満の正の整数は 1 バイト、32 バイト未満のテキストは MessagePack で fixstr、CBOR でヘッダー 1 バイト、half 精度で正確に読み戻せる浮動小数は 8 バイトではなく 2 バイトです。
CBOR には正規の順があります。マップのキーをエンコードした長さ、次にバイト順で並べます。これを入れると RFC 8949 に従う二つのエンコーダが同じ値に同じバイトを出します。ペイロードに署名を載せるときに必要な性質です。MessagePack に正規形はないので、書きは JSON のキー順を保ちます。
しないこと
- スキーマを検証しません。hp という項目はここでは整数で、87 が妥当な体力かはアプリケーションの仕事です。
- CBOR タグの意味は、登録されたものに名前を付けるところまでしか分かりません。タグは番号と値で示し、解釈しません。
- MessagePack のタイムスタンプ拡張は標準の型 -1 のときだけ日付として見せ、その場合も日付は生のバイトの隣の便宜です。
- 自動判定はバイトを読んだ結果で、宣言ではありません。0x82 は MessagePack では 2 項目のマップ、CBOR では 2 項目の配列なので、曖昧なペイロードは逆に読まれることがあります。形式は手で選べます。
- 1 MiB まで開き、ツリーには 4000 個の値まで、ダンプには 8192 バイトまで描き、残りはバイトマップに残します。
- 取得するものはありません。スキーマも、レジストリも、別ドメインのフォントやスクリプトも、解析もありません。
なぜ無料か
Pikkola は一つの仕事だけをする小さな道具を作り、無料で置きます。アカウントも、結果に掛ける鍵もありません。ブラウザで動く静的なページは配信の費用がほとんどかからないので、値段を付ける理由も、誰が使うかを見る理由もありません。
連絡
読めなかったペイロード、名前を間違えた形式バイト、読みにくい韓国語や日本語の文: hello@pikkola.com。失敗した最初のバイトの 16 進数を送っていただけると最も役に立ちます。