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.
Ignore, By default Json.NET ignores JSON if there is no field or property for its value to be set to during deserialization.
Error, son.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.
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.
Include, will write a field/property value to JSON when serializing if the value is the same as the field/property's default value.
Ignore, 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 option is very interesting because it allows to reduce the size of the JSON. In other words, if the value corresponds to the default value, it will not be serialized if the Ignore option is chosen. Cela fonctionne pour les chaine de caractère vide, les valeur numérique à 0 etc. Mais cela fonctionne aussi If your Schema defines elements containing the default attribute as shown in the example below, an equivalent attribute will be generated on the properties.
"city": { "description": "Defined the country of delivery or billing", "type": "string", "minLength": 5, "maxLength": 40, "default": "New York" },
The example below illustrates the resulting JSON value:
PurchaseOrderType po = new PurchaseOrderType(); po.BillTo.city = "New York"; po.OrderDate = DateTime.Now.ToString(); po.ShipTo = null; // Save data to json file po.SaveToFile(@"c:\temp\po.json");
Include { "ShipTo": null, "BillTo": { "name": null, "street": null, "city": "New York", "state": null, "zip": 0, "country": null }, "OrderDate": "4/21/2020 9:29:52 AM" } | Ignore { "BillTo": {}, "OrderDate": "4/21/2020 9:40:47 AM" } |
---|