Deserialization by JSON.NET of a list of interfaces inside another class

I already surfed thorough other questions, but I can't solve this.

The JSON is the following

[
    {
        "$type": "UWP.Model.Management.VirtualCarPark.Item.AggregatorItem, UWP.Model",
        "UId": 17,
        "Name": "K2 BDB-INCON4-U",
        "IsPublished": true,
        "Items": [{
                "$type": "UWP.Model.Management.VirtualCarPark.Item.Item, UWP.Model",
                "IsPublished": true,
                "UId": 18,
                "OwnerUId": 17,
                "Name": "1: Root - I/O Modules K2 Switch 1",
                "UIdStreet": 1,
                "UIdArea": 1
            },
            {
                "$type": "UWP.Model.Management.VirtualCarPark.Item.Item, UWP.Model",
                "IsPublished": true,
                "UId": 19,
                "OwnerUId": 17,
                "Name": "2: Root - I/O Modules K2 Switch 2",
                "UIdStreet": 1,
                "UIdArea": 1
            }
        ]
    },
    {
        "$type": "UWP.Model.Management.VirtualCarPark.Item.AggregatorItem, UWP.Model",
        "UId": 41,
        "Name": "K3 SH2INDI424",
        "IsPublished": true,
        "Items": [{
                "$type": "UWP.Model.Management.VirtualCarPark.Item.Item, UWP.Model",
                "IsPublished": true,
                "UId": 42,
                "OwnerUId": 41,
                "Name": "1: Root - Switches K3 Switch 1",
                "UIdStreet": 1,
                "UIdArea": 1
            },
            {
                "$type": "UWP.Model.Management.VirtualCarPark.Item.Item, UWP.Model",
                "IsPublished": true,
                "UId": 43,
                "OwnerUId": 41,
                "Name": "2: Root - Switches K3 Switch 2",
                "UIdStreet": 1,
                "UIdArea": 1
            },
            {
                "$type": "UWP.Model.Management.VirtualCarPark.Item.Item, UWP.Model",
                "IsPublished": true,
                "UId": 44,
                "OwnerUId": 41,
                "Name": "3: Root - Switches K3 Switch 3",
                "UIdStreet": 1,
                "UIdArea": 1
            },
            {
                "$type": "UWP.Model.Management.VirtualCarPark.Item.Item, UWP.Model",
                "IsPublished": true,
                "UId": 45,
                "OwnerUId": 41,
                "Name": "4: Root - Switches K3 Switch 4",
                "UIdStreet": 1,
                "UIdArea": 1
            }
        ]
    }
]

As you can see I put the type thanks

TypeNameHandling = TypeNameHandling.Auto

So, I can't understand why I can't deserialize as the following

var lstAggregatorItem = JsonConvert.DeserializeObject<List<AggregatorItem>>(tokenAggregators.ToString());

The class AggregatorItem has only interfaces, none implementation of Item since I want to work only with interfaces and no direct implementation

public class AggregatorItem : IAggregatorItem
{
    public int UId { get; set; }
    public string Name { get; set; }

    public bool IsPublished
    {
        get { return Items.Any(i => i.IsPublished); }
    }
    
    public AggregatorItem()
    {
        Items = new List<IItem>();
    }

    public IList<IItem> Items { get; set; }

    public IItem GetItem(int itemUId)
    {
        return Items.SingleOrDefault(i => i.UId == itemUId);
    }

    public void Remove(int itemUId)
    {
        var itemToRemove = Items.SingleOrDefault(i => i.UId == itemUId);

        if (itemToRemove == null)
            return;

        itemToRemove.Reset();
        Items.Remove(itemToRemove);
    }

    public void AddItem(IItem item)
    {
        Items.Add(item);
    }

    public void Reset()
    {
        foreach (var item in Items)
        {
            item.Reset();
        }

        Items.Clear();
    }
}

So, is there something to teach to serializer "use this implementation of this class"? Because this exception is annoying me

Newtonsoft.Json.JsonSerializationException: 'Could not create an instance of type UWP.Model.Management.VirtualCarPark.Item.IItem. Type is an interface or abstract class and cannot be instantiated. Path '[0].Items[0].IsPublished', line 10, position 22.



Read more here: https://stackoverflow.com/questions/67016000/deserialization-by-json-net-of-a-list-of-interfaces-inside-another-class

Content Attribution

This content was originally published by lunatic84 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: