title: Compare JSON and BSON
body: <p>I am comparing JSON and BSON for serializing objects. These objects contain several arrays of a large number of integers. In my test the object I am serializing contains a total number of about 12,000 integers. I am only interested in how the sizes compare of the serialized results. I am using JSON.NET as the library which does the serialization. I am using JSON because I also want to be able to work with it in Javascript.</p>
  
  <p>The size of the JSON string is about 43kb and the size of the BSON result is 161kb. So a difference factor of about 4. This is not what I expected because I looked at BSON because I thought BSON is more efficient in storing data.</p>
  
  <p>So my question is why is BSON not efficient, can it be made more efficient? Or is there another way of serializing data with arrays containing large number of integers, which can be easily handled in Javascript?</p>
  
  <p>Below you find the code to test the JSON/BSON serialization.</p>
  
  <pre><code>        // Read file which contain json string
          string _jsonString = ReadFile();
          object _object = Newtonsoft.Json.JsonConvert.DeserializeObject(_jsonString);
          FileStream _fs = File.OpenWrite("BsonFileName");
          using (Newtonsoft.Json.Bson.BsonWriter _bsonWriter = new BsonWriter(_fs) 
                 { CloseOutput = false })
          {
              Newtonsoft.Json.JsonSerializer _jsonSerializer = new JsonSerializer();
              _jsonSerializer.Serialize(_bsonWriter, _object);
              _bsonWriter.Flush();
          }
  </code></pre>
  
  <p>Edit:</p>
  
  <p>Here are the resulting files
  <a href="https://skydrive.live.com/redir?resid=9A6F31F60861DD2C!362&amp;authkey=!AKU-ZZp8C_0gcR0" rel="noreferrer">https://skydrive.live.com/redir?resid=9A6F31F60861DD2C!362&amp;authkey=!AKU-ZZp8C_0gcR0</a></p>