How to: Control Serialization of Derived Classes.; 4 minutes to read +1; In this article. Using the XmlElementAttribute attribute to change the name of an XML element is not the only way to customize object serialization.
Active4 years, 5 months ago
Update: solved! It seems like Json.NET does include derived type properties by default, but they were not included because of an error in my code where the derived type was overwritten by a base type.
I am currently working on a project for school, and I stumbled upon a problem. Stick figure badminton game.
I need to serialize an object to Json, which I do using Newtonsoft Json.NET. The object I am trying to serialize has a List of objects of a certain base class, but the objects in that List are of derived types with their own unique properties.
Currently, only the properties of the base class are included in the resulting Json. If it's possible, I'd like the Json converter to detect of which derived class the objects in the collection are, and to serialize their unique properties.
Below some code as an example of what I'm doing.
Classes I use:
Sonicwall ssl-vpn netextender free download. We recommend checking your downloads with an antivirus. Download the required product from the developer's site for free safely and easily using the official link provided by the developer of SonicWALL SSL-VPN NetExtender below.We wish to warn you that since SonicWALL SSL-VPN NetExtender files are downloaded from an external source, FDM Lib bears no responsibility for the safety of such downloads. Thank you for using our software library.
Serializing some dummy objects:
The Json that would currently be generated:
Newtonsoft Json Serialize Struct
The Json I want:
Any ideas? Thank you!
Bert Willekens
Bert WillekensBert Willekens
2 Answers
The default behavior of json.NET is to include all properties on derived types. The only reason you would not be getting them is if you have defined a evanmcdonnalevanmcdonnal
[DataContract] on the base type which you have not extended to your derived types or if you have something like optin serialization ect.
30.6k1212 gold badges6161 silver badges8787 bronze badges
Decorate the properties with Ignore attribute if you dont want them serialized like
XtremeBytesXtremeBytes
Not the answer you're looking for? Browse other questions tagged c#jsoninheritanceserializationjson.net or ask your own question.-->Newtonsoft Json Serialize Derived Class Only Flights
Using the XmlElementAttribute attribute to change the name of an XML element is not the only way to customize object serialization. You can also customize the XML stream by deriving from an existing class and instructing the XmlSerializer instance how to serialize the new class.
For example, given a
Book class, you can derive from it and create an ExpandedBook class that has a few more properties. However, you must instruct the XmlSerializer to accept the derived type when serializing or deserializing. This can be done by creating a XmlElementAttribute instance and setting its Type property to the derived class type. Add the XmlElementAttribute to a XmlAttributes instance. Then add the XmlAttributes to a XmlAttributeOverrides instance, specifying the type being overridden and the name of the member that accepts the derived class. This is shown in the following example.
ExampleNewtonsoft Json Serialize To FileSee alsoComments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |