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.
CBOR is defined in an Internet Standards Document, RFC 7049. The format has been designed to be stable for decades.
The objectives of CBOR, roughly in decreasing order of importance,are:
- The representation must be able to unambiguously encode most common data formats used in Internet standards.
- The code for an encoder or decoder must be able to be compact in order to support systems with very limited memory, processor power, and instruction sets.
- Data must be able to be decoded without a schema description.
- The serialization must be reasonably compact, but data compactness is secondary to code compactness for the encoder and decoder.
- The format must be applicable to both constrained nodes and high-volume applications.
- The format must support all JSON data types for conversion to and from JSON.
- The format must be extensible, and the extended data must be decodable by earlier decoders.
In the context of JSON data format:
- JSON falls short when we have to send binary data.
- We typically have to base64 encode the binary data to be sent in the JSON payload, this typically adds extra size to the data (about 1/3).
- The receiver needs to have a prior knowledge of incoming data so that they can base64 decode the string in the payload.
CBOR is compact(less bytes for same representation) and faster(encoding and decoding time) than JSON.
The following JSON data takes 55 bytes.
When encoded using CBOR, it takes 44 bytes.
Here is the CBOR playground to explore more.
Breaking the above representation down:
A3 # map(3) 64 # text(4) 6E616D65 # "name" 65 # text(5) 7275736869 # "rushi" 65 # text(5) 656D61696C # "email" 70 # text(16) 61646D696E407275736869732E636F6D # "email@example.com" 63 # text(3) 7A6970 # "zip" 1A 000124F9 # unsigned(75001)
Pefer CBOR over JSON When you have the following:
- Payload is more than 2KB
- Payload has binary data.
- When data-payload readability over the network is not a constraint.
There are libraries available for most languages and incorporating CBOR in-place for JSON data-interchange should not take more than few minutes to modify your existing code. Simply put, CBOR can be used as a binary drop-in replacement for JSON.
For example, folks already using Jackson library for Java can do the following:
Jackson (Java) data format module that supports reading and writing CBOR (“Concise Binary Object Representation”) encoded data. Module extends standard Jackson streaming API (JsonFactory, JsonParser, JsonGenerator), and as such works seamlessly with all the higher level data abstractions (data binding, tree model, and pluggable extensions).
1 2 3 4 5 6
CBORFactory f = new CBORFactory(); ObjectMapper mapper = new ObjectMapper(f); // and then read/write data as usual SomeType value = ...; byte cborData = mapper.writeValueAsBytes(value); SomeType otherValue = mapper.readValue(cborData, SomeType.class);
1 2 3 4 5 6 7 8 9