-1

maybe a similar question has already been answered somewhere else. But I've spent a lot of time to look for it and to try getting a solution. So I hope someone can help.

I have a class

public class SampleClass
{
   public int Id {get;set;}
   public string TypeName {get;set;}
   public BaseClass Data {get;set;}
}

Then I have several "data" classes derived from BaseClass, for example:

public class DataClass: BaseClass
{
    public string Name {get;set;}
    public string Address {get;set;}
}

Serialization works fine.

var sample = new SampleClass();
sample.Id = 1;
sample.TypeName = "DataClass";

var data = new DataClass();
data.Name = "Test";
data.Address = "Address";

sample.Data = data;

var jsonString = JsonConvert.SerializeObject(sample);

Result: {"Id":1,"TypeName":"DataClass","Data":{"Name":"Test","Address":"Address"}}

But the way back brings an empty "Data" property:

var backToObject = JsonConvert.DeserializeObject<SampleClass>(jsonString);

Is there a way to tell the Json converter that it should deserialize "Data" into the type given in "TypeName"?

Belenos
  • 29
  • 3
  • Try to use type name handling https://www.newtonsoft.com/json/help/html/SerializeTypeNameHandling.htm – Paolo Iommarini Aug 17 '22 at 21:25
  • This post looks like it will assist you https://stackoverflow.com/questions/6904825/deserialize-json-to-anonymous-object – pelican_incident Aug 17 '22 at 21:30
  • 1
    *Is there a way to tell the Json converter that it should deserialize "Data" into the type given in "TypeName"?* You can create a custom `JsonConverter` to deserialize your class hierarchy. Please see [Deserializing polymorphic json classes without type information using json.net](https://stackoverflow.com/q/19307752/3744182), [Json.Net Serialization of Type with Polymorphic Child Object](https://stackoverflow.com/q/29528648/3744182) or [How to implement custom JsonConverter in JSON.NET to deserialize a List of base class objects?](https://stackoverflow.com/q/8030538/3744182) for details. – dbc Aug 17 '22 at 22:20
  • That being said, it's better to put the `TypeName` property inside `BaseClass` rather than `SampleClass` so `BaseClass` is self-contained and can be reused elsewhere. – dbc Aug 17 '22 at 22:23

1 Answers1

1
var data = new DataClass
{
    Name = "Test",
    Address = "Address"
};

var sample = new SampleClass
{
    Id = 1,
    TypeName = "DataClass"
};

sample.Data = data;

var jsonString = JsonConvert.SerializeObject(sample, new JsonSerializerSettings
{
    TypeNameHandling = TypeNameHandling.All
});

var backToObject = JsonConvert.DeserializeObject<SampleClass>(jsonString, new JsonSerializerSettings
{
    TypeNameHandling = TypeNameHandling.Auto
});

The deserialization result looks like:

[17:59:50 INF] {"Id": 1, "TypeName": "DataClass", "Data": {"Name": "Test", "Address": "Address", "$type": "DataClass"}, "$type": "SampleClass"}
wwd
  • 310
  • 1
  • 9
  • That's quite a good approach. But in my case it doesn't work. The used type for the nested class comes from a separate assembly. So I need to "inject" the type into the (de)serializer. Is there a way to do this? – Belenos Aug 18 '22 at 09:57