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.
- JSON
- XML
- YAML
Please read the Newtonsoft.Json documentation before proceeding.
Please read the XDocument documentation before proceeding.
Please read the YamlDotNet documentation before proceeding.
String Serialization​
You can serialize data to strings and deserialize back from them as follows:
- JSON
- XML
- YAML
// Strongly-typed
var configuration = json.GetJson<Configuration>();
var json = configuration.ToJson();
// Dynamically-typed
var jobject = json.GetJson();
// Strongly-typed
var configuration = xml.GetXml<Configuration>();
var xml = configuration.ToXml();
// 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:
- JSON
- XML
- YAML
// Strongly-typed
var configuration = jsonFile.ReadJson<Configuration>();
jsonFile.WriteJson(configuration);
// Dynamically-typed
var jobject = jsonFile.ReadJson();
// Strongly-typed
var configuration = xmlFile.ReadXml<Configuration>();
xmlFile.WriteXml(configuration);
// 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:
- JSON
- XML
- YAML
jsonFile.UpdateJson<Configuration>(
update: x => x.Value = "new-value");
xmlFile.UpdateXml<Configuration>(
update: x => x.Value = "new-value");
yamlFile.UpdateYaml<Configuration>(
update: x => x.Value = "new-value");