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 : IDisposableRemarks
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
| Name | Description |
|---|---|
| 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.csInitializes a new instance of the Utf8YamlWriter class that writes to a buffer writer.
public Utf8YamlWriter(IBufferWriter<byte> bufferWriter, Utf8YamlWriterOptions options = default)Parameters
bufferWriterIBufferWriter<byte>- The destination of the UTF-8 bytes.
optionsUtf8YamlWriterOptions- The options of the writer.
Exceptions
ArgumentNullExceptionbufferWriterisnull.
Utf8YamlWriter(Stream, Utf8YamlWriterOptions)
Utf8YamlWriter.csInitializes a new instance of the Utf8YamlWriter class that writes to a stream.
public Utf8YamlWriter(Stream utf8Stream, Utf8YamlWriterOptions options = default)Parameters
utf8StreamStream- The destination of the UTF-8 bytes. The writer does not dispose it.
optionsUtf8YamlWriterOptions- The options of the writer.
Exceptions
ArgumentNullExceptionutf8Streamisnull.ArgumentExceptionutf8Streamis not writable.
Properties
| Name | Description |
|---|---|
| BytesCommitted | Gets the number of bytes written to the destination so far. |
| BytesPending | Gets the number of bytes written to the buffer of the writer and not yet to the destination. |
| CurrentDepth | Gets the number of collections that are started and not ended. |
| Options | Gets the options of the writer. |
BytesCommitted
Utf8YamlWriter.csBytesPending
Utf8YamlWriter.csCurrentDepth
Utf8YamlWriter.csOptions
Utf8YamlWriter.csMethods
| Name | Description |
|---|---|
| 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. |
Dispose()
Utf8YamlWriter.csEnds the last line of a complete document, writes the pending bytes and releases the buffer of the writer.
public void Dispose()Flush()
Utf8YamlWriter.csWrites the pending bytes to the destination, and flushes the stream.
public void Flush()Exceptions
ObjectDisposedException- The writer is disposed.
WriteAliasValue(ReadOnlySpan<char>)
Utf8YamlWriter.csWrites an alias to the node with the anchor anchorName: *name.
public void WriteAliasValue(ReadOnlySpan<char> anchorName)Parameters
anchorNameReadOnlySpan<char>
Exceptions
ArgumentExceptionanchorNameis not a valid anchor name.InvalidOperationException- A node cannot be written here, or properties are pending: an alias has none.
WriteAnchor(ReadOnlySpan<char>)
Utf8YamlWriter.csSets the anchor of the next node: &name.
public void WriteAnchor(ReadOnlySpan<char> name)Parameters
nameReadOnlySpan<char>
Exceptions
ArgumentExceptionnameis not a valid anchor name.InvalidOperationException- The next node already has an anchor.
WriteBooleanValue(bool)
Utf8YamlWriter.csWrites true or false as a plain scalar.
WriteComment(ReadOnlySpan<char>)
Utf8YamlWriter.csWrites a comment on its own line: # text, one line per line of text.
public void WriteComment(ReadOnlySpan<char> text)Parameters
textReadOnlySpan<char>- The text of the comment. Its line breaks start new comment lines.
Exceptions
ArgumentExceptiontextcontains 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.csEnds 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.csEnds 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.csEnds the current sequence.
public void WriteEndSequence()Exceptions
InvalidOperationException- The current collection is not a sequence, or properties are pending.
WriteNullValue()
Utf8YamlWriter.csWrites null as a plain scalar.
public void WriteNullValue()Exceptions
InvalidOperationException- A node cannot be written here.
WriteNumberValue overloads
WriteNumberValue(decimal)
Utf8YamlWriter.csWrites a decimal number as a plain scalar.
WriteNumberValue(double)
Utf8YamlWriter.csWrites a floating-point number as a plain scalar: .inf, -.inf and .nan for the special values.
WriteNumberValue(int)
Utf8YamlWriter.csWrites an integer as a plain scalar.
WriteNumberValue(long)
Utf8YamlWriter.csWrites an integer as a plain scalar.
WriteNumberValue(float)
Utf8YamlWriter.csWrites a floating-point number as a plain scalar: .inf, -.inf and .nan for the special values.
WriteNumberValue(uint)
Utf8YamlWriter.csWrites an integer as a plain scalar.
WriteNumberValue(ulong)
Utf8YamlWriter.csWrites an integer as a plain scalar.
WritePropertyName overloads
WritePropertyName(ReadOnlySpan<byte>)
Utf8YamlWriter.csWrites the key of a mapping entry, as a string scalar.
public void WritePropertyName(ReadOnlySpan<byte> utf8Name)Parameters
utf8NameReadOnlySpan<byte>
Exceptions
ArgumentExceptionutf8Nameis not valid UTF-8.InvalidOperationException- The writer is not at the key of a mapping entry.
WritePropertyName(ReadOnlySpan<char>)
Utf8YamlWriter.csWrites the key of a mapping entry, as a string scalar.
public void WritePropertyName(ReadOnlySpan<char> name)Parameters
nameReadOnlySpan<char>
Exceptions
ArgumentExceptionnamecontains an invalid UTF-16 sequence.InvalidOperationException- The writer is not at the key of a mapping entry.
WriteStartDocument()
Utf8YamlWriter.csStarts 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.csStarts a mapping.
Parameters
styleYamlStyle- 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.csStarts a sequence.
Parameters
styleYamlStyle- 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.csWrites a string scalar from its UTF-8 bytes.
public void WriteStringValue(ReadOnlySpan<byte> utf8Value, ScalarStyle style = ScalarStyle.Any)Parameters
utf8ValueReadOnlySpan<byte>- The value, encoded in UTF-8.
styleScalarStyle- The preferred style, see
Utf8YamlWriter.WriteStringValue.
Exceptions
ArgumentExceptionutf8Valueis 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.csWrites a string scalar.
public void WriteStringValue(ReadOnlySpan<char> value, ScalarStyle style = ScalarStyle.Any)Parameters
valueReadOnlySpan<char>- The value.
styleScalarStyle- The preferred style.
ScalarStyle.Anywrites a plain scalar when it reads back as the same string, and a quoted one otherwise.ScalarStyle.Plainwrites a plain scalar even when it reads back as another type, such as123. A quoted style that cannot represent the value falls back to double quotes, andScalarStyle.LiteralandScalarStyle.Foldedwrite a literal block scalar where it is possible.
Exceptions
ArgumentExceptionvaluecontains 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.csSets the tag of the next node.
public void WriteTag(ReadOnlySpan<char> tag)Parameters
tagReadOnlySpan<char>- The resolved tag:
!, a local tag such as!point, or a URI such astag:yaml.org,2002:str, written as!!str. A tag that starts with the prefix of a%TAGdirective of the document is written with its handle (seeUtf8YamlWriter.WriteTagDirective), another URI verbatim (!<uri>), and the characters a tag cannot contain are escaped (%21).
Exceptions
ArgumentExceptiontagcontains an invalid UTF-16 sequence, or is neither!, a local tag, a URI, nor starts with the prefix of a%TAGdirective of the document.InvalidOperationException- The next node already has a tag.
WriteTagDirective(ReadOnlySpan<char>, ReadOnlySpan<char>)
Utf8YamlWriter.csDeclares 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
handleReadOnlySpan<char>- The handle:
!,!!, or!followed by letters, digits,-or_and!, such as!e!. prefixReadOnlySpan<char>- The prefix of the tags, decoded:
%escapes are written where they are needed.
Exceptions
ArgumentExceptionhandleis not a valid handle, orprefixis 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.