I have the following line of source code in order to deserialise a JSON string:
List<I_Interface> list =
JsonConvert.DeserializeObject<List<I_Interface>>(input_JSON);
I_Interface
inherits of the INotification
interface.
I have different implementations for the I_Interface
with different rules:
- In case of the presence of the fields
"action"
and"quantity"
, I want the object to be apublic class Jobs:INotification
- In case of the presence of a
"zone"
field, I want the object to be apublic class Engine_Zone : INotification
- ...
Is there a system I can use for choosing the deserialiser to create the right type of class?
I've been looking for a JsonSerializerSettings.Binder
, but the type is not described on the official website.
What I'm looking for is something like:
Serialisation_Binder.add(
new object{TypeName="Jobs",
new Mandatory_Property("action"),
new Mandatory_Property("quantity")
}
);
Serialisation_Binder.add(
new object{TypeName="Engine_Zone",
new Mandatory_Property("zone")
}
);
input_JSON
looks as follows:
[{"action": 1, "quantity" : 20},
{"action": 2, "quantity" : 26},
{"zone": 1}
]
JsonConvert.DeserializeObject<List<I_Interface>>(input_JSON, Serialisation_Binder);
=> The last line creates a List<I_Interface>
with following entries:
ClassName : Jobs, action=[1], quantity=[20]
ClassName : Jobs, action=[2], quantity=[26]
ClassName : Engine_Zone, zone=[1]