0

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 a public class Jobs:INotification
  • In case of the presence of a "zone" field, I want the object to be a public 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]
Dominique
  • 16,450
  • 15
  • 56
  • 112
  • To deserialise to an interface, you either need to use `TypeNameHandling` in the settings when serialising (which is often a really bad idea), or make a custom converter. – DavidG Apr 28 '23 at 12:15
  • I don't want to deserialise into an interface, but into an object, implementing that interface. The problem is that the type of that object , its class, should be retrieved from the different fields in the JSON string, and I don't know how to bind JSON fields to a class. – Dominique Apr 28 '23 at 12:18
  • Well yes, my comment is accurate, to clarify feel free to replace "interface" with "object that implements an interface". That is implied by the fact you can't do it any other way. – DavidG Apr 28 '23 at 12:19
  • https://stackoverflow.com/questions/50613560/how-to-deserialize-interfaces-with-newtonsoft-json-net – DavidG Apr 28 '23 at 12:19
  • @DavidG: your example mentions different classes, all of them implementing a different interface. In my question, there are different class, all of them implementing the same interface. – Dominique Apr 28 '23 at 12:26
  • It doesn't matter, the same principle applies – DavidG Apr 28 '23 at 12:27
  • 1
    If it helps, here's an answer I wrote that might help https://stackoverflow.com/questions/69633110/deserialization-of-json-to-list-of-interface-with-generic-type-parameter/69633716#69633716 – DavidG Apr 28 '23 at 12:38
  • Binders are only useful when you have a ["$type"](https://www.newtonsoft.com/json/help/html/serializetypenamehandling.htm) property in your JSON, which you do not... – dbc Apr 28 '23 at 15:16
  • 1
    Absent that you will need to create a custom JsonConverter. To do that see Please see [Deserializing polymorphic json classes without type information using json.net](https://stackoverflow.com/q/19307752), [Json.Net Serialization of Type with Polymorphic Child Object](https://stackoverflow.com/q/29528648) or [How to implement custom JsonConverter in JSON.NET to deserialize a List of base class objects?](https://stackoverflow.com/q/8030538). – dbc Apr 28 '23 at 15:16

0 Answers0