Setting up a JSON reader, I have a get-set class as a base to deserialize objects into lists. However I’ve run into a problem, where I need one list to be able to change its type. For example:
public class SomewhereInAJsonSubLevel
{
public string name { get; set; }
public int num { get; set; }
public string method { get; set; }
public List<(Of type)> content { get; set; }
}
The List is of custom types, and it can vary for this level. But to put it simply, it can either be of INT
, STRING
, or CUSTOM_CLASS
. Each one obviously has a different list layout.
With it being a JSON, my experience so far has been that the list type is set as a concrete type above.
In theory, the type would relate to the method, so the question is then:
is there a way to keep the above get-set structure, but have a way to do a “if Method name is X, then List<Int>
content, or if Method type is Y, List<Custom_Class>
content”
Edit: So to clear things up, I am not making a JSON Reader, I’m using NewtonSoft’s JSON to serialize/de-serialize. This class example would be located somewhere a few layers deep.
I could make identical setups for each separate list type, but that would mean copying all the levels above, just to change one of these designations 5 or 6 layers deep, since each layer would then have to be changed for specific class. It’s a lot of duplicate code, which I want to avoid.
So I guess what I was looking for, but couldn’t find the word for, is polymorphism in a way, for the one specific item that will vary its List<Type>
in this specific get-set class.
5