Home  /  Blog  /  Documentation  /  Code Generator   /  Settings  Serialization  /  JSon Serialization

JSon settings

The code generated for Serializer/Deserializer functions uses JSon.Net Serializers from NewtonSoft. Detailed documentation is available on the official NewtonSoft website.

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 in 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 :

Advanded serialization setting

DateFormatHandedling, DateFormatStringDateParseHandedling, DateTimeZoneHandling, DefaultValueHandling, FloatFormatHandlingFloatParseHandling, MissingMemberHandling, NullValueHandling, StringEscapeHandling
All these parameters are those of the JSON serializer itself. The official documentation is available on the author's website.

For copyright reasons this documentation is not included here but only illustrated with examples.

DateFormatHandling

DateFormatHandling controls how dates are serialized. you have two possibilities :
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

Image showing the DateFormatString options

This option defines the date format that will be used in the Json file 

The default date format is ISO 8601

But if by exemple the format "dd MM, yyyyy" is specified the result will be :

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

Ignore : skip writing JSON  null properties

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.

Image showing the DefaultValue in action

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
filepo.SaveToFile("c:\\temp\\po.json");
Include
Ignore