2

I'm trying to (de)serialize a class with this simple piece of code:-

var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All };

// Serialize
var json = JsonConvert.SerializeObject(obj, settings);

// Deseralize - this is where it fails
var test = JsonConvert.DeserializeObject<MyObject>(json, settings);

DeseralizeObject() fails with a JsonSerializationException:-

Error resolving type specified in JSON 'Xxx.Common.MyObject, Xxx.Common'. Path '$type', line 1, position 110

Inner exception: JsonSerializationException, message "Could not load assembly 'Xxx.Common".

I don't understand why it can't load the assembly - it's being referenced by this project!

It works if I don't use the JsonSerializerSettings, however I need this because the class being serialized will eventually have a List<SomeBaseClass> property that will contain derived types.

Any thoughts? I'm using Json.Net v6.0.

Edit:

I just tried adding TypeNameAssemblyFormat = FormatterAssemblyStyle.Full to my serializer settings, which resulted in this different (and confusing) exception message:-

Type specified in JSON 'Xxx.Common.MyObject, Xxx.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not compatible with 'Xxx.Common.MyObject, Xxx.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Path '$type', line 1, position 165.

Edit2:

To clarify things, the above code is a complete repro of the problem, and resides in a larger WPF application, while MyObject resides in a different project ("Xxx.Common") in the same solution, and is referenced by the WPF application project - I've simply replaced our company namespace with "Xxx" for this post.

MyObject is a simple POCO that I've created to rule out any issues that may be due to complex types, and consists of a few string and double properties, i.e.

public class MyObject
{
    public string Name {get;set;}
    public double Foo {get;set;}
    ...
}

The serialized JSON looks like this (again company NS replaced) - the pertinent part (i.e. the "$type") appears to be correct:-

{"$type":"Xxx.Common.MyObject, Xxx.Common","Name":null,"Foo":0.0,"StepSize":0.0,"Convergence":0.0,"Cutoff":0.0}

Andrew Stephens
  • 9,413
  • 6
  • 76
  • 152
  • Please show a short but *complete* program demonstrating the problem. – Jon Skeet Nov 05 '15 at 10:14
  • @JonSkeet please see Edit2 above. – Andrew Stephens Nov 05 '15 at 10:21
  • Providing full structure of `MyObject` and the serialized data `json` will be helpful. – Arghya C Nov 05 '15 at 10:24
  • 1
    You claim "the above code is a complete repro of the problem" - it's clearly not. It's a snippet of code. A complete repro would be something we could copy, paste, compile and run - albeit in this case using multiple assemblies. See http://stackoverflow.com/help/mcve – Jon Skeet Nov 05 '15 at 10:29
  • Are you [embedding DLLs](http://stackoverflow.com/questions/5934160/how-to-use-json-net-correctly-when-deseralizing-from-embedded-dll)? Is there a chance you have an old version of the DLL (possibly from a different format (x86 vs x64)) in your path somewhere? Could you have an old version of the DLL in the [GAC](http://stackoverflow.com/questions/8324829/old-dlls-in-gac-are-still-being-used)? See also [Old DLL file keeps being used](http://stackoverflow.com/questions/5575253/old-dll-file-keeps-being-used). – dbc Nov 05 '15 at 10:41
  • @dbc I've tried serializing a class that lives in the same project as the code, and still get the problem. Interestingly moving the code out into a standalone repro project works fine, so the failure is down to some outside (non-code) factor. The code in my WPF application resides in an assembly that gets loaded dynamically using MAF, so I'm now thinking this may have something to do with it. – Andrew Stephens Nov 05 '15 at 10:49
  • Json.NET's binder is [`DefaultSerializationBinder`](https://github.com/JamesNK/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/Serialization/DefaultSerializationBinder.cs). You could make a local copy, set it in [`JsonSerializerSettings.Binder`](http://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_JsonSerializerSettings_Binder.htm), and debug. It would appear two copies of the DLL are getting loaded. If you can create a full, reproducible example of the problem (including how you use MAF) we might be able to help more. – dbc Nov 05 '15 at 11:01

0 Answers0