I asked myself this question many times and could not come up with a solution.
Suppose there is some kind of warehouse management software that displays all stored products as a json file. Products may be stored in general form or in a more specific form.
For example, there are two types of products:
General product: { "pid": 12345, "name": "SomeGeneralProduct" } Special product: { "pid": 67890, "name": "SpecialProduct", "color": "green" }
There may be a few more options, but all have pid and name . Now I want to analyze and analyze a whole bunch of products that I get as a json file.
I could just go and create a Product class that contains all the possible members that a product may have, but I think this is a waste of time / maintainability and can be solved in a more OOP way. The only problem is that I could not find anything about it yet.
What I really would like to have at the end looks 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 that the example I would like to have is too simple, but I just want to make it clear that I don't want to have tons of if / else or switch inside my main code, if possible)
I would like to know how can I solve this, if possible?
Is there a library that can handle this or what would be a good way to handle this?
source share