First of all C# doesn’t deserialize json strings. C# can be used to invoke libraries which do.
Based on pure guesses I would go ahead and say you’re maybe using Newtonsoft.Json
to do the deserializing although you might very well be using something else. Let’s take a step back.
If that were the case, then “getting rid” of List<object>
would be your smallest concern. Actually being able to come up with instances which aren’t just empty, plain’ol System.Object
instances would be your first challenge. That’s simply because telling the deserializer what your super (base) type is won’t even make them go look for possible subtypes since the subtypes of any type is an open set (anyone can take and reference your assembly and extend your class without you feeling anything).
Having a list of references of type System.Object
each pointing towards instancesof type MyNamespace.Group
or MyNamespace.Condition
might look like a drag until you realize that having instances of MyNamespace.Group
and the other is the actual challenge in this particular use case you’re describing.
Taking another step back, in OOP, whenever you want to store a list of possibly different types of instances and your language is strongly typed and provides some sort of generics (C# is included), you cannot have a generic argument for element type which is not implicitly assignable from each and every one of the specific types you want your instances to potentially be. In your case, object
is compatible with more than Group
and Condition
. If that is your sole problem then you should simply create a 3rd class (let’s call it EitherGroupOrCondition
) and make that the root of a naturally occurring class hierarchy (both Group
and Condition
should now extend EitherGroupOrCondition
). Then you could turn your List<object>
into a List<EitherGroupOrCondition>
.
Going back to deserializing: You would need to look at JsonSerializerSettings.TypeNameHandling
and it’s entourage of features.
You should consider the following prerequisites to achieving your goal:
- understanding OOP thoroughly
- understanding your particular Json deserializer’s capabilities (hopefully you’re using
Newtonsoft.Json
)
Both of these prerequisites take time and patience and I encourage you to go ahead and fill them and never work blindly, without understanding at least the proposed product surface of that which you are using.