I have to consume a Rest API where the content has variable structure in a child list ..
Here is a simplified representation that a Small Car has several components:- Window, Engine and Door
{
"ProductCode": 1234,
"ProductName": "SmallCar",
"Components": [
{
"PartType": "Window",
"Operation": "Electric",
"Tinted": true,
},
{
"PartType": "Engine",
"Capacity": 2000,
"Cylinders": 6,
"Layout": "V6"
},
{
"PartType": "Door",
"DoorStyle": "Standard"
}
]
}
My POCO classes are defined as below:-
public class Product
{
public int ProductCode {get; set;}
public string ProductName {get; set;}
public List<Component> Components {get; set;}
}
public class Component
{
public string PartType {get; set;}
}
public class Window : Component
{
public string Operation {get; set;}
public bool Tinted {get; set;}
}
public class Engine : Component
{
public int Capacity {get; set;}
public int Cylinders {get; set;}
public string Layout {get; set;}
}
public class Door : Component
{
public string DoorStyle {get; set;}
}
During the deserialization, how can I get it to create the appropriate component object (Window, Door, Engine). With var product = DeserializeObject<Product>(....)
I just get a list of Components
with PartType
set, but I need the list to be of each class type according to the PartType