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:

  1. The representation must be able to unambiguously encode most common data formats used in Internet standards.
  2. 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.
  3. Data must be able to be decoded without a schema description.
  4. The serialization must be reasonably compact, but data compactness is secondary to code compactness for the encoder and decoder.
  5. The format must be applicable to both constrained nodes and high-volume applications.
  6. The format must support all JSON data types for conversion to and from JSON.
  7. 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.
Example:
The following JSON data takes 55 bytes.

{"name":"rushi","email":"admin@rushis.com","zip":75001}

When encoded using CBOR, it takes 44 bytes.
Here is the CBOR playground to explore more.

A3646E616D6565727573686965656D61696C7061646D696E407275736869732E636F6D637A69701A000124F9

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 # "admin@rushis.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);

Typical Javascript Front-end code:

1
2
3
4
5
6
7
8
9
// https://github.com/paroga/cbor-js
// Include cbor.js in your or HTML page:
// <script src="path/to/cbor.js" type="text/javascript"></script>
 
// Then you can use it via the CBOR-object in your code:
 
var initial = { Hello: "World" };
var encoded = CBOR.encode(initial);
var decoded = CBOR.decode(encoded);
“If you do not know how to ask the right question, you discover nothing.”
-W. Edward Deming

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>