Json.NET 3.5 Release 5 – .NET 2.0 Support, Error Handling & Databinding

.NET 2.0 Support

Json.NET now has .NET 2.0 support! The great thing about 2.0 support is Json.NET 1.3.1 can finally be retired. Going on 2 years old now, 1.3.1 is chock full of bugs that I have fastidiously ignored. Upgrade recommended!

If you’re interested in the details I have previously blogged about adding .NET 2.0 support to Json.NET here.

Error Handling

Json.NET now has an Error event on the JsonSerializer and an OnErrorAttribute for handling serialization errors. When an error occurs you can access the exception, see details about the object and member the error occurred on and choose between setting the error as handled and letting it be thrown to your application.

List<string> errors = new List<string>();
List<DateTime> c = JsonConvert.DeserializeObject<List<DateTime>>(@"[
  ""I am not a date and will error!"",
  new JsonSerializerSettings
      Error = delegate(object sender, ErrorEventArgs args)
          args.ErrorContext.Handled = true;
      Converters = { new IsoDateTimeConverter() }
// 2009-09-09T00:00:00Z
// 1977-02-20T00:00:00Z
// 2000-12-01T00:00:00Z
// The string was not recognized as a valid DateTime. There is a unknown word starting at index 0.
// Unexpected token parsing date. Expected String, got StartArray.
// Cannot convert null value to System.DateTime.

In this example we are deserializing a JSON array to a collection of DateTimes. On the JsonSerializerSettings a handler has been assigned to the Error event which will log the error message and mark the error as handled.

The result of deserializing the JSON is three successfully deserialized dates and three error messages: one for the badly formatted string, "I am not a date and will error!", one for the nested JSON array and one for the null value since the list doesn't allow nullable DateTimes. The event handler has logged these messages and Json.NET has continued on deserializing the JSON because the errors were marked as handled.

Find more documentation about error handling here.


Lots of small additions have been made to LINQ to JSON to enable easy databinding.

  • Added type descriptor objects under the Newtonsoft.Json.Linq.ComponentModel namespace
  • Added ITypedList and IBindingList implementations to JContainer
  • Added INotifyPropertyChanging and INotifyPropertyChanged implementations to JObject

As someone who is traditionally a web programmer it has been really interesting looking into the world of WPF databinding in more detail.

Release vs Beta

Finally one a minor detail the eagle eyed might have already noticed: from now on Json.NET updates are going to be called Releases rather than Betas.


Here is a complete list of what has changed since Json.NET 3.5 Beta 4.

  • New feature - Added .NET 2.0 support
  • New feature - Added serialization error handling via Error event on JsonSerializer and OnErrorAttribute
  • New feature - Added implicit conversions from primitive values to JValue
  • New feature - Added type descriptor objects to support LINQ to JSON databinding
  • New feature - Added ITypedList and IBindingList to JContainer
  • New feature - Added INotifyPropertyChanging and INotifyPropertyChanged to JObject
  • New feature - Added ConstructorHandling to allow object creation using non-public default constructors
  • New feature - Added roundtrip ADO.NET Entities serialization and deserialization
  • New feature - Added set indexer to JToken
  • Change - IsRequired now accepts null properties
  • Change - JsonSerializer now creates JObject/JArray when deserializing complex JSON to an object type
  • Change - Empty string to be deserialized as null for nullable types
  • Change - CloneNode on JToken renamed to CloneToken
  • Change - JProperty constructor that doesn't take a value made internal
  • Fix - Schema generation with negative enum values
  • Fix - Exception when loading JObject with multiple properties of the same name. Last property will be used
  • Fix - Exception when deserializing null value to List<Nullable<T>>
  • Fix - Generic type deserialization with type name tracking enabled


Json.NET CodePlex Project

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

Sunday Podcasts 3

Homer, lighten up! You're making happy hour bitterly ironic.

The Penny Arcade PodcastFor The Vin

The authors of the comic website Penny Arcade record their thought process as they write a comic strip.

In this episode they discuss their experiences of being molested at Disneyland by costumed characters, Swedes doing time in the big house for IP infringement, fantasy books and recruiting Vin Diesel into their DnD party.

No TV, no beer make Homer something something.

This American LifeThe Fix Is In

Corporate crime is fascinating. Your average burglar isn’t the sharpest tool in the shed and neither is the crime he commits so it is interesting to see what happens when smart people decide that the law doesn’t apply to them. Examples: Enron scandal, Madoff’s $65 billion Ponzi scheme.

The Fix Is In is a rerun show from 2000 about a global price fixing conspiracy and the executive, Mark Whitacre, who cooperated with the FBI in exposing it. Whitacre goes to amazing lengths to gather evidence and was described by the FBI as the most cooperative witness in their history. A film has even been made about his exploits: The Informant! – appropriately a comedy.


TechEd 2009 New Zealand In A NutShell

Last night I co-presented an Intergen Twilight at our Wellington office, talking to clients about TechEd this year. My partner in crime for the talk was Daniel Horwood who was the man behind the Hands on Labs at TechEd this year.

I think it went really well and we got a lot of good feedback talking to customers after the presentation.

Download the slides of our talk here (30megs).

It may be on a lousy channel, but The Simpsons are on TV!