YamlDocument
Definition
Namespace: Corvus.Yaml
Assembly: Corvus.Text.Json.dll
Provides methods for parsing YAML content and converting it to JsonDocument instances or JSON strings.
public static class YamlDocument
Inheritance
Object → YamlDocument
Methods
| Method | Description |
|---|---|
Convert(ReadOnlySpan<byte>, Utf8JsonWriter, YamlReaderOptions) static |
Converts UTF-8 YAML bytes to JSON, writing the output to the specified Utf8JsonWriter. |
ConvertToJsonString static |
Converts UTF-8 YAML bytes to a JSON string. |
ConvertToYaml static |
Converts a JsonElement to YAML, writing the output to the specified... |
ConvertToYamlString static |
Converts a JsonElement to a YAML string. |
EnumerateEvents static |
Enumerates the YAML parse events from UTF-8 YAML bytes, invoking the specified callback for each event. |
Parse static |
Parses UTF-8 YAML bytes and returns a JsonDocument containing the equivalent JSON representation. |
ParseAsync(Stream, YamlReaderOptions, CancellationToken) static |
Parses UTF-8 YAML from a Stream asynchronously and returns a... |
Examples
Parse YAML into a JsonDocument, or convert between YAML and JSON string representations using the standalone System.Text.Json package (no Corvus.Text.Json dependency required).
using System.Text.Json;
using Corvus.Yaml;
string yaml = """
name: Alice
age: 30
hobbies:
- reading
- cycling
""";
using JsonDocument doc = YamlDocument.Parse(yaml);
string name = doc.RootElement.GetProperty("name").GetString()!;
// name: "Alice"
Converting YAML to a JSON string
string json = YamlDocument.ConvertToJsonString(yaml);
// json: {"name":"Alice","age":30,"hobbies":["reading","cycling"]}
Converting JSON to YAML
string yamlOutput = YamlDocument.ConvertToYamlString(
"""{"name":"Alice","age":30,"hobbies":["reading","cycling"]}""");
// yamlOutput:
// name: Alice
// age: 30
// hobbies:
// - reading
// - cycling
Multi-document streams
using JsonDocument docs = YamlDocument.Parse(multiDocYaml,
new YamlReaderOptions { DocumentMode = YamlDocumentMode.MultiAsArray });
Applies To
| Product | Versions |
|---|---|
| .NET | 9, 10 |
| .NET Standard | 2.0, 2.1 |
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests.
Open an issue