You are viewing an old version of this page. View the current version.
Compare with Current
View Page History
« Previous
Version 7
Next »
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.
#region Serialize/Deserialize
/// <summary>
/// Serializes current PurchaseOrderType object into an json string
/// </summary>
public virtual string Serialize()
{
Newtonsoft.Json.JsonSerializerSettings settings = new Newtonsoft.Json.JsonSerializerSettings();
return JsonConvert.SerializeObject(this, settings);
}
/// <summary>
/// Deserializes PurchaseOrderType object
/// </summary>
/// <param name="input">string workflow markup to deserialize</param>
/// <param name="obj">Output PurchaseOrderType object</param>
/// <param name="exception">output Exception value if deserialize failed</param>
/// <returns>true if this Serializer can deserialize the object; otherwise, false</returns>
public static bool Deserialize(string input, out PurchaseOrderType obj, out System.Exception exception)
{
exception = null;
obj = default(PurchaseOrderType);
try
{
obj = Deserialize(input);
return true;
}
catch (System.Exception ex)
{
exception = ex;
return false;
}
}
public static bool Deserialize(string input, out PurchaseOrderType obj)
{
System.Exception exception = null;
return Deserialize(input, out obj, out exception);
}
public static PurchaseOrderType Deserialize(string input)
{
Newtonsoft.Json.JsonSerializerSettings settings = new Newtonsoft.Json.JsonSerializerSettings();
return JsonConvert.DeserializeObject<PurchaseOrderType>(input, settings);
}
#endregion
public virtual void SaveToFile(string fileName)
{
System.IO.StreamWriter streamWriter = null;
try
{
string xmlString = Serialize();
System.IO.FileInfo outputFile = new System.IO.FileInfo(fileName);
streamWriter = outputFile.CreateText();
streamWriter.WriteLine(xmlString);
streamWriter.Close();
}
finally
{
if ((streamWriter != null))
{
streamWriter.Dispose();
}
}
}
public static PurchaseOrderType LoadFromFile(string fileName)
{
System.IO.FileStream file = null;
System.IO.StreamReader sr = null;
try
{
file = new System.IO.FileStream(fileName, FileMode.Open, FileAccess.Read);
sr = new System.IO.StreamReader(file);
string xmlString = sr.ReadToEnd();
sr.Close();
file.Close();
return Deserialize(xmlString);
}
finally
{
if ((file != null))
{
file.Dispose();
}
if ((sr != null))
{
sr.Dispose();
}
}
}
}
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 to a file
By default the generated methods are : LoadFromFile(...), SaveToFile(...), Serialize(...), Deserialize(...)
Here is an example to produce a JSON file from a generated class.
static void Main(string[] args)
{
Console.WriteLine("Basic JSON Serialization based on propertyName");
PurchaseOrderType po = new PurchaseOrderType();
po.BillTo.name = "Kaila Tamara";
po.BillTo.street = "3490 Green Hill Road";
po.BillTo.zip = "72761";
po.OrderDate = DateTime.Now;
po.SaveToFile(@"c:\temp\po.json");
}