Utf8YamlWriter
Definition
Namespace: Corvus.Text.Json.Yaml
Assembly: Corvus.Text.Json.dll
A high-performance, low-allocation ref struct that writes YAML content to an IBufferWriter or Stream.
public readonly struct Utf8YamlWriter
Remarks
Supports both block and flow collection styles. Block style uses indentation; flow style uses inline comma-separated syntax. When SkipValidation is false, the writer validates structural correctness (e.g., property names must precede values in mappings, containers must be properly closed). This is a ref struct and must not be copied. Always pass by ref and dispose exactly once via a using declaration or explicit call to Dispose.
Constructors
| Constructor | Description |
|---|---|
| Utf8YamlWriter(...) | Initializes a new instance of the Utf8YamlWriter struct with a specified bufferWriter. |
Properties
| Property | Type | Description |
|---|---|---|
| CurrentDepth | int |
Gets the current depth (number of open containers). |
Methods
| Method | Description |
|---|---|
| Dispose() | Releases resources used by this writer. |
| Flush() | Commits the YAML text written so far to the output destination. |
| WriteBooleanValue(bool) | Writes a boolean value (true or false). |
| WriteEmptyMapping() | Writes an empty mapping as {}. |
| WriteEmptySequence() | Writes an empty sequence as \[\]. |
| WriteEndMapping() | Writes the end of a YAML mapping. |
| WriteEndSequence() | Writes the end of a YAML sequence. |
| WriteNullValue() | Writes a null value. |
| WriteNumberValue(ReadOnlySpan<byte>) | Writes a raw numeric value. |
| WritePropertyName(ReadOnlySpan<byte>) | Writes a property name in a mapping. |
| WriteStartMapping(YamlCollectionStyle) | Writes the start of a YAML mapping. |
| WriteStartSequence(YamlCollectionStyle) | Writes the start of a YAML sequence. |
| WriteStringValue(ReadOnlySpan<byte>) | Writes a string value, quoting it if necessary to preserve round-trip safety. |
Examples
Write structured YAML output to a buffer or stream using the low-level writer API.
using Corvus.Text.Json.Yaml;
var buffer = new ArrayBufferWriter<byte>();
using (var writer = new Utf8YamlWriter(buffer))
{
writer.WriteStartMapping();
writer.WritePropertyName("name"u8);
writer.WriteStringValue("Alice"u8);
writer.WritePropertyName("age"u8);
writer.WriteNumberValue("30"u8);
writer.WritePropertyName("active"u8);
writer.WriteBooleanValue(true);
writer.WriteEndMapping();
}
string yaml = Encoding.UTF8.GetString(buffer.WrittenSpan);
// yaml:
// name: Alice
// age: 30
// active: true
Nested mappings and sequences
using (var writer = new Utf8YamlWriter(buffer))
{
writer.WriteStartMapping();
writer.WritePropertyName("person"u8);
writer.WriteStartMapping();
writer.WritePropertyName("name"u8);
writer.WriteStringValue("Alice"u8);
writer.WriteEndMapping();
writer.WritePropertyName("hobbies"u8);
writer.WriteStartSequence();
writer.WriteStringValue("reading"u8);
writer.WriteStringValue("cycling"u8);
writer.WriteEndSequence();
writer.WriteEndMapping();
}
Flow-style collections
Use YamlCollectionStyle.Flow for compact inline output:
using (var writer = new Utf8YamlWriter(buffer))
{
writer.WriteStartMapping();
writer.WritePropertyName("tags"u8);
writer.WriteStartSequence(YamlCollectionStyle.Flow);
writer.WriteStringValue("admin"u8);
writer.WriteStringValue("user"u8);
writer.WriteEndSequence();
writer.WriteEndMapping();
}
// tags: [admin, user]
Writing to a stream
using var stream = new MemoryStream();
using (var writer = new Utf8YamlWriter(stream))
{
writer.WriteStartMapping();
writer.WritePropertyName("key"u8);
writer.WriteStringValue("value"u8);
writer.WriteEndMapping();
writer.Flush();
}
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