Newtonsoft JSON Interface Serialization error
        Posted  
        
            by 
                Ben
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Ben
        
        
        
        Published on 2012-09-07T20:39:10Z
        Indexed on 
            2012/09/07
            21:38 UTC
        
        
        Read the original article
        Hit count: 517
        
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.
© Stack Overflow or respective owner