Skip to main content

Data Serialization

Structured data plays an essential role in build automation. You may want to read a list of repositories to be checked out, write data that's consumed by another tool, or update version numbers of SDKs and tools you consume. The central entry point for data serialization is the SerializationTasks class, which comes with support for JSON, XML, and YAML.

note

Please read the Newtonsoft.Json documentation before proceeding.

note

Please read the XDocument documentation before proceeding.

note

Please read the YamlDotNet documentation before proceeding.

String Serialization

You can serialize data to strings and deserialize back from them as follows:

Build.cs
// Strongly-typed
var configuration = json.GetJson<Configuration>();
var json = configuration.ToJson();

// Dynamically-typed
var jobject = json.GetJson();
Build.cs
// Strongly-typed
var configuration = xml.GetXml<Configuration>();
var xml = configuration.ToXml();
Build.cs
// Strongly-typed
var configuration = yaml.GetYaml<Configuration>();
var yaml = configuration.ToYaml();

File Serialization

You can serialize data to files and deserialize back from them as follows:

Build.cs
// Strongly-typed
var configuration = jsonFile.ReadJson<Configuration>();
jsonFile.WriteJson(configuration);

// Dynamically-typed
var jobject = jsonFile.ReadJson();
Build.cs
// Strongly-typed
var configuration = xmlFile.ReadXml<Configuration>();
xmlFile.WriteXml(configuration);
Build.cs
// Strongly-typed
var configuration = yamlFile.ReadYaml<Configuration>();
yamlFile.WriteYaml(configuration);

Updating Files

Instead of reading, updating, and writing files in separated steps, you can also use the atomic functions below:

Build.cs
jsonFile.UpdateJson<Configuration>(
update: x => x.Value = "new-value");
Build.cs
xmlFile.UpdateXml<Configuration>(
update: x => x.Value = "new-value");
Build.cs
yamlFile.UpdateYaml<Configuration>(
update: x => x.Value = "new-value");