Json.NET 4.0 Release 6 – Serialization Performance

The big feature of this release is improved performance. For the first time in a couple of years I’ve sat down and benchmarked, profiled and tuned Json.NET.

And now, in the spirit of the season: start shopping. And for every dollar of Krusty merchandise you buy, I will be nice to a sick kid. For legal purposes, sick kids may include hookers with a cold.

For all its features Json.NET was already fast but there are improvements in JsonTextReader which I rewrote and object deserilization. Json.NET is faster than both .NET framework JSON serializers in all scenarios again.

Improved error messages

One of the nice features of JsonTextReader is that it keeps track of its current line number and line position and includes that information when it encounters bad JSON.

This release adds that line number and position information to deserialization errors. Now deserialization should be much easier to troubleshoot.

string json = @"[
  1,
  2,
  3,
  null
]";
 
List<int> numbers = JsonConvert.DeserializeObject<List<int>>(json);
// Error converting value {null} to type 'System.Int32'. Line 5, position 7.

Worst. Bug. Ever. Fixed.

Visual Studio Ultimate Edition has a feature called Intellitrace. Intellitrace and Json.NET didn’t get along. After a year of at first being unsure why only some people were getting the error, and then not knowing what exactly why Intellitrace was breaking Json.NET, I finally nailed it down to one three line method that Intellitrace didn’t like. Fixed... finally.

Changes

Here is a complete list of what has changed since Json.NET 4.0 Release 4 (if you’re curious where v5 is, it was just one fix for Windows Phone Mango and NuGet).

  • New feature - Added line number information to deserialization errors
  • New feature - Added ReadAsInt32 to JsonReader
  • New feature - Added BinaryReader/BinaryWriter constructor overloads to BsonReader/BsonWriter
  • Change - JsonTextReader error message when additional JSON content found is more descriptive
  • Fix - Removed unused utility methods
  • Fix - Fixed elusive Intellitrace runtime destabilization error
  • Fix - Fixed internal exception thrown when deserializing Decimal/DateTimeOffset/Byte lists
  • Fix - Fixed potential multi-threading serializing issue
  • Fix - Fixed serializing types that hides a base classes property with a proeprty of the same name
  • Fix - Fixed BsonReader to use BinaryReader instead of base stream
  • Fix - Fixed referencing the NuGet package from Windows Phone 7.1 projects

Links

Json.NET CodePlex Project

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

Json.NET 4.0 Release 4 – Bug fixes

This release contains bug fixes and some minor changes.

Custom type names with TypeNameHandling

Note that support for custom type names was added in a previous Json.NET release.

One commonly asked for feature is the ability to customize the value of the $type property when using TypeNameHandling. Fortunately new in .NET 4 is the virtual SerializationBinder.BindToName method that translates a Type to a custom string.

To customize the $type property in Json.NET just create a class that inherits from SerializationBinder and override the BindToName and the BindToType methods with your custom naming logic. The example SerializationBinder example below serializes types to just their name without any namespace or assembly information, those details are passed into the binder itself via a constructor parameter and used during deserialization.

public class TypeNameSerializationBinder : SerializationBinder
{
  public string TypeFormat { get; private set; }
 
  public TypeNameSerializationBinder(string typeFormat)
  {
    TypeFormat = typeFormat;
  }
 
  public override void BindToName(Type serializedType, out string assemblyName, out string typeName)
  {
    assemblyName = null;
    typeName = serializedType.Name;
  }
 
  public override Type BindToType(string assemblyName, string typeName)
  {
    string resolvedTypeName = string.Format(TypeFormat, typeName);
 
    return Type.GetType(resolvedTypeName, true);
  }
}

The simplest way to use a custom serialization binder is with JsonConvert and JsonSerializerSettings.

TypeNameSerializationBinder binder = new TypeNameSerializationBinder("YourAppNamespace.{0}, YourAppAssembly");
 
IList<object> values = new List<object>
  {
    new Customer
      {
        Name = "Caroline Customer"
      },
    new Purchase
      {
        ProductName = "Elbow Grease",
        Price = 5.99m,
        Quantity = 1
      }
  };
 
string json = JsonConvert.SerializeObject(values, Formatting.Indented, new JsonSerializerSettings
{
  TypeNameHandling = TypeNameHandling.Auto,
  Binder = binder
});
 
//[
//  {
//    "$type": "Customer",
//    "Name": "Caroline Customer"
//  },
//  {
//    "$type": "Purchase",
//    "ProductName": "Elbow Grease",
//    "Price": 5.99,
//    "Quantity": 1
//  }
//]

To deserialize just reuse the custom SerializationBinder with DeserializeObject.

Changes

Here is a complete list of what has changed since Json.NET 4.0 Release 3.

  • Change - JsonTextReader.Culture is now CultureInfo.InvariantCulture by default
  • Change - KeyValurPairConverter no longer depends on the order of the key and value properties
  • Change - Time zone conversions now use new TimeZoneInfo instead of TimeZone
  • Fix - Fixed boolean values sometimes being capitalized when converting to XML
  • Fix - Fixed error when deserializing ConcurrentDictionary
  • Fix - Fixed serializing some Uris returning the incorrect value
  • Fix - Fixed occasional error when converting non-long integer properties to XML
  • Fix - Fixed deserializing byte arrays with type name information
  • Fix - Fixed flag enum items not being correctly camel cased
  • Fix - Fixed JsonValidatingReader validating the first array item twice
  • Fix - Fixed JsonSchema not correctly validating integers as a subset of decimal
  • Fix - Fixed bad BSON when writing long strings of complex UTF8 characters
  • Fix - Fixed error not being raised for additional content in JSON string for JArray.Parse and JObject.Parse
  • Fix - Fixed DataTableConverter including nulls with NullValueHandling.Ignore

Links

Json.NET CodePlex Project

Json.NET 4.0 Release 4 Download – Json.NET source code, documentation and binaries

Json.NET 4.0 Release 3 – LINQ to JSON performance

One of Json.NET’s most popular features - LINQ to JSON – has gotten a speed boost.

Previously the LINQ to JSON collection objects (JObject and JArray) internally used a linked list to keep track of their children. While this gave them complete control over the order of properties inside objects, it made lookups by key (a property name for an object, an index for an array) slow over large JSON documents.

Json.NET 4.0 Release 3 changes the internal structure of the collection objects to eliminate linked lists – JArray now uses a list internally to store child values and JObject uses a combination of a list and dictionary, letting it preserve property order and make property name lookups O(1).

I'll have to read Marge's book, and I swore never to read again after To Kill a Mockingbird gave me no useful advice on killing mockingbirds. It did teach me not to judge a man based on the color of his skin, but what good does that do me?

From your perspective the API for LINQ to JSON is exactly the same, it is just faster.

Assembly file names

One thing to note in this release is that to play nicely with NuGet the different versions of Json.NET now all use the same assembly file name: Newtonsoft.Json.dll

If you want to find out exactly which version of the dll you are using the Windows file properties dialog display’s a .NET assembly’s title as the File description.

Oh no, the Pawnee have returned. They probably want their souls back.

Changes

Here is a complete list of what has changed since Json.NET 4.0 Release 2.

  • New feature - Improved support for deserializing objects using non-default constructors
  • New feature - JsonConverterAttribute now allowed on constructor parameters
  • New feature - JsonPropertyAttribute now allowed on constructor parameters
  • New feature - Added Serializable attribute to exceptions
  • New feature - JsonObject and JsonProperty attributes can now be placed on an interface and used when serializing implementing objects
  • New feature - Improve TypeNameHandling.Auto to skip adding an unneeded type name for collection interfaces
  • New feature - LINQ to JSON internals rewritten to use lists and dictionaries rather than linked lists
  • New feature - Added support for deserializing to readonly collections and dictionaries on classes with non-default constructors
  • New feature - Added serialization constructors to all Exceptions
  • New feature - Added support for serialization event attributes on base classes
  • New feature - Added support for BindToName on SerializationBinder
  • New feature - Missing JSON errors can now be handled using JsonSerializer error handling
  • New feature - Added Populate and IgnoreAndPopulate options to DefaultValueHandling for automatically populating default values during deserialization
  • New feature - Added support for setting readonly fields when marked up with JsonPropertyAttribute
  • New feature - Added Order to JsonPropertyAttribute to override the order of serialized JSON
  • New feature - Added Culture to JsonTextReader to use when converting values from JSON text
  • New feature - Added support for reading byte arrays from JSON integer arrays
  • New feature - Added support for deserializing IDictionary properties
  • New feature - Added ToObject to JToken for deserializing LINQ to JSON objects to a .NET object
  • New feature - Added support for Guid, TimeSpan and Uri to LINQ to JSON
  • Change - Changed WriteEndObject, WriteEndArray, WriteEndConstructor on JsonWriter to be virtual
  • Change - Moved JPropertyDescriptor to Newtonsoft.Json.Linq namespace
  • Change - Additional content after the $ref property now ignored when reading schema references
  • Change - Changed JToken.Children to return an empty iterator rather than erroring
  • Change - Changed the assembly file names to all be Newtonsoft.Json.dll to fix NuGet referencing
  • Change - Changed $id and $ref properties to allow null
  • Fix - Changed .NET 2.0 version to use LinqBridge source code rather than ilmerge to fix error
  • Fix - Fixed deserializing to IEnumerable<T> properties
  • Fix - Fixed DataTable and DataColumn names not being modified by CamelCasePropertyNamesContractResolver
  • Fix - Fixed JObject loading JSON with comments
  • Fix - Fixed error when using a Specified property with no setter
  • Fix - Fixed transient constructor error on Windows Phone 7
  • Fix - Fixed deserializing null values into nullable generic dictionaries
  • Fix - Fixed potential casting error when writing JSON using a JsonReader
  • Fix - Fixed converting emtpy XML elements with an array attribute not writing other attributes
  • Fix - Fixed deserializing null values into DataTables
  • Fix - Fixed error when deserializing readonly IEnumerable<T> array properties
  • Fix - Fixed not including type name for byte[] values
  • Fix - Fixed BsonWriter failing silently when writing values outside of an Object or Array
  • Fix - Fixed serializer attempting to use dynamic code generation in partial trust
  • Fix - Fixed serializing objects with DataContract and DataMember attributes on base classes

Links

Json.NET CodePlex Project

Json.NET 4.0 Release 3 Download – Json.NET source code, documentation and binaries

Json.NET 4.0 Release 2 – NuGet 1.2 and Dynamic

This release builds on two areas introduced in Json.NET 4.0: NuGet and .NET 4 dynamic support.

NuGet 1.2

I understand. Let us celebrate our agreement with the adding of chocolate to milk. NuGet is fast becoming the preferred way to install libraries in .NET. NuGet 1.2 was recently released and among other improvements it adds support for the Windows Phone.

Json.NET 4.0 Release 2 has been updated to support NuGet 1.2 and Windows Phone – there is nothing you need to do, it should just work. Windows Phone brings Json.NET’s NuGet support up to 7 different frameworks:

  • .NET 4
  • .NET 4 Client Profile
  • .NET 3.5
  • .NET 3.5 Client Profile
  • .NET 2
  • Silverlight 4
  • Windows Phone

Dynamic Improvements

Feedback on the initial release of dynamic support in Json.NET 4.0 was that values didn’t behave as expected. Concatenating two integers would throw an exception for example – less than ideal.

Release 2 adds support for common value operations: comparing, adding, subtracting, multiplying, dividing, incrementing and decrementing all now work like they should.

dynamic product = new JObject();
product.ProductName = "Elbow Grease";
product.Enabled = true;
product.Price = 4.90m;
product.StockCount = 9000;
product.StockValue = 44100;
 
// All Elbow Grease must go sale!
// 50% off price
 
product.Price = product.Price / 2;
product.StockValue = product.StockCount * product.Price;
product.ProductName = product.ProductName + " (SALE)";
 
string json = product.ToString();
// {
//   "ProductName": "Elbow Grease (SALE)",
//   "Enabled": true,
//   "Price": 2.45,
//   "StockCount": 9000,
//   "StockValue": 22050.0
// }

And More

Like always many smaller features has been added (EnumMemberAttribute serializer support for example) and bugs have been fixed.

Changes

Here is a complete list of what has changed since Json.NET 4.0 Release 1.

  • New feature - Added commonly used primitive value operations to dynamic JValue
  • New feature - Added IComparable to JValue
  • New feature - Added JsonConstructorAttribute to explicitly define which constructor to use during deserialization
  • New feature - Added IFormattable to JValue
  • New feature - Added Load and Parse helper methods to JToken
  • New feature - Added ExpandoObjectConverter that deserializes to primitive values rather than LINQ to JSON objects for ExpandObjects
  • New feature - Added EnumMemberAttribute support to StringEnumConverter
  • New feature - Added CloseInput/CloseOutput to JsonReader/JsonWriter to control whether the underlying stream is closed. Default to true
  • Change - JValue ToString now calls ToString on the internal value
  • Change - NuGet spec file includes client profiles
  • Change - BsonReader/BsonWriter close the underlying stream by default when closed
  • Change - CamelCasePropertyNamesContractResolver updates dictionary and dynamic property names
  • Fix – DefaultValueHandling no longer includes a property when the default value is the same value but a different type
  • Fix – Objects created with paramatized constructors fixed to not set properties that aren't in JSON
  • Fix – Fixed comments in JSON sometimes causing errors when deserializing
  • Fix – Error when reflecting over type that has multiple index properties inherited fixed
  • Fix – Error when reusing JsonSerializer with preserve references enabled fixed
  • Fix – Poor error message when dynamic type cannot be created during deserialization fixed
  • Fix – Error when setting a null value on a dynamic type during deserialization fixed
  • Fix – JsonTextReader.ReadAsDecimal not attempting to convert string values to decimal fixed
  • Fix – Duplicate type name within an assembly error fixed
  • Fix – Deserializing a DataTable property fixed
  • Fix – Code emit error when deserializing interfaces in certain situations fixed
  • Fix – Performance issues when deserializing with partial type names fixed
  • Fix – Deserializing some decimal values on classes with non-default constructors fixed

Links

Json.NET CodePlex Project

Json.NET 4.0 Release 2 Download – Json.NET source code, documentation and binaries