BSON is often referred as Binary JSON. BSON is the serialization of JSON-like objects that are binary encoded. Just like JSON, BSON also supports nested objects, arrays, strings, integers etc.
BSON supports some more data types, especially raw binary data. So we can consider BSON as a super-set of JSON.
BSON is designed to be fast to transport and fast to decode. The main features of BSON are:
- Light weight and
Lets stop lecturing about BSON and move on to discussion regarding BSON decoding and integrating it to objC project.
We will make use of Martin Kou’s Bson Decode library for this purpose. Get the files along with the binary I created for your ease from https://github.com/deepukjayan/BSON.
Add these files to your project:
Now add the required flags for these files:
The binary we added will be already listed. We will have to change it to “Optional” to avoid the error when running in iPhone simulator.
Thats it. You are all set to decode some BSON.
Import BSONCodec.h to where ever use need to decode BSON. With this wrapper, BSON decoding is as simple as a you alloc a new NSDictionary.
myDict = [myBsonRecieved BSONValue];
Lets see what to do if we need to encode something to BSON.
Just like you decoded the BSON, simply call
bsonDataToSend = [myDict BSONEncode];
Hope that helps.