Use CBOR 编码为 compact 二进制 JSON
CBOR (Concise 二进制 Object Representation, RFC 8949) 是二进制 serialization 格式化为 JSON-like 数据, smaller 和 faster than JSON.
- 1
CBOR encodes JSON-like 数据 (objects, arrays, strings, numbers, booleans, 空) 在二进制. 它是 smaller than JSON (否 quotes, 否 delimiters, compact 整数编码) 和 faster 到解析 (否文本 scanning). 它 also supports types JSON lacks: 字节 strings, dates, bignums.
- 2 Base64 编码器 →
之后编码数据到 CBOR (二进制), 编码结果 as base64 为 transport 通过文本-仅 channels (HTTP, JSON fields, logs). receiver decodes base64 到 bytes, 然后 decodes CBOR 到原始数据结构.
- 3 Base64 解码器 →
解码 the base64 to recover the CBOR bytes, then decode CBOR to the original data. This two-step process (base64 → CBOR → data) is common when CBOR must transit text-based protocols.
- 4
Use CBOR 当: 载荷大小 matters (IoT, mobile), parsing 速度是 关键, 或你 need 二进制数据 (字节 arrays) 无 base64 inflation. 为 web APIs, JSON 是仍然默认 — CBOR requires 内容 negotiation (Accept: 应用/cbor). Use 它为 内部 services 和 constrained environments.
常见问题
- CBOR vs MessagePack?
- 两者是 二进制 JSON formats. CBOR 是 IETF 标准 (RFC 8949) 具有 richer 类型系统 (tags, 简单 values). MessagePack 是 faster 但更少 extensible. Choose CBOR 为标准合规和 extensibility; MessagePack 为 raw 速度.
- 是 CBOR smaller than JSON?
- 典型 20-50% smaller 为相同数据, depending 开内容. Strings 和大 integers 收益最多. 为数据具有许多小 integers 或 booleans, savings 是 larger 由于 CBOR's compact 整数编码.