Text String
CBOR major type 3 encodes UTF-8 text strings. The argument gives the byte count of the UTF-8 encoding, followed by that many bytes. 0x65 0x68 0x65 0x6c 0x6c 0x6f = 5-byte string 'hello'. CBOR text strings MUST be valid UTF-8.
Encoding
Major type 3 is the text string type, equivalent to JSON strings. Unlike JSON which uses backslash escaping, CBOR text strings are raw UTF-8 bytes with a byte length prefix. No escaping is needed – any valid UTF-8 sequence is valid.
The RFC 8949 well-formedness rule requires text strings to be valid UTF-8. Implementations SHOULD validate. Byte strings (major type 2) exist for data that is not valid UTF-8 or should not be interpreted as text.
Key difference from JSON: CBOR text strings are length-prefixed, so parsers do not need to scan for a closing delimiter. This makes CBOR parsing significantly faster than JSON for large strings.
Examples
| Label | Hex | Value |
|---|---|---|
| Empty | 60 | "" |
| "a" | 61 61 | "a" |
| "hello" | 65 68 65 6c 6c 6f | "hello" |
| "IETF" | 64 49 45 54 46 | "IETF" (4 bytes) |