-
-
Notifications
You must be signed in to change notification settings - Fork 491
Samples.ConvertYamlToJson
Antoine Aubry edited this page Jun 23, 2019
·
3 revisions
Shows how to convert a YAML document to JSON.
using System.IO;
using YamlDotNet.Serialization;
using YamlDotNet.Samples.Helpers;
using Xunit.Abstractions;
namespace YamlDotNet.Samples
{
public class ConvertYamlToJson
{
private readonly ITestOutputHelper output;
public ConvertYamlToJson(ITestOutputHelper output)
{
this.output = output;
}
[Sample(
DisplayName = "Convert YAML to JSON",
Description = "Shows how to convert a YAML document to JSON."
)]
public void Main()
{
// convert string/file to YAML object
var r = new StringReader(@"
scalar: a scalar
sequence:
- one
- two
");
var deserializer = new DeserializerBuilder().Build();
var yamlObject = deserializer.Deserialize(r);
var serializer = new SerializerBuilder()
.JsonCompatible()
.Build();
var json = serializer.Serialize(yamlObject);
output.WriteLine(json);
}
}
}
{"scalar": "a scalar", "sequence": ["one", "two"]}