4

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?

TorbenJ
  • 4,462
  • 11
  • 47
  • 84
  • 1
    If you are willing to try Json.NET, see [Deserializing polymorphic json classes without type information using json.net](http://stackoverflow.com/questions/19307752/deserializing-polymorphic-json-classes-without-type-information-using-json-net) or [Json.Net Serialization of Type with Polymorphic Child Object](http://stackoverflow.com/questions/29528648/json-net-serialization-of-type-with-polymorphic-child-object). – dbc Aug 12 '15 at 06:03
  • I'm already using Json.NET so this is no problem :) Thanks for the references I'll try them out. – TorbenJ Aug 12 '15 at 06:10

2 Answers2

2

Json .Net should work for you.

As above mentioned you can use the Method JsonConvert.DeserializeObject()

Just set the setting var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All };

And then deserialize like this:

List<Product> pList = JsonConvert.DeserializeObject<List<Product>>(jsonString, settings);
Boot750
  • 891
  • 1
  • 7
  • 17
  • 2
    This won't work in this case as I don't know the type of the entries and cannot influence the serialization of the products because I can't modify the software that delivers the json data. – TorbenJ Aug 12 '15 at 06:22
  • im sorry then, could you get it to work with the method of dbc? – Boot750 Aug 12 '15 at 07:39
0

I was looking for something similar, here is the solution I came out

  1. have a static dictionary in base class <subclass_identifier, Type>
  2. in each subclass, have a static constructor to insert subtype identifier and subtype
Simon
  • 1
  • 1