I asked myself this question many times and could not come up with a solution.
Lets assume there is some sort of warehouse management software that outputs all saved products as a json file. Products can be saved in a general form or a more specific form.
For example there are two sorts of products:
General product: { "pid": 12345, "name": "SomeGeneralProduct" }
Special product: { "pid": 67890, "name": "SpecialProduct", "color": "green" }
There might be some more variations but all have pid
and name
in common.
Now I want to parse and analyse a whole bunch of products that I get as a json file.
I could just go and create a class Product
that contains all possible members a product could possibly have but I think that's a waste of time/maintainability and could be solved in a more OOP way. The only problem is that I could not find anything about this topic so far.
What I really would like to have in the end is something like this:
class BaseClass
{
public int Pid { get; set; }
public string Name { get; set; }
}
class DerivedClass : BaseClass
{
public string Color { get; set; }
}
string jsonProducts = "JSON-DATA-OF-PRODUCTS";
List<BaseClass> products = SomeDeserializer.Deserialize(jsonProducts);
(Note: I know the example what I would like to have is too simple but I just want to make clear that I don't want to have tons of if/else or switches inside my main code if possible)
I would like to know how I can solve this if possible?
Is there any library that can handle this or what would be a good way to tackle this?