0

I'm working on a project where I have to load files from a JSON file into objects. Let's say that I got the following classes :

  • Class Abstract : Areatype
  • Class : Restaurant
  • Class : Cinema
  • Class : Lobby

Every sub class has other properties, so I can't really figure out how to convert the loaded file into subclasses... I really don't want to handle this with switch/case or if/else statements because I want to make this dynamically as possible.

So my question is: how can I deserialize into this polymorphism structure?

halfer
  • 19,824
  • 17
  • 99
  • 186
M Yil
  • 877
  • 1
  • 14
  • 35
  • Don't you have one property which holds something that can be used to identify the "type"? All you need then is to map that type to different .NET classes declaratively. But in the end how this is done depends on what JSON framework you use on .NET... and if the data is truly dynamic: https://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object – Lucero Oct 31 '16 at 22:32
  • 1
    If you plan to use [Json.Net](http://www.newtonsoft.com/json), see [Deserializing polymorphic json classes without type information using json.net](http://stackoverflow.com/q/19307752/10263) – Brian Rogers Nov 01 '16 at 00:40
  • @Lucero Yes i do hold that property that can identify the type. But then i have to do it with switch statements or else/if and I really don't want to do that. If for example I have 1 class and want to read a file where only 1 object is declared, I can read it without Identifying... That I want also for my stucture and I'm sure that is possible – M Yil Nov 01 '16 at 10:58

0 Answers0