Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Two things you might try:</p> <p>Implement a try/parse model:</p> <pre><code>public class Organisation { public string Name { get; set; } [JsonConverter(typeof(RichDudeConverter))] public IPerson Owner { get; set; } } public interface IPerson { string Name { get; set; } } public class Tycoon : IPerson { public string Name { get; set; } } public class Magnate : IPerson { public string Name { get; set; } public string IndustryName { get; set; } } public class Heir: IPerson { public string Name { get; set; } public IPerson Benefactor { get; set; } } public class RichDudeConverter : JsonConverter { public override bool CanConvert(Type objectType) { return (objectType == typeof(IPerson)); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { // pseudo-code object richDude = serializer.Deserialize&lt;Heir&gt;(reader); if (richDude == null) { richDude = serializer.Deserialize&lt;Magnate&gt;(reader); } if (richDude == null) { richDude = serializer.Deserialize&lt;Tycoon&gt;(reader); } return richDude; } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { // Left as an exercise to the reader :) throw new NotImplementedException(); } } </code></pre> <p>Or, if you can do so in your object model, implement a concrete base class between IPerson and your leaf objects, and deserialize to it. </p> <p>The first can potentially fail at runtime, the second requires changes to your object model and homogenizes the output to the lowest common denominator.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload