Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserializing a list of interfaces with a custom JsonConverter?

I have a List<ISomething> in a json file and I can't find an easy way to deserialize it without using TypeNameHandling.All (which I don't want / can't use because the JSON files are hand-written).

Is there a way to apply the attribute [JsonConverter(typeof(MyConverter))] to members of the list instead to the list?

{
    "Size": { "Width": 100, "Height": 50 },
    "Shapes": [
        { "Width": 10, "Height": 10 },
        { "Path": "foo.bar" },
        { "Width": 5, "Height": 2.5 },
        { "Width": 4, "Height": 3 },
    ]
}

In this case, Shapes is a List<IShape> where IShape is an interface with these two implementors: ShapeRect and ShapeDxf.

I've already created a JsonConverter subclass which loads the item as a JObject and then checks which real class to load given the presence or not of the property Path:

var jsonObject = JObject.Load(reader);

bool isCustom = jsonObject
    .Properties()
    .Any(x => x.Name == "Path");

IShape sh;
if(isCustom)
{
    sh = new ShapeDxf();
}
else
{
    sh = new ShapeRect();
}

serializer.Populate(jsonObject.CreateReader(), sh);
return sh;

How can I apply this JsonConverter to a list?

Thanks.

like image 786
TesX Avatar asked Dec 05 '16 15:12

TesX


People also ask

What is JsonConverter?

Converts an object to and from JSON. Newtonsoft.Json.


1 Answers

In your class, you can mark your list with a JsonProperty attribute and specify your converter with the ItemConverterType parameter:

class Foo
{
    public Size Size { get; set; }

    [JsonProperty(ItemConverterType = typeof(MyConverter))]        
    public List<IShape> Shapes { get; set; }
}

Alternatively, you can pass an instance of your converter to JsonConvert.DeserializeObject, assuming you have implemented CanConvert such that it returns true when objectType == typeof(IShape). Json.Net will then apply the converter to the items of the list.

like image 103
Brian Rogers Avatar answered Nov 14 '22 22:11

Brian Rogers