0

The json structures come over from network like this:

{
  type: "a",
  specialPropA: "propA"
}
{
  type: "b",
  specialPropB: 1
}

I am trying to deserialize them as top level objects with a custom JsonConverter. I have the following hierarchy and annotate the interface with a custom converter. In the client I use IBase as a type paratemer for the a deserializer helper method.

[JsonConverter(typeof(MyJsonConverter))]
public interface IBase 
{
  string type { get; }
}
public class DerivedA : IBase
{
  string type => "A";

  string specialPropA { get; set; }
}
public class DerivedB : IBase
{
  string type => "B";

  int specialPropB { get; set; }
}

Then the implementation of the JsonConverter.

public class MyJsonConverter
{
   public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
   {
        JObject jsonObject = JObject.Load(reader);
        if (type == 'A') return JObject.toObject<DerivedA>();
        else return JObject.toObject<DerivedB>();
   }
}

However, it seems that I get stack overflow. The custom deseriazer seems to run over and over again. Is there a work-around?

2

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.