Home  /  Blog  /  Documentation  /  Code Generator   /  Settings  Serialization

Serialization

Introduction

The .NET framework offers many serializer,s namely, binary serialization, XML serialization and JavaScriptSerializer. Binary serialization preserves the state of the object between different invocations of an application by preserving type fidelity. XML serialization uses XML as an open standard to serialize only the public properties and fields. It does not preserve type fidelity but provides support for serialization in human-readable, cross-platform XML. The framework provides many options to customize the serialization process to meet application requirements.

Xsd2code++ takes care of adding the set of attributes that are necessary for serialization/deserialization process, but also the serialization methods in memory or on disk. Serialization is a process that has many options and subtleties. This is why xsd2code++ brings a simple solution to automatically integrate these mechanisms into your classes without you having to worry about it.

Binary serialization and XML are integrated into the Dotnet FrameWork, you have no additional libraries to reference in your project. Xsd2code++ allows serialization through other solutions. As is NewtonSoft.JSON, MessagePack. In this case external libraries must be added to your project.

To summarize the different serialization possibilities are :

A graph showing the different serialization possibilities

To enable the generation of serialization methods, you must activate the option in the Serialization section as shown below :

Image showing the enabling of the Serialization and it's forms

Serialization mechanisms are based on reflection where the names of public properties are used. However, certain attributes make it possible to refine the serialization mechanisms. It is therefore very important to activate the generation of attributes according to the choice of serialization.

Image showing 2 different output settings enabled and it's effects

Then, many parameters are available to customize method names, encoding, conditional serialization mechanisms. These options are described in the chapters below:
XML Serialization
JSon Serialization
MessagePack Serialization
BSon Serialization

When the Serialization is active, you will see various new fuctions in your classes.

public virtual string Serialize() { ...}
public virtual void SaveToFile(string fileName)public virtual bool SaveToFile(string fileName, out Exception exception) { ...}
public static bool Deserialize(string input, out USAddress obj, out Exception exception) { ...}
public static bool Deserialize(string input, out USAddress obj) { ...}
public new static USAddress Deserialize(string input) { ...}
public static USAddress Deserialize(Stream s) { ...}
public static bool LoadFromFile(string fileName, out USAddress obj, out Exception exception) { ...}
public static bool LoadFromFile(string fileName, out USAddress obj) { ...}

This makes it extremely easy to serialize/deserialize either in memory or in a file.

static void Main(string[] args)
{
    Console.WriteLine("Hello World!");
    PurchaseOrderType po = new PurchaseOrderType();
    po.BillTo.country = "us";
    po.BillTo.name = "James";
    po.BillTo.street = "19 av road street";
 
    // Serialization in memory
    string serializedJsonObject = po.Serialize();
 
    // Serialization to file
    po.SaveToFile("c:\temp\po.json";);
 
    // Deserialization from memory
    PurchaseOrderType deserilizedObject = PurchaseOrderType.Deserialize(serializedJsonObject);
 
    // Deserialization from file
    PurchaseOrderType deserilizedObjectFromFile = PurchaseOrderType.LoadFromFile("c:\temp\po.json");
}

DefaultEncoder / EnableEncoding

It is possible to choose the encoding format of the file. The defaultEncoder parameter allows to define the encoding that will be used when calling the SaveToFile method. For example UTF8. An overload of the SaveToFile method allows to choose another encoding. This default encoding is also used for the LoadFromFile method call. The different possibilities are :

  • UTF8
  • UTF32
  • BigEndianUnicode
  • Unicode
  • ASCII
Console.WriteLine("Hello World!");
PurchaseOrderType po = new PurchaseOrderType();
po.BillTo.country = "us";
po.BillTo.name = "James";
po.BillTo.street = "19 av road street";
 
// Serialization to file
po.SaveToFile("c:\temp\po.json", System.Text.Encoding.UTF8);
 
// Deserialization from file
PurchaseOrderType deserilizedObjectFromFile = PurchaseOrderType.LoadFromFile("c:\temp\po.json", System.Text.Encoding.UTF8);

Serialization/Deserilization method name

You have the possibility to change the name of the default serialization/deserialization methods. 

Image showing the option to change the method names

For example, LoadFromFile can be replaced by FromXMLFile, SaveToFile by ToXMLFile. It's up to you to choose according to your naming convention

ShouldSerialize

This parameter allows you to reduce the size of your XML or JSon streams for values that do not need to be serialized. xsd2code++ depending on the type proposes the following cases :

  • Empty collections (if the collection is null or empty, it will not be serialized)
  • Empty complex types (If the object is null, it wil not be serialized)
  • Empty Enumeration types (If the enumeration has a default value, it will not be serialized)
  • Empty Nullable (If the property has no value, it will not be serialized)
  • Empty simple types (If the simple type (int, float ...) has a default value, it will not be serialized)
  • Empty Strings (If the property is null or is an empy string, it will not be serialized)
Image showing the effects of the ShouldSerialize

This parameter is part of the flow reduction strategy that is described in the article here