I have the following class.
public class Foo { [XmlElement("Bar", typeof(Bar))] [XmlElement("Pub", typeof(Pub))] public BaseBar Bar { get; set; } } I would like to move from XML serialization to JSON (System.Text.Json) serialization, what is the equivalent for the attribute [XmlElement("Bar", typeof(Bar))]?
See this sample: https://dotnetfiddle.net/pU8QAU
Edit:
I am looking for a way to define it on property level, so if I have 2 properties I would like to have different names for those.
public class Foo { [XmlElement("Bar", typeof(Bar))] [XmlElement("Pub", typeof(Pub))] public BaseBar Bar { get; set; } [XmlElement("Bar2", typeof(Bar))] [XmlElement("Pub2", typeof(Pub))] public BaseBar Bar2 { get; set; } } See this sample: https://dotnetfiddle.net/M6nla
Edit2:
The given answer by Guru Stron produces this output
{"Bar":{"$type":"Pub" … I am looking for
{"Pub":{… Where Pub should be serialized from the property Bar if it is of type Pub.