Utf8YamlWriter Class

Writes YAML as UTF-8 to a Stream or an IBufferWriter<T>, one node at a time, like System.Text.Json.Utf8JsonWriter. Block collections are indented, flow collections are written on one line, and every scalar gets the simplest style that reads back as the same value.

public sealed class Utf8YamlWriter : IDisposable

Remarks

The bytes are kept in a buffer until Utf8YamlWriter.Flush or Utf8YamlWriter.Dispose, or until the buffer is large: call one of them when the writing is done.

A mapping alternates keys and values. A key is usually written with Utf8YamlWriter.WritePropertyName, but any node can be a key: a collection or a long scalar is written as an explicit key (? key).

Utf8YamlWriter.WriteTagDirective declares the %TAG directives of a document before it starts, and Utf8YamlWriter.WriteComment writes comments between the entries of block collections.

Implements

Constructors

NameDescription
Utf8YamlWriter(IBufferWriter<byte>, Utf8YamlWriterOptions)Initializes a new instance of the Utf8YamlWriter class that writes to a buffer writer.
Utf8YamlWriter(Stream, Utf8YamlWriterOptions)Initializes a new instance of the Utf8YamlWriter class that writes to a stream.

Utf8YamlWriter overloads

Utf8YamlWriter(IBufferWriter<byte>, Utf8YamlWriterOptions)

Utf8YamlWriter.cs

Initializes a new instance of the Utf8YamlWriter class that writes to a buffer writer.

public Utf8YamlWriter(IBufferWriter<byte> bufferWriter, Utf8YamlWriterOptions options = default)
Parameters
bufferWriter IBufferWriter<byte>
The destination of the UTF-8 bytes.
options Utf8YamlWriterOptions
The options of the writer.
Exceptions
ArgumentNullException
bufferWriter is null.

Utf8YamlWriter(Stream, Utf8YamlWriterOptions)

Utf8YamlWriter.cs

Initializes a new instance of the Utf8YamlWriter class that writes to a stream.

public Utf8YamlWriter(Stream utf8Stream, Utf8YamlWriterOptions options = default)
Parameters
utf8Stream Stream
The destination of the UTF-8 bytes. The writer does not dispose it.
options Utf8YamlWriterOptions
The options of the writer.
Exceptions
ArgumentNullException
utf8Stream is null.
ArgumentException
utf8Stream is not writable.

Properties

NameDescription
BytesCommittedGets the number of bytes written to the destination so far.
BytesPendingGets the number of bytes written to the buffer of the writer and not yet to the destination.
CurrentDepthGets the number of collections that are started and not ended.
OptionsGets the options of the writer.

BytesCommitted

Utf8YamlWriter.cs

Gets the number of bytes written to the destination so far.

public long BytesCommitted { get; }

Property value

long

BytesPending

Utf8YamlWriter.cs

Gets the number of bytes written to the buffer of the writer and not yet to the destination.

public int BytesPending { get; }

Property value

int

CurrentDepth

Utf8YamlWriter.cs

Gets the number of collections that are started and not ended.

public int CurrentDepth { get; }

Property value

int

Gets the options of the writer.

public Utf8YamlWriterOptions Options { get; }

Property value

Utf8YamlWriterOptions

Methods

NameDescription
Dispose()Ends the last line of a complete document, writes the pending bytes and releases the buffer of the writer.
Flush()Writes the pending bytes to the destination, and flushes the stream.
WriteAliasValue(ReadOnlySpan<char>)Writes an alias to the node with the anchor anchorName: *name.
WriteAnchor(ReadOnlySpan<char>)Sets the anchor of the next node: &name.
WriteBooleanValue(bool)Writes true or false as a plain scalar.
WriteComment(ReadOnlySpan<char>)Writes a comment on its own line: # text, one line per line of text.
WriteEndDocument()Ends the current document with an explicit ... marker.
WriteEndMapping()Ends the current mapping.
WriteEndSequence()Ends the current sequence.
WriteNullValue()Writes null as a plain scalar.
WriteNumberValue(decimal)Writes a decimal number as a plain scalar.
WriteNumberValue(double)Writes a floating-point number as a plain scalar: .inf, -.inf and .nan for the special values.
WriteNumberValue(int)Writes an integer as a plain scalar.
WriteNumberValue(long)Writes an integer as a plain scalar.
WriteNumberValue(float)Writes a floating-point number as a plain scalar: .inf, -.inf and .nan for the special values.
WriteNumberValue(uint)Writes an integer as a plain scalar.
WriteNumberValue(ulong)Writes an integer as a plain scalar.
WritePropertyName(ReadOnlySpan<byte>)Writes the key of a mapping entry, as a string scalar.
WritePropertyName(ReadOnlySpan<char>)Writes the key of a mapping entry, as a string scalar.
WriteStartDocument()Starts a new document with an explicit --- marker. The first document does not need one.
WriteStartMapping(YamlStyle)Starts a mapping.
WriteStartSequence(YamlStyle)Starts a sequence.
WriteStringValue(ReadOnlySpan<byte>, ScalarStyle)Writes a string scalar from its UTF-8 bytes.
WriteStringValue(ReadOnlySpan<char>, ScalarStyle)Writes a string scalar.
WriteTag(ReadOnlySpan<char>)Sets the tag of the next node.
WriteTagDirective(ReadOnlySpan<char>, ReadOnlySpan<char>)Declares a %TAG directive for the next document: the tags that start with prefix are written with handle, such as !e!point for tag:example.com,2000:point.

Ends the last line of a complete document, writes the pending bytes and releases the buffer of the writer.

public void Dispose()

Writes the pending bytes to the destination, and flushes the stream.

public void Flush()

Exceptions

ObjectDisposedException
The writer is disposed.

WriteAliasValue(ReadOnlySpan<char>)

Utf8YamlWriter.cs

Writes an alias to the node with the anchor anchorName: *name.

public void WriteAliasValue(ReadOnlySpan<char> anchorName)

Parameters

anchorName ReadOnlySpan<char>

Exceptions

ArgumentException
anchorName is not a valid anchor name.
InvalidOperationException
A node cannot be written here, or properties are pending: an alias has none.

WriteAnchor(ReadOnlySpan<char>)

Utf8YamlWriter.cs

Sets the anchor of the next node: &name.

public void WriteAnchor(ReadOnlySpan<char> name)

Parameters

name ReadOnlySpan<char>

Exceptions

ArgumentException
name is not a valid anchor name.
InvalidOperationException
The next node already has an anchor.

WriteBooleanValue(bool)

Utf8YamlWriter.cs

Writes true or false as a plain scalar.

public void WriteBooleanValue(bool value)

Parameters

value bool

Exceptions

InvalidOperationException
A node cannot be written here.

WriteComment(ReadOnlySpan<char>)

Utf8YamlWriter.cs

Writes a comment on its own line: # text, one line per line of text.

public void WriteComment(ReadOnlySpan<char> text)

Parameters

text ReadOnlySpan<char>
The text of the comment. Its line breaks start new comment lines.

Exceptions

ArgumentException
text contains an invalid UTF-16 sequence or a character that a comment cannot contain, such as a control character.
InvalidOperationException
The writer is inside a flow collection, between a key and its value, or properties are pending.

Remarks

A comment is written before the root node of a document, after it, or between the entries of a block collection, at their indentation. A comment inside an empty block collection is followed by {} or [] on the next line.

WriteEndDocument()

Utf8YamlWriter.cs

Ends the current document with an explicit ... marker.

public void WriteEndDocument()

Exceptions

InvalidOperationException
No document is started, a collection is not ended, or properties are pending.

WriteEndMapping()

Utf8YamlWriter.cs

Ends the current mapping.

public void WriteEndMapping()

Exceptions

InvalidOperationException
The current collection is not a mapping, its last key has no value, or properties are pending.

WriteEndSequence()

Utf8YamlWriter.cs

Ends the current sequence.

public void WriteEndSequence()

Exceptions

InvalidOperationException
The current collection is not a sequence, or properties are pending.

WriteNullValue()

Utf8YamlWriter.cs

Writes null as a plain scalar.

public void WriteNullValue()

Exceptions

InvalidOperationException
A node cannot be written here.

WriteNumberValue overloads

WriteNumberValue(decimal)

Utf8YamlWriter.cs

Writes a decimal number as a plain scalar.

public void WriteNumberValue(decimal value)
Parameters
value decimal
Exceptions
InvalidOperationException
A node cannot be written here.

WriteNumberValue(double)

Utf8YamlWriter.cs

Writes a floating-point number as a plain scalar: .inf, -.inf and .nan for the special values.

public void WriteNumberValue(double value)
Parameters
value double
Exceptions
InvalidOperationException
A node cannot be written here.

WriteNumberValue(int)

Utf8YamlWriter.cs

Writes an integer as a plain scalar.

public void WriteNumberValue(int value)
Parameters
value int
Exceptions
InvalidOperationException
A node cannot be written here.

WriteNumberValue(long)

Utf8YamlWriter.cs

Writes an integer as a plain scalar.

public void WriteNumberValue(long value)
Parameters
value long
Exceptions
InvalidOperationException
A node cannot be written here.

WriteNumberValue(float)

Utf8YamlWriter.cs

Writes a floating-point number as a plain scalar: .inf, -.inf and .nan for the special values.

public void WriteNumberValue(float value)
Parameters
value float
Exceptions
InvalidOperationException
A node cannot be written here.

WriteNumberValue(uint)

Utf8YamlWriter.cs

Writes an integer as a plain scalar.

public void WriteNumberValue(uint value)
Parameters
value uint
Exceptions
InvalidOperationException
A node cannot be written here.

WriteNumberValue(ulong)

Utf8YamlWriter.cs

Writes an integer as a plain scalar.

public void WriteNumberValue(ulong value)
Parameters
value ulong
Exceptions
InvalidOperationException
A node cannot be written here.

WritePropertyName overloads

WritePropertyName(ReadOnlySpan<byte>)

Utf8YamlWriter.cs

Writes the key of a mapping entry, as a string scalar.

public void WritePropertyName(ReadOnlySpan<byte> utf8Name)
Parameters
utf8Name ReadOnlySpan<byte>
Exceptions
ArgumentException
utf8Name is not valid UTF-8.
InvalidOperationException
The writer is not at the key of a mapping entry.

WritePropertyName(ReadOnlySpan<char>)

Utf8YamlWriter.cs

Writes the key of a mapping entry, as a string scalar.

public void WritePropertyName(ReadOnlySpan<char> name)
Parameters
name ReadOnlySpan<char>
Exceptions
ArgumentException
name contains an invalid UTF-16 sequence.
InvalidOperationException
The writer is not at the key of a mapping entry.

WriteStartDocument()

Utf8YamlWriter.cs

Starts a new document with an explicit --- marker. The first document does not need one.

public void WriteStartDocument()

Exceptions

InvalidOperationException
A collection is not ended, or properties are pending.

WriteStartMapping(YamlStyle)

Utf8YamlWriter.cs

Starts a mapping.

public void WriteStartMapping(YamlStyle style = YamlStyle.Any)

Parameters

style YamlStyle
The style of the mapping. Inside a flow collection, every collection is written in the flow style.

Exceptions

InvalidOperationException
A node cannot be written here, or the maximum depth is reached.

WriteStartSequence(YamlStyle)

Utf8YamlWriter.cs

Starts a sequence.

public void WriteStartSequence(YamlStyle style = YamlStyle.Any)

Parameters

style YamlStyle
The style of the sequence. Inside a flow collection, every collection is written in the flow style.

Exceptions

InvalidOperationException
A node cannot be written here, or the maximum depth is reached.

WriteStringValue overloads

WriteStringValue(ReadOnlySpan<byte>, ScalarStyle)

Utf8YamlWriter.cs

Writes a string scalar from its UTF-8 bytes.

public void WriteStringValue(ReadOnlySpan<byte> utf8Value, ScalarStyle style = ScalarStyle.Any)
Parameters
utf8Value ReadOnlySpan<byte>
The value, encoded in UTF-8.
style ScalarStyle
The preferred style, see Utf8YamlWriter.WriteStringValue.
Exceptions
ArgumentException
utf8Value is not valid UTF-8, or cannot be written in the plain style that was asked for.
InvalidOperationException
A node cannot be written here.

WriteStringValue(ReadOnlySpan<char>, ScalarStyle)

Utf8YamlWriter.cs

Writes a string scalar.

public void WriteStringValue(ReadOnlySpan<char> value, ScalarStyle style = ScalarStyle.Any)
Parameters
value ReadOnlySpan<char>
The value.
style ScalarStyle
The preferred style. ScalarStyle.Any writes a plain scalar when it reads back as the same string, and a quoted one otherwise. ScalarStyle.Plain writes a plain scalar even when it reads back as another type, such as 123. A quoted style that cannot represent the value falls back to double quotes, and ScalarStyle.Literal and ScalarStyle.Folded write a literal block scalar where it is possible.
Exceptions
ArgumentException
value contains an invalid UTF-16 sequence, or cannot be written in the plain style that was asked for.
InvalidOperationException
A node cannot be written here.

WriteTag(ReadOnlySpan<char>)

Utf8YamlWriter.cs

Sets the tag of the next node.

public void WriteTag(ReadOnlySpan<char> tag)

Parameters

tag ReadOnlySpan<char>
The resolved tag: !, a local tag such as !point, or a URI such as tag:yaml.org,2002:str, written as !!str. A tag that starts with the prefix of a %TAG directive of the document is written with its handle (see Utf8YamlWriter.WriteTagDirective), another URI verbatim (!<uri>), and the characters a tag cannot contain are escaped (%21).

Exceptions

ArgumentException
tag contains an invalid UTF-16 sequence, or is neither !, a local tag, a URI, nor starts with the prefix of a %TAG directive of the document.
InvalidOperationException
The next node already has a tag.

WriteTagDirective(ReadOnlySpan<char>, ReadOnlySpan<char>)

Utf8YamlWriter.cs

Declares a %TAG directive for the next document: the tags that start with prefix are written with handle, such as !e!point for tag:example.com,2000:point.

public void WriteTagDirective(ReadOnlySpan<char> handle, ReadOnlySpan<char> prefix)

Parameters

handle ReadOnlySpan<char>
The handle: !, !!, or ! followed by letters, digits, - or _ and !, such as !e!.
prefix ReadOnlySpan<char>
The prefix of the tags, decoded: % escapes are written where they are needed.

Exceptions

ArgumentException
handle is not a valid handle, or prefix is empty or contains an invalid UTF-16 sequence.
InvalidOperationException
A collection is not ended, properties are pending, the document already started, or the handle is already declared.

Remarks

A directive is written before the --- of its document, which the writer writes after the directives. Once the root node of a document is written, a directive ends it with ... and applies to the next document. A tag that is neither a local tag nor a URI can only be written with a directive whose prefix starts it.