0

I have a class in this format:

[XmlRoot("Root")]
public class XYZ
{
    public string A{ get; set; }

    public string B{ get; set; }

    public string C{ get; set; }

    public string D{ get; set; }

    public ParentClass E{ get; set; }

    public string F{ get; set; }
}

There are many child classes of this parent class for property E, and I need it so that if I pass the properties as JSON or XML in a post request that correspond to the child objects then it should store that as E

But it's only showing properties that are in the ParentClass

I have seen similar posts such as this on the topic: Deserialising Json to derived types in Asp.Net Web API

The problem with the other solutions I have seen is that they seem to expect you to pass the subclass's object type in the request which I don't want to do, it should work it out. And I don't think I should have to specify a list of child objects that it should serialize to either.

Example request body:

 {
   "E":{
      "ParentClassesProperty": "Foobar"
      "ChildClassesProperty": "Hello World"
   },
   "A":"foobar",
   "B":"hfj",
   "C": "",
   "D":"foo",
   "F":"bar"
 }

Controller method signature:

 [HttpPost]
 [Route("route")]
 [Authorize(Foo = "foo", Bar = "Bar")]
 public HttpStatusCode NotifyMetrix(XYZ xyz)
Kieran
  • 612
  • 6
  • 22
  • You need to provide the properties of ParentClass and the Children here. JSON maps directly 1:1 with the substructure. Here, E has 2 properties on it - it isn't meant to map to different sub classes directly. – Daniel Lorenz Apr 16 '20 at 13:27
  • For the JSON of E, the ParentClassProperty is meant to one that the subclass inherits from and the subclassproperty would be local to that subclass only. Is that still wrong? – Kieran Apr 16 '20 at 13:38
  • If E can be many different sets of data, but the other properties are the same should I just make E dynamic type, I'm going to use XSD's to check the data anyway. Or is there a better way? – Kieran Apr 16 '20 at 13:39
  • You would need to mark E as "dynamic" and then in theory you could access the various properties... But if that doesn't work, then the issue is that it would deserialize to JObject instead and you'd have to deal with it that way. – Daniel Lorenz Apr 16 '20 at 16:13
  • 1) You have tagged this as both [tag:xml] and [tag:json]. Are you asking how to do polymorphic deserialization for both? 2) Regarding JSON, what JSON serializer are you using? Web API uses both [tag:json.net] and [tag:datacontractjsonserializer] as well as [tag:system.text.json] in .NET Core 3.0 and subsequent. See https://learn.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/json-and-xml-serialization#json-media-type-formatter and https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to – dbc Apr 16 '20 at 16:38
  • I would need it to have polymorphic capabilities for both XML and JSON, it looks like it's using the default formatter – Kieran Apr 17 '20 at 11:58

0 Answers0