Skip to main content

4. Serialization Considerations

This section discusses considerations for creating CBOR encodings.

4.1. Preferred Serialization​

For some values at the data model level, CBOR provides multiple serializations. For many applications, it is desirable that an encoder always chooses a particular serialization; this is referred to as "preferred serialization". The preferred serialization is defined as follows:

  • The preferred serialization for an unsigned integer is the shortest possible encoding of its value.
  • The preferred serialization for a negative integer is the shortest possible encoding of its value.
  • The preferred serialization for a byte string is to encode it with definite length.
  • The preferred serialization for a text string is to encode it with definite length and using the shortest form for the length.
  • The preferred serialization for an array is to encode it with definite length.
  • The preferred serialization for a map is to encode it with definite length and with keys appearing in bytewise lexicographic order of their deterministic encodings.
  • The preferred serialization for false is 0xf4, for true is 0xf5, and for null is 0xf6.
  • The preferred serialization for floating-point values is to use the shortest form that preserves the value.

4.2. Deterministically Encoded CBOR​

Some protocols may require that all senders convert their data into a single, deterministic encoding. This section defines "deterministically encoded CBOR" as a CBOR encoding that follows the preferred serialization rules.

4.2.1. Core Deterministic Encoding Requirements​

A CBOR encoding satisfies the "core deterministic encoding requirements" if it follows the preferred serialization defined in Section 4.1.

4.2.2. Additional Deterministic Encoding Considerations​

Deterministic encoding is not limited to the preferred serialization rules. Applications may define additional requirements for deterministic encoding.

4.2.3. Length-First Map Key Ordering​

For applications that require deterministic encoding but cannot use bytewise lexicographic ordering, this specification defines an alternative: length-first map key ordering.