Json.net deserialized nested dictionary

I am using Json.NET to deserialize an object that includes a nested dictionary. Here is an example of what I'm trying to do

public interface IInterface
{
    String Name { get; set; }
}

public class AClass : IInterface
{
    public string Name { get; set; }
}

public class Container
{
    public Dictionary<IInterface, string> Map { get; set; }
    public Container()
    {
        Map = new Dictionary<IInterface, string>();
    }
}


public static void Main(string[] args)
    {
        var container = new Container();
        container.Map.Add(new AClass()
        {
            Name = "Hello World"
        }, "Hello Again");
        var settings = new JsonSerializerSettings
        {
            TypeNameHandling = TypeNameHandling.Objects,
            PreserveReferencesHandling = PreserveReferencesHandling.All,

        };


        string jsonString = JsonConvert.SerializeObject(container, Formatting.Indented, settings);
        var newContainer = JsonConvert.DeserializeObject<Container>(jsonString);
    }

I received an exception message: Could not convert the string "ConsoleApplication1.AClass" to the dictionary type "ConsoleApplication1.IInterface". Create a TypeConverter to convert from a string to a key type object. My apologies, but I can not find a way to de-serialize the interface in the dictionary.

Thank you in advance

+3
source share
1 answer

, JSON () , Json.Net Json ( ToString()), . -, JsonArray.

. .

+4

All Articles