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.

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();

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();

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");