G
G
Georgy Pelageykin2016-08-27 01:34:51
Mono
Georgy Pelageykin, 2016-08-27 01:34:51

JSON serialization library that supports inheritance?

Good day. What can be done to (de)serialize such classes?

public class DataA
  {
    public Int32 Value;
  }

  public class DataB : DataA
  {
    public Double Value2;
  }

So far, the only thing that could digest this correctly (that is, "eating" a field of type DataA containing an instance of DataB correctly deserialize also DataB) is the XmlSerializer. Everything is fine with him, but the format is painfully heavy and does not know how, for example, to serialize dictionaries. LitJson does not support polymorphism, two problems arose with JsonFx - it was not possible to configure TypeHinting, which seems to be designed for this and refuses to serialize public fields - only properties or private fields.
Can anyone work with JsonFx and tell me how to make it work, or maybe there is another library?
PS It's happening under Unity3d, so I can't use DataContractJsonSerializer, and I'm not ready to buy JSON.Net for Unity yet.
PPS accidentally forgot to remove auto-properties - I would like to serialize simple public fields, because autoproperties have a private auto-generated "backing..." field that jsonfx tries to serialize.
PPPS I apologize for the incorrect question, I used an outdated version of the library adapted for unity3d. The version from this repository works fine.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
asArtem, 2016-09-01
@ArXen42

Json.net supports inheritance, moreover, it can even correctly convert DataA to DataB if before that there was a cast to the base type (DataA b = new DataB and b was stored in json and then you need to get DataB b2 = Json.Deserialize<>( jsonString) ). The whole trick is in the TypeNameHandling setting.
stackoverflow.com/questions/8513042/json-net-seria...
www.newtonsoft.com/json/help/html/serializetypenam...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question