CBOR

The Concise Binary Object Representation (CBOR) is a data format whose design goals include the possibility of extremely small code size, fairly small message size, and extensibility without the need for version negotiation.

!!! abstract “References”

- [CBOR Website](http://cbor.io) - the main source on CBOR
- [CBOR Playground](http://cbor.me) - an interactive webpage to translate between JSON and CBOR
- [RFC 7049](https://tools.ietf.org/html/rfc7049) - the CBOR specification

Serialization

The library uses the following mapping from JSON values types to CBOR types according to the CBOR specification (RFC 7049):

JSON value typevalue/rangeCBOR typefirst byte
nullnullNull0xF6
booleantrueTrue0xF5
booleanfalseFalse0xF4
number_integer-9223372036854775808..-2147483649Negative integer (8 bytes follow)0x3B
number_integer-2147483648..-32769Negative integer (4 bytes follow)0x3A
number_integer-32768..-129Negative integer (2 bytes follow)0x39
number_integer-128..-25Negative integer (1 byte follow)0x38
number_integer-24..-1Negative integer0x20..0x37
number_integer0..23Integer0x00..0x17
number_integer24..255Unsigned integer (1 byte follow)0x18
number_integer256..65535Unsigned integer (2 bytes follow)0x19
number_integer65536..4294967295Unsigned integer (4 bytes follow)0x1A
number_integer4294967296..18446744073709551615Unsigned integer (8 bytes follow)0x1B
number_unsigned0..23Integer0x00..0x17
number_unsigned24..255Unsigned integer (1 byte follow)0x18
number_unsigned256..65535Unsigned integer (2 bytes follow)0x19
number_unsigned65536..4294967295Unsigned integer (4 bytes follow)0x1A
number_unsigned4294967296..18446744073709551615Unsigned integer (8 bytes follow)0x1B
number_floatany value representable by a floatSingle-Precision Float0xFA
number_floatany value NOT representable by a floatDouble-Precision Float0xFB
stringlength: 0..23UTF-8 string0x60..0x77
stringlength: 23..255UTF-8 string (1 byte follow)0x78
stringlength: 256..65535UTF-8 string (2 bytes follow)0x79
stringlength: 65536..4294967295UTF-8 string (4 bytes follow)0x7A
stringlength: 4294967296..18446744073709551615UTF-8 string (8 bytes follow)0x7B
arraysize: 0..23array0x80..0x97
arraysize: 23..255array (1 byte follow)0x98
arraysize: 256..65535array (2 bytes follow)0x99
arraysize: 65536..4294967295array (4 bytes follow)0x9A
arraysize: 4294967296..18446744073709551615array (8 bytes follow)0x9B
objectsize: 0..23map0xA0..0xB7
objectsize: 23..255map (1 byte follow)0xB8
objectsize: 256..65535map (2 bytes follow)0xB9
objectsize: 65536..4294967295map (4 bytes follow)0xBA
objectsize: 4294967296..18446744073709551615map (8 bytes follow)0xBB
binarysize: 0..23byte string0x40..0x57
binarysize: 23..255byte string (1 byte follow)0x58
binarysize: 256..65535byte string (2 bytes follow)0x59
binarysize: 65536..4294967295byte string (4 bytes follow)0x5A
binarysize: 4294967296..18446744073709551615byte string (8 bytes follow)0x5B

Binary values with subtype are mapped to tagged values (0xD8..0xDB) depending on the subtype, followed by a byte string, see “binary” cells in the table above.

!!! success “Complete mapping”

The mapping is **complete** in the sense that any JSON value type can be converted to a CBOR value.

!!! info “NaN/infinity handling”

If NaN or Infinity are stored inside a JSON number, they are serialized properly. This behavior differs from the normal JSON serialization which serializes NaN or Infinity to `null`.

!!! info “Unused CBOR types”

The following CBOR types are not used in the conversion:

  - UTF-8 strings terminated by "break" (0x7F)
  - arrays terminated by "break" (0x9F)
  - maps terminated by "break" (0xBF)
  - byte strings terminated by "break" (0x5F)
  - date/time (0xC0..0xC1)
  - bignum (0xC2..0xC3)
  - decimal fraction (0xC4)
  - bigfloat (0xC5)
  - expected conversions (0xD5..0xD7)
  - simple values (0xE0..0xF3, 0xF8)
  - undefined (0xF7)
  - half-precision floats (0xF9)
  - break (0xFF)

!!! info “Tagged items”

Binary subtypes will be serialized as tagged items. See [binary values](../binary_values.md#cbor) for an example.

??? example

```cpp
--8<-- "examples/to_cbor.cpp"
```

Output:

```c
--8<-- "examples/to_cbor.output"
```

Deserialization

The library maps CBOR types to JSON value types as follows:

CBOR typeJSON value typefirst byte
Integernumber_unsigned0x00..0x17
Unsigned integernumber_unsigned0x18
Unsigned integernumber_unsigned0x19
Unsigned integernumber_unsigned0x1A
Unsigned integernumber_unsigned0x1B
Negative integernumber_integer0x20..0x37
Negative integernumber_integer0x38
Negative integernumber_integer0x39
Negative integernumber_integer0x3A
Negative integernumber_integer0x3B
Byte stringbinary0x40..0x57
Byte stringbinary0x58
Byte stringbinary0x59
Byte stringbinary0x5A
Byte stringbinary0x5B
UTF-8 stringstring0x60..0x77
UTF-8 stringstring0x78
UTF-8 stringstring0x79
UTF-8 stringstring0x7A
UTF-8 stringstring0x7B
UTF-8 stringstring0x7F
arrayarray0x80..0x97
arrayarray0x98
arrayarray0x99
arrayarray0x9A
arrayarray0x9B
arrayarray0x9F
mapobject0xA0..0xB7
mapobject0xB8
mapobject0xB9
mapobject0xBA
mapobject0xBB
mapobject0xBF
Falsefalse0xF4
Truetrue0xF5
Nullnull0xF6
Half-Precision Floatnumber_float0xF9
Single-Precision Floatnumber_float0xFA
Double-Precision Floatnumber_float0xFB

!!! warning “Incomplete mapping”

The mapping is **incomplete** in the sense that not all CBOR types can be converted to a JSON value. The following CBOR types are not supported and will yield parse errors:

 - date/time (0xC0..0xC1)
 - bignum (0xC2..0xC3)
 - decimal fraction (0xC4)
 - bigfloat (0xC5)
 - expected conversions (0xD5..0xD7)
 - simple values (0xE0..0xF3, 0xF8)
 - undefined (0xF7)

!!! warning “Object keys”

CBOR allows map keys of any type, whereas JSON only allows strings as keys in object values. Therefore, CBOR maps with keys other than UTF-8 strings are rejected.

!!! warning “Tagged items”

Tagged items will throw a parse error by default. They can be ignored by passing `cbor_tag_handler_t::ignore` to function `from_cbor`. They can be stored by passing `cbor_tag_handler_t::store` to function `from_cbor`.

??? example

```cpp
--8<-- "examples/from_cbor.cpp"
```

Output:

```json
--8<-- "examples/from_cbor.output"
```