BindingFailure in deserializing via JSON.Net 4.0 r3

I am trying to deserialize a JSON string that was serialized by JSON.Net 4.0 r3 itself. The settings are the same for serialization and deserialization. The following exception occurs:

Could not find type 'System.Collections.Generic.List`1[[System.Drawing.PointF, System.Drawing]]' in assembly '"mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089"'

After turning on "stop when BindingFailure is thrown", I see the problem is in the following part (JSON.Net source code, DefaultSerializationBinder.cs):

  if (assemblyName != null)
    Assembly assembly;

#if !SILVERLIGHT && !PocketPC
    // look, I don't like using obsolete methods as much as you do but this is the only way
    // Assembly.Load won't check the GAC for a partial name
#pragma warning disable 618,612
    assembly = Assembly.LoadWithPartialName(assemblyName);
#pragma warning restore 618,612
    assembly = Assembly.Load(assemblyName);

    if (assembly == null)
      throw new JsonSerializationException("Could not load assembly '{0}'.".FormatWith(CultureInfo.InvariantCulture, assemblyName));

    Type type = assembly.GetType(typeName); // BindingFailure here
    if (type == null)
      throw new JsonSerializationException("Could not find type '{0}' in assembly '{1}'.".FormatWith(CultureInfo.InvariantCulture, typeName, assembly.FullName));

The error shown at the point of BindingFailure is

The assembly with display name 'System.Drawing' failed to load in the 'LoadFrom' binding context of the AppDomain with ID 1. The cause of the failure was: System.IO.FileNotFoundException: Could not load file or assembly 'System.Drawing' or one of its dependencies. The system cannot find the file specified.

The operating system is Windows 7, 64-bit. I am using Visual Studio 2010 and targeting this application for Framework v4. The target is not Silverlight or PocketPC.

Why would it fail to load "System.Drawing" in this case? Where should I start investigating whether this is a JSON.Net issue or a problem with my Framework 4.0 installation?

Thanks in advance.


It turns out the issue is related to JSON.Net. I used to use the following setting

jsonSerializerSettings = new JsonSerializerSettings
     TypeNameHandling = TypeNameHandling.All

This was because of a suggestion in a post (that involved deserializing generic collections like List). It was suggested that full type information would help with JSon.Net errors about the "Cannot instantiate abstract base classes" error message.

If I change it to

jsonSerializerSettings = new JsonSerializerSettings
     TypeNameHandling = TypeNameHandling.Auto

it works fine. Interestingly, some type information does not get emitted, I do not get the BindingFailure and the deserialization works fine. I have no idea why it did not work with "Auto" in the first place.

Need Your Help

Filter/Sort user comments PHP


I'm a php newb so you'll have to bear with me. I'm working on a user comment section where people can submit condition/snow reports for some backcountry cabins. I've got the submission and display ...

Using Linq to find a group of common objects

c# linq

What I'm trying to do is, if you look below in the code, write a Linq statement that will return the group of ShiftEvents. A group is defined by 1.) EmpId, 2.) EndDate/Time of one ShiftEvent =

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.