Newtonsoft JSON Interface Serialization error
- by Ben
I am using C# .NET 4.0, Newtonsoft JSON 4.5.0.
public class Recipe
{
[JsonProperty(TypeNameHandling = TypeNameHandling.All)]
public List<IFood> Foods{ get; set; }
...
}
I want to serialize and deserialize this Recipe object. If I serialize and deserialize the object during application lifetime this succeeds, but if I serialize the object, exit application and then deserialize it then it throws an exception, that it cannot instantiate IFood (since it is an interface).
The problem is that it does not serialize the implementation of interface.
"$type": "System.Collections.Generic.List`1[[NSM.Shared.Models.IFood, NSMShared]], mscorlib"
I tried using TypeNameHandling.Object and Array and Auto, but it didn't help. Is there any way to serialize it properly? Or at least to define the class mapping before deserializing?
EDIT:
I am using JSON coupled with Hammock ( http://code.google.com/p/relax-net/ ), C# driver for CouchDB, which internally serializes and deserializes objects. As mentioned the problem is that it does not serialize the interface implementation.