© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
60 replies
Elio

✅ JSON Deserialize Derived Class

Hi, i'm struggling to deserialize my object with the Newtonsoft.Json Librairy. I've a collection of
Step
Step
named
Steps
Steps
which can be a
Stepspecial
Stepspecial
or
Stepnormal
Stepnormal
. I'm trying to write my JsonConverter to deserialize the collection of
Step
Step
but when i try to deserialize i keep getting the stack overflow error because jObject.ToObject call back
ReadJson
ReadJson
:
public class StepConverterJSON : JsonConverter<Step>
{
    #region Properties
    public override bool CanWrite => false;
    #endregion

    #region Override Methods
    public override void WriteJson(JsonWriter writer, Step? value, JsonSerializer serializer) { throw new NotImplementedException(); }

    public override Step? ReadJson(JsonReader reader, Type objectType, Step? existingValue, bool hasExistingValue, JsonSerializer serializer)
    {
        // Load a JObject from the JsonReader
        JObject jObject = JObject.Load(reader);

        if(jObject["Message"] != null)
        {
            // Use the new serializer to deserialize the object
            StepSpecial? stepSpecial = serializer.Deserialize<StepSpecial>(reader);
            return stepSpecial;
        }
        else
        {
            StepNormal? stepNormal = jObject.ToObject<StepNormal>(serializer);
            //StepNormal? stepNormal = serializer.Deserialize<StepNormal>(reader);
            return stepNormal;
        }
    }
    #endregion
}
public class StepConverterJSON : JsonConverter<Step>
{
    #region Properties
    public override bool CanWrite => false;
    #endregion

    #region Override Methods
    public override void WriteJson(JsonWriter writer, Step? value, JsonSerializer serializer) { throw new NotImplementedException(); }

    public override Step? ReadJson(JsonReader reader, Type objectType, Step? existingValue, bool hasExistingValue, JsonSerializer serializer)
    {
        // Load a JObject from the JsonReader
        JObject jObject = JObject.Load(reader);

        if(jObject["Message"] != null)
        {
            // Use the new serializer to deserialize the object
            StepSpecial? stepSpecial = serializer.Deserialize<StepSpecial>(reader);
            return stepSpecial;
        }
        else
        {
            StepNormal? stepNormal = jObject.ToObject<StepNormal>(serializer);
            //StepNormal? stepNormal = serializer.Deserialize<StepNormal>(reader);
            return stepNormal;
        }
    }
    #endregion
}

Any idea how to solve this and write a good
ReadJson
ReadJson
method ? i've thought about the
populate
populate
method but not sure this is the good way of doing it.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

JSON Deserialize
C#CC# / help
2y ago
JSON deserialize
C#CC# / help
2y ago
Serializing a derived class with System.Text.Json
C#CC# / help
3y ago
Derived class , BaseClass, Dictionary
C#CC# / help
2y ago