0

I have a class as below with the property of type object. Earlier I used to assign class dynamically to object property Item as below using XmlElementAttribute.

   [Serializable]

   public class Employee
    {
        public int EmployeeID { get; set; }
        public string EmployeeName { get; set; }
    }

    [Serializable]
    public class Department
    {
        public int DepartmentID { get; set; }
        public string DepartmentName { get; set; }
    }

    [Serializable]
    public class ParentClass
    {
        public string Location { get; set; }

        [System.Xml.Serialization.XmlElementAttribute("Emp", GetType(Employee)), System.Xml.Serialization.XmlElementAttribute("Dept", GetType(Department))]       
        public object Item { get; set; }
    }

 

Here is XML data containing property Emp, then automatically Employee class get assigned to the Item property of ParentClass.

I want to have similar functionality with System.Text.Json.

Is there any alternative for XmlElementAttribute("Emp", GetType(Employee)) in json like below

  [System.Text.Json.JsonPropertyName["Emp", GetType(Employee))]]

I have JSON input below

{
"Location": "Chicago",
"Emp": {
    "EmployeeID":123,
    "EmployeeName":"Peter"
}
}

Here since my JSON property name is Emp, I want to assign the Employee type dynamically to the object Item property of ParentClass.

{

"Location": "NJ",
"Dept": {
    "DepartmentID":567,
    "DepartmentName":"IT"
}
}

Similarly, if I have JSON data as above, I want to assign Department type to the object property Item of ParentClass.

Kindly suggest any solution for this.

When I googled, I'm able to find only below

    [JsonPropertyName("Wind")]

But I want to bind type dynamically similar to XmlElementAttribute("Emp", GetType(Employee))

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
  • Support for polymorphism was [added in .NET 7](https://devblogs.microsoft.com/dotnet/system-text-json-in-dotnet-7/#type-hierarchies). See [this answer specifically](https://stackoverflow.com/a/74352597/3744182) to [Is polymorphic deserialization possible in System.Text.Json?](https://stackoverflow.com/q/58074304/3744182). Before .NET 7 you would need to create a custom converter, the other answers to that question cover the possibilities. – dbc Dec 07 '22 at 16:32
  • Note that the attributes `[JsonDerivedType(typeof(Employee), "Emp")]` need to be added to the base type. In you case that is `object`, so you will need to either introduce an abstract base type for all item types, or use a custom type info resolver like the one shown in the answer. Does [Is polymorphic deserialization possible in System.Text.Json?](https://stackoverflow.com/a/59744873/3744182) answer your question sufficiently, or do you need a specific, tailored answer? – dbc Dec 07 '22 at 16:34
  • Also, are you using .NET 7, .NET 6, or something else? – dbc Dec 07 '22 at 18:25
  • @dbc Thanks for the response. I'm using .Net Core 3.1, so I can use JsonConvertor to fit my requirement right? If possible can you provide some example code that fit my requirement. I gone through examples but still not clear how can I implement for my scenario. It would be a really great help if you provide some code sample. – Sravani Yadav Dec 08 '22 at 01:42
  • You can refer to the link which dbc offered,and check the line `JsonSerializer.Serialize(objects, options);`,you can set the objects with the model type which you want it to serialize. – Yiyi You Dec 28 '22 at 09:27

0 Answers0