Skip to content
Maps
step 1/5

Reading — step 1 of 5

Read

~1 min readComposite Types

Maps

Maps (dictionaries) are length-prefixed sequences of (key, value) pairs.

  • fixmap: 0x80..0x8f — top 4 bits 1000, low 4 bits = pair count (0-15).
  • map 16: 0xde + 2 byte length BE.
  • map 32: 0xdf + 4 byte length BE.

Each pair: key encoded, value encoded.

Example: {"name":"Alice","age":30}:

  • fixmap of length 2: 82.
  • key1: fixstr "name" (a4 6e 61 6d 65).
  • value1: fixstr "Alice" (a5 41 6c 69 63 65).
  • key2: fixstr "age" (a3 61 67 65).
  • value2: positive fixint 30 (1e).
  • Total: 82 a4 6e 61 6d 65 a5 41 6c 69 63 65 a3 61 67 65 1e = 17 bytes.

vs JSON {"name":"Alice","age":30} = 25 bytes.

Keys can be any type:

  • String (most common).
  • Integer.
  • Tuples (in some implementations).

Most implementations restrict to hashable types.

Ordering:

  • Wire format preserves insertion order.
  • But map equality: order-independent (per spec).
  • Decoder may produce dict (Python) or LinkedHashMap (Java) — language-dependent.

Duplicate keys:

  • Spec says: undefined behavior.
  • Most libraries: last value wins.
  • Don't rely on it.

Decoding:

python

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…