BSON Support in Web API 2.1

Short Introduction about BSON

BSON (Binary JSON) is binary encoded serialization of JSON documents.BSON also supports embedding document. Basically BSON extends JSON model to provide additional data types for making encoding and decoding faster than JSON.

Properties of BSON:

  1. BSON is lighter than JSON so overhead is minimum when transfer data over network.

  2. Encoding data to BSON and decoding data from BSON is efficient because of data types and numeric data types are stored as number unlike JSON.

Since I am focusing on enabling BSON Support in Web API so let’s see how we can enable BSON supports in Web API:

Since BSON media type supports available from 2.1 version so update your Web API framework using NuGet package if required .Once you update your framework you can see BSONMediaTypeFormatter class in intelligence.

BSON Formatter is not available as default formatter like JSON/XML so we have to do some modification in config file as below:

We are focusing about how we can enable BSON media type formatter in Web API

Now Web API pipeline has three media type formatter available (JSON, xml, BSON) and based on content negotiation feature in Web API framework, will pick best matching formatter for generating response.

If client request for application/BSON then Web API will take BSON formatter to fulfill client requirements.

Hope this will help to enhance your knowledge.

Ebook Download
View all
Learn
View all