What is BSON ?

What is BSON ?

BSON in Web API 2.1

Binary Serialization object notation. “BSON” stands for “Binary JSON”, but BSON and JSON are serialized very differently. BSON is “JSON-like”, because objects are represented as name-value pairs, similar to JSON. Unlike JSON, numeric data types are stored as bytes, not strings

The BSON was designed to be lightweight, easy to scan, and fast to encode/decode.

BSON VS JSON

·        BSON is comparable in size to JSON. Depending on the data, a BSON payload may be smaller or larger than a JSON payload. For serializing binary data, such as an image file, BSON is smaller than JSON, because the binary data does is not base64-encoded.

·        BSON documents are easy to scan because elements are prefixed with a length field, so a parser can skip elements without decoding them.

·        Encoding and decoding are efficient, because numeric data types are stored as numbers, not strings.

Native .Net client apps can benefit from using BSON in place of text-based formats such as JSON or XML whereas for browser clients, you will probably want to stick with JSON, because JavaScript can directly convert the JSON payload.

Enabling BSON on the Server

In your Web API configuration, add the BsonMediaTypeFormatter to the formatters collection.

 

public static class WebApiConfig

{

    public static void Register(HttpConfiguration config)    {

        config.Formatters.Add(new BsonMediaTypeFormatter());

        // Other Web API configuration not shown...

    }

}

Now if the client requests “application/bson”, Web API will use the BSON formatter.

Happy Coding :)

comments powered by Disqus