Json.NET 3.5 Release 6 – Binary JSON (BSON) support

A new Json.NET release! As well as the new features below this release also adds documentation for common questions and I’ve spent some time having fun working hard on performance to make this the fastest version ever. Expect a post with sweet Excel graphs in the near future.

Binary JSON (BSON) support

Json.NET now supports reading and writing binary JSON (BSON).

BSON is a standard for binary JSON. BSON supports all JSON types (objects, arrays, strings, integers, etc). Anything that can be represented in JSON can also be represented in BSON.

BSON supports some additional types, most notably binary data, making it a superset of JSON. Json.NET handles reading all BSON and will translate any unsupported types to JSON.

The two major benefits of BSON over JSON are size and speed. BSON stores binary data directly, avoiding the time overhead and the additional size of base64 encoded text that regular JSON has with binary data. Reading and writing BSON is also faster than JSON.

Product p = new Product();
p.ExpiryDate = DateTime.Parse("2009-04-05T14:45:00Z");
p.Name = "Carlos' Spicy Wieners";
p.Price = 9.95m;
p.Sizes = new[] { "Small", "Medium", "Large" };
 
MemoryStream ms = new MemoryStream();
JsonSerializer serializer = new JsonSerializer();
 
// serialize product to BSON
BsonWriter writer = new BsonWriter(ms);
serializer.Serialize(writer, p);
 
Console.WriteLine(BitConverter.ToString(ms.ToArray()));
// 7C-00-00-00-02-4E-61-6D-65-00-16-00-00-00-43-61-72-6C-
// 6F-73-27-20-53-70-69-63-79-20-57-69-65-6E-65-72-73-00-
// 09-45-78-70-69-72-79-44-61-74-65-00-E0-51-BD-76-20-01-
// 00-00-01-50-72-69-63-65-00-66-66-66-66-66-E6-23-40-04-
// 53-69-7A-65-73-00-2D-00-00-00-02-30-00-06-00-00-00-53-
// 6D-61-6C-6C-00-02-31-00-07-00-00-00-4D-65-64-69-75-6D-
// 00-02-32-00-06-00-00-00-4C-61-72-67-65-00-00-00
 
 
ms.Seek(0, SeekOrigin.Begin);
 
// deserialize product from BSON
BsonReader reader = new BsonReader(ms);
Product deserializedProduct = serializer.Deserialize<Product>(reader);
 
Console.WriteLine(deserializedProduct.Name);
// Carlos' Spicy Wieners

Decoupling

Json.NET no longer has dependencies on the Entity Framework, LINQ to SQL or data annotations assemblies. It should now run under the .NET 4 Client Profile – a subset of the .NET framework designed for client applications. Read more about the .NET 4 Client Profile here.

DataSetConverter

Json.NET now automatically serializes and deserializes DataSets and DataTables.

A DataSet is serialized as an object with table names as its properties. A DataTable is serialized as an array of table rows, each row an object of name/values using the column name and column value for that row.

DataSet ds = new DataSet();
ds.Tables.Add(CreateDataTable("FirstTable", 2));
ds.Tables.Add(CreateDataTable("SecondTable", 1));
 
string json = JsonConvert.SerializeObject(ds, Formatting.Indented, new IsoDateTimeConverter());
// {
//   "FirstTable": [
//     {
//       "StringCol": "Item Name",
//       "Int32Col": 1,
//       "BooleanCol": true,
//       "TimeSpanCol": "10.22:10:15.1000000",
//       "DateTimeCol": "2000-12-29T00:00:00Z",
//       "DecimalCol": 64.0021
//     },
//     {
//       "StringCol": "Item Name",
//       "Int32Col": 2,
//       "BooleanCol": true,
//       "TimeSpanCol": "10.22:10:15.1000000",
//       "DateTimeCol": "2000-12-29T00:00:00Z",
//       "DecimalCol": 64.0021
//     }
//   ],
//   "SecondTable": [
//     {
//       "StringCol": "Item Name",
//       "Int32Col": 1,
//       "BooleanCol": true,
//       "TimeSpanCol": "10.22:10:15.1000000",
//       "DateTimeCol": "2000-12-29T00:00:00Z",
//       "DecimalCol": 64.0021
//     }
//   ]
// }
 
DataSet deserializedDs = JsonConvert.DeserializeObject<DataSet>(json, new IsoDateTimeConverter());

LINQ to JSON SelectToken

SelectToken provides a method to query LINQ to JSON using a single string path to a desired JToken. SelectToken is handy for dynamic queries easy because the entire query is defined in a string.

string name = (string)o.SelectToken("Manufacturers[0].Name");

SelectToken is a method on JToken and takes a string path to a child token. SelectToken returns the child token or a null reference if a token couldn't be found at the path's location.

The path is made up of property names and array indexes separated by periods. Array indexes can use either square or round brackets. Both of the following are valid paths and are equivalent to each other: Manufacturers[0].Name and Manufacturers(0).Name.

JObject o = JObject.Parse(@"{
  ""Stores"": [
    ""Lambton Quay"",
    ""Willis Street""
  ],
  ""Manufacturers"": [
    {
      ""Name"": ""Acme Co"",
      ""Products"": [
        {
          ""Name"": ""Anvil"",
          ""Price"": 50
        }
      ]
    },
    {
      ""Name"": ""Contoso"",
      ""Products"": [
        {
          ""Name"": ""Elbow Grease"",
          ""Price"": 99.95
        },
        {
          ""Name"": ""Headlight Fluid"",
          ""Price"": 4
        }
      ]
    }
  ]
}");
 
string name = (string)o.SelectToken("Manufacturers[0].Name");
// Acme Co
 
decimal productPrice = (decimal)o.SelectToken("Manufacturers[0].Products[0].Price");
// 50
 
string productName = (string)o.SelectToken("Manufacturers[1].Products[0].Name");
// Elbow Grease

Changes

Here is a complete list of what has changed since Json.NET 3.5 Release 5.

  • New feature - Added reading and writing binary JSON (BSON) support via BsonReader, BsonWriter
  • New feature - Added support for reading and writing byte arrays to JsonReader, JsonWriter and LINQ to JSON classes
  • New feature - Added ReadAsBytes to JsonReader
  • New feature - Added DataSetConverter, DataTableConverter
  • New feature - Added JPath and SelectToken to JToken
  • New feature - Added ObjectCreationHandling to JsonPropertyAttribute
  • New feature - Added IValueProvider, ReflectionValueProvider, DynamicValueProvider
  • New feature - Added serialization event support to Silverlight version
  • New feature - Added DBNull support to JValue
  • Change - Removed dependency on Entity Framework, LINQ to SQL and data annotations assemblies
  • Change - Upgraded Silverlight project to Silverlight 3
  • Change - Changed IsRequired to Required enum on JsonPropertyAttribute with options Default, AllowNull and Always
  • Change - Change converter to be resolved from new Converter property on JsonProperty
  • Change - Improved error message when JSON array encountered for object and vice-versa when deserializing
  • Change - Improved error message for converting types when deserializing
  • Change - Improved error message from getting and setting properties when serializing and deserializing
  • Change - Class converter now resolved from contract
  • Change - Built in serializers now resolved when contract is created
  • Change - JsonRaw removed and replaced with JRaw
  • Fix - Schema id not being written for collections
  • Fix - TimeSpan serialization incorrect on Compact Framework
  • Fix - Unicode line break string encoding when writing JSON
  • Fix - Empty string being incorrectly converted to null when deserializing
  • Fix - Unclosed object when including type name with array
  • Fix - Serializing objects with generic methods failing on Compact Framework
  • Fix - Remove DateTimeOffset stand-in from Compact Framework build
  • Fix - Modified .NET 2.0 build to run on environments without .NET 2.0 SP1
  • Fix - Changed deserialization to always use a new value created from a JsonConverter
  • Fix - XmlNodeConverter not converting comments in JSON
  • Fix - ToString culture inconsistency when serializing dictionary key
  • Fix - JTokenWriter not writing comments
  • Fix - Properties on existing derived objects not being populated during deserialization

Links

Json.NET CodePlex Project

Json.NET 3.5 Release 6 Download – Json.NET source code, documentation and binaries