Concise Binary Object Representation ( CBOR) is a binary data serialization format loosely based on JSON authored by Carsten Bormann and Paul Hoffman. Like JSON, it allows the transmission of data objects that contain name–value pairs, but with size efficiency. This increases processing and transfer speeds at the cost of human readability. It is one of the data serialization layers for the Constrained Application Protocol (CoAP) and the data format on which COSE messages are based. It is also used in the Client-to-Authenticator Protocol (CTAP) within the scope of the FIDO2 project. CBOR is defined in IETF .
CBOR was inspired by MessagePack, which was developed and promoted by Sadayuki Furuhashi. CBOR extended MessagePack, particularly by allowing to distinguish text strings from byte strings, which was implemented in 2013 in MessagePack.
For types 0, 1, and 7, there is no payload; the count is the value. For types 2 (byte string) and 3 (text string), the count is the length of the payload. For types 4 (array) and 5 (map), the count is the number of items (pairs) in the payload. For type 6 (tag), the payload is a single item and the count is a numeric tag number which describes the enclosed item.
A2 # map(2)
61 # text(1)
78 # "x"
01 # unsigned(1)
62 # text(2)
797A # "yz"
F5 # primitive(21)
82 # array(2)
02 # unsigned(2)
61 # text(1)
77 # "w"
Note: While the JSON representation requires 27 characters – the 5 spaces can be omitted – the same data is encoded in only 13 octets of CBOR.
The 5-bit short count field encodes counts 0–23 directly. Short counts of 24–27 indicate the count value is in a following 8, 16, 32 or 64-bit extended count field. Values 28–30 are not assigned and must not be used.
Types are divided into "atomic" types 0–1 and 6–7, for which the count field encodes the value directly, and non-atomic types 2–5, for which the count field encodes the size of the following payload field.
A short count of 31 is used with non-atomic types 2–5 to indicate an indefinite length; the payload is the following items until a "break" marker byte of 255 (type=7, short count=31). A short count of 31 is not permitted with the other atomic types 0, 1 or 6.
Type 6 (tag) is unusual in that its count field encodes a value directly, but also has a payload field (which always consists of a single item).
Extended counts, and all multi-byte values, are encoded in network (big-endian) byte order.
A short count of 31 indicates an indefinite-length string. This is followed by zero or more definite-length strings of the same type, terminated by a "break" marker byte. The value of the item is the concatenation of the values of the enclosed items. Items of a different type, or nested indefinite-length strings, are not permitted. Text strings must be individually well-formed; UTF-8 characters may not be split across items.
Alternatively, an indefinite-length encoding with a short count of 31 may be used. This continues until a "break" marker byte of 255. Because nested items may also use the indefinite encoding, the parser must pair the break markers with the corresponding indefinite-length header bytes.
Type 5 is similar but encodes a map (also called a dictionary, or associative array) of key/value pairs. In this case, the count encodes the number of pairs of items. If the indefinite-length encoding is used, there must be an even number of items before the "break" marker byte.
The tag number provides additional type information for the following item, beyond what the 3-bit major type can provide. For example, a tag of 1 indicates that the following number is a Unix time value. A tag of 2 indicates that the following byte string encodes an unsigned bignum. A tag of 32 indicates that the following text string is a URI as defined in . defines tags 64–87 to encode homogeneous arrays of fixed-size integer or floating-point values as byte strings.
The tag 55799 is allocated to mean "CBOR data follows". This is a semantic no-op, but allows the corresponding tag bytes d9 d9 f7 to be prepended to a CBOR file without affecting its meaning. These bytes may be used as a "magic number" to distinguish the beginning of CBOR data.
The all-ones tag values 0xffff, 0xffffffff and 0xffffffffffffffff are reserved to indicate the absence of a tag in a CBOR decoding library; they should never appear in a data stream.
The break marker pseudo-item may not be the payload of a tag.
Values 0–19 are not currently defined.
The values 20–23 are used to encode the special values false, true, [[null|Nullable type]], and [[undefined|Undefined value]].
A short count of 24 indicates a 1-byte extended count follows which can be used in future to encode additional special values. To simplify decoding, the values 0–31 may not be encoded in this form. None of the values 32–255 are currently defined.
Short counts of 25, 26 or 27 indicate a following extended count field is to be interpreted as a (big-endian) 16-, 32- or 64-bit IEEE floating point value. These are the same sizes as an extended count, but are interpreted differently. In particular, for all other major types, a 2-byte extended count of 0x1234 and a 4-byte extended count of 0x00001234 are exactly equivalent. This is not the case for floating-point values.
Short counts 28–30 are reserved, like for all other major types.
A short count of 31 encodes the special "break" marker which terminates an indefinite-length encoding. This is related to, but different from, the use with other major types where a short count of 31 begins an indefinite length encoding. This is not an item, and may not appear in a defined-length payload.
| Standard actions | 0–23 | Required | Required | ||
| Specification required | 24–32767 (24–215-1) | Required | Required | ||
| First Come First Served | 32768–18446744073709551615 (215–264-1) | Required | Required | Required | . The URL can point to an Internet-Draft or a web page. |
61 # text(1)
62 # "b"
01 # unsigned(1)
61 # text(1)
61 # "a"
02 # unsigned(2)
61 # text(1)
61 # "a"
02 # unsigned(2)
61 # text(1)
62 # "b"
01 # unsigned(1)
The DAG-CBOR specification allows consistent content-addressable storage. Specifically, a piece of data can be uniquely serialized as a sequence of bytes, and this sequence can then be hashed as its CID (content ID), usable for content-addressing.
The name "DAG" denotes "directed acyclic graph", since the specification was produced specifically to allow for data objects in the form of Merkle tree.
|
|