The code generated for Serializer/Deserializer uses JSon.Net from NewtonSoft. Detailed documentation is available on the official website https://www.newtonsoft.com/.
xsd2code++ allows to configure the serializer on the setting that is the most relevant. Those can evolve over time with the addition of additional parameters. These parameters are detailed after this document.
How to enable JSON serialization?
The first thing to do is to enable the generation of serialization methods like this :
Serialization→Enable = true
Serialization→DefaultSerialiser = JSonSerializer
This is enough to produce a JSON file or stream from the generated classes.
Each of your classes will contain the serialization & deserialization methods. However, it is possible to group everything in a base class. To do so, the option below must be activated
GenericBaseClass → Enable = true
Serialize/deserialize basic sample
By default the generated methods are : LoadFromFile(...), SaveToFile(...), Serialize(...), Deserialize(...)
Here is an example to produce a JSON file from a generated class.
Advanded serialization setting
DateFormatHandedling, DateFormatString, DateParseHandedling, DateTimeZoneHandling, DefaultValueHandling, FloatFormatHandling, FloatParseHandling, MissingMemberHandling, NullValueHandling, StringEscapeHandling
All these parameters are those of the JSON serializer itself. The official documentation is available on the author's website.
https://www.newtonsoft.com/json/help/html/SerializationSettings.htm
For copyright reasons this documentation is not included here but only illustrated with examples.
DateFormatHandling
DateFormatHandling controls how dates are serialized. Deux possibilités,
IsoDateFormat : writes dates in the ISO 8601 format, e.g. "2012-03-21T05:40Z".
MicrosoftDateFormat : Dates are written in the Microsoft JSON format, e.g. "\/Date(1198908717056)\/".
DateFormatString
Ce paramètre permet de définir le format de date qui sera produit dans le fichier JSON
Par defaut la date est au format ISO 8601
{"ShipTo":[],"BillTo": "OrderDate":"2020-04-20T19:38:49.1119852+02:00"}
But if by exemple the format "d MMMM, yyyyy" is specified the result will be :
{"ShipTo":[],"BillTo": "OrderDate":"20 April, 2020"}
MissingMemberHandling
MissingMemberHandling controls how missing members, e.g. JSON contains a property that isn't a member on the object, are handled during deserialization.
Member | Description |
---|---|
Ignore | By default Json.NET ignores JSON if there is no field or property for its value to be set to during deserialization. |
Error | Json.NET errors when there is a missing member during deserialization. |
NullValueHandling
NullValueHandling controls how null values on .NET objects are handled during serialization and how null values in JSON are handled during deserialization.
Member | Description |
---|---|
Include | By default Json.NET writes null values to JSON when serializing and sets null values to fields/properties when deserializing. |
Ignore | Json.NET will skip writing JSON properties if the .NET value is null when serializing and will skip setting fields/properties if the JSON property is null when deserializing. |
Include : writes null values to JSON when serializing and sets null values to fields/properties when deserializing
{ "ShipTo": null, "BillTo": { "name": "Kaila Tamara", "street": "3490 Green Hill Road", "city": null, "state": null, "zip": "72761", "country": "US" }, "OrderDate": "2020-04-20T19:53:16.1639456+02:00" }
Ignore : skip writing JSON null properties
{ "BillTo": { "name": "Kaila Tamara", "street": "3490 Green Hill Road", "zip": "72761", "country": "US" }, "OrderDate": "2020-04-20T19:46:37.0493786+02:00" }
NullValueHandling can also be customized on individual properties with JsonPropertyAttribute.
DefaultValueHandling
DefaultValueHandling controls how Json.NET uses default values set using the .NET DefaultValueAttribute when serializing and deserializing.
Member | Description |
---|---|
Include | By default Json.NET will write a field/property value to JSON when serializing if the value is the same as the field/property's default value. The Json.NET deserializer will continue setting a field/property if the JSON value is the same as the default value. |
Ignore | Json.NET will skip writing a field/property value to JSON if the value is the same as the field/property's default value, or the custom value specified in DefaultValueAttribute if the attribute is present. The Json.NET deserializer will skip setting a .NET object's field/property if the JSON value is the same as the default value. |
DefaultValueHandling can also be customized on individual properties with JsonPropertyAttribute.