Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# .NET – Deserialize a derived class into a list of its Base

Im trying to deserialize a List of Holograms from a json file. The List of Holograms can contain HologramGroup, that is a derived class of Hologram. I’m trying to use a JsonConverter to do this. I want to check if a Hologram contains the property "holograms". If this is the case, this Hologram should be returned as HologramGroup.

The Converter:

public class MyJsonConverter : JsonConverter<Hologram>
    {
        public override Hologram ReadJson(JsonReader reader, Type objectType, Hologram existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            JObject root = JObject.Load(reader);

            if (root.TryGetValue("holograms", out var type))
            {
                return root.ToObject<HologramGroup>();
            }

            return root.ToObject<Hologram>();
        }

        public override void WriteJson(JsonWriter writer, Hologram value, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }
    }

Classes:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    [JsonConverter(typeof(ArJsonConverter))] //<- Im hoping to use the converter for all Holograms in my JSON. Not sure if this is the right approach.
    public class Hologram
    {
        public string HologramType { get; set; }
        public List<HologramProperty> Properties { get; set; }
    }


    public class HologramGroup : Hologram
    {
        public List<Hologram> Holograms { get; set; }
    }

    public class Scene
    {
        public string Name { get; set; }
        public List<Hologram> Holograms { get; set; }
    }

JSON to deserialize:

{
  "name": "MyScene",
  "holograms": [
    {
      "hologramType": "Video",    
      "properties": []
    },
    {
      "hologramType": "Picture",    
      "properties": []
    },
    {
      "hologramType": "HologramGroup",    
      "properties": [],
      "holograms": [
        {
          "hologramType": "Model3D",        
          "properties": []
        },
        {
          "hologramType": "Model3D",        
          "properties": []
        }     
      ]
    }
  ]
}

This code makes my Application crash and I’m not quite sure why. Polymorphism is quite new for me and I could use some help.

>Solution :

try this:

[JsonConverter(typeof(MyJsonConverter))]
public class Hologram
{
    public string HologramType { get; set; }
    public List<HologramProperty> Properties { get; set; }
}

public class HologramGroup : Hologram
{
    public List<Hologram> Holograms { get; set; }
}

public class Scene
{
    public string Name { get; set; }
    public List<Hologram> Holograms { get; set; }
}

public class MyJsonConverter : JsonConverter<Hologram>
{
    public override Hologram ReadJson(JsonReader reader, Type objectType, Hologram existingValue, bool hasExistingValue, JsonSerializer serializer)
    {
        JObject root = JObject.Load(reader);

        if (root.TryGetValue("holograms", out var _))
        {
            HologramGroup group = new HologramGroup();
            serializer.Populate(root.CreateReader(), group);
            return group;
        }

        Hologram hologram = new Hologram();
        serializer.Populate(root.CreateReader(), hologram);
        return hologram;
    }

    public override void WriteJson(JsonWriter writer, Hologram value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading