Utf8TomlWriter Class
A high-performance, forward-only writer of UTF-8 TOML documents.
public sealed class Utf8TomlWriter : IAsyncDisposable, IDisposableRemarks
The writer writes what it is asked, in that order: a key/value pair after a Utf8TomlWriter.WritePropertyName, and a table header with Utf8TomlWriter.WriteTableHeader. TOML requires the keys of a table to be written before its sub-tables, and like Utf8JsonWriter, the writer does not check that a key is written once.
The output only uses TOML 1.0 syntax, so TOML 1.0 and TOML 1.1 readers accept it, unless Utf8TomlWriterOptions.AllowHexEscapes is set.
- Implements
Constructors
| Name | Description |
|---|---|
| Utf8TomlWriter(IBufferWriter<byte>, Utf8TomlWriterOptions) | Initializes a new writer that writes to an IBufferWriter<T>. |
| Utf8TomlWriter(Stream, Utf8TomlWriterOptions) | Initializes a new writer that writes to a stream. |
Utf8TomlWriter overloads
Utf8TomlWriter(IBufferWriter<byte>, Utf8TomlWriterOptions)
Utf8TomlWriter.csInitializes a new writer that writes to an IBufferWriter<T>.
public Utf8TomlWriter(IBufferWriter<byte> bufferWriter, Utf8TomlWriterOptions options = default)Parameters
bufferWriterIBufferWriter<byte>- The destination of the UTF-8 text.
optionsUtf8TomlWriterOptions- The options of the writer.
Utf8TomlWriter(Stream, Utf8TomlWriterOptions)
Utf8TomlWriter.csInitializes a new writer that writes to a stream.
public Utf8TomlWriter(Stream utf8Stream, Utf8TomlWriterOptions options = default)Parameters
utf8StreamStream- The destination of the UTF-8 text.
optionsUtf8TomlWriterOptions- The options of the writer.
Properties
| Name | Description |
|---|---|
| BytesCommitted | Gets the number of bytes flushed to the destination. |
| BytesPending | Gets the number of bytes written and not flushed yet. |
| CurrentDepth | Gets the number of arrays and inline tables being written. |
| Options | Gets the options of the writer. |
BytesCommitted
Utf8TomlWriter.csBytesPending
Utf8TomlWriter.csCurrentDepth
Utf8TomlWriter.csOptions
Utf8TomlWriter.csMethods
| Name | Description |
|---|---|
| Dispose() | Flushes the pending bytes and releases the buffer of the writer. |
| DisposeAsync() | Flushes the pending bytes and releases the buffer of the writer. |
| Flush() | Writes the pending bytes to the destination. The line of the last key/value pair or header is ended first. |
| FlushAsync(CancellationToken) | Writes the pending bytes to the destination. The line of the last key/value pair or header is ended first. |
| Reset() | Discards the state and the pending bytes, to write a new document to the same destination. |
| Reset(IBufferWriter<byte>) | Discards the state and the pending bytes, to write a new document to another destination. |
| Reset(Stream) | Discards the state and the pending bytes, to write a new document to another destination. |
| WriteArrayOfTablesHeader(ReadOnlySpan<byte>) | Writes the header of an element of an array of tables with a single key, such as [[a]]. |
| WriteArrayOfTablesHeader(ReadOnlySpan<string>) | Writes the header of an element of an array of tables, such as [[a.b]]. |
| WriteBlankLine() | Writes an empty line, to separate the parts of the document. |
| WriteBooleanValue(bool) | Writes a boolean value. |
| WriteComment(ReadOnlySpan<char>) | Writes a comment, as # followed by text. After a key/value pair or a header, the comment is on the same line; otherwise it is on its own line. |
| WriteComment(string) | Writes a comment, as # followed by text. After a key/value pair or a header, the comment is on the same line; otherwise it is on its own line. |
| WriteDateTimeValue(TomlDateTime) | Writes a date or a time value. |
| WriteDateTimeValue(DateOnly) | Writes a local date value. |
| WriteDateTimeValue(DateTimeOffset) | Writes an offset date-time value. |
| WriteDateTimeValue(TimeOnly) | Writes a local time value. |
| WriteDottedPropertyName(ReadOnlySpan<string>) | Writes a dotted key, such as a.b.c. The value that follows completes the key/value pair. |
| WriteEndArray() | Writes the end of an array value. |
| WriteEndInlineTable() | Writes the end of an inline table value. |
| WriteFloatValue(decimal) | Writes a Decimal as a float value, with all its digits. |
| WriteFloatValue(double) | Writes a float value, with the shortest text that reads back as the same value. |
| WriteFloatValue(float) | Writes a float value, with the shortest text that reads back as the same Single. |
| WriteIntegerValue(long, TomlIntegerFormat) | Writes an integer value. |
| WritePropertyName(ReadOnlySpan<byte>) | Writes a UTF-8 key. The value that follows completes the key/value pair. |
| WritePropertyName(ReadOnlySpan<char>) | Writes a key. The value that follows completes the key/value pair. |
| WritePropertyName(string) | Writes a key. The value that follows completes the key/value pair. |
| WriteRawValue(ReadOnlySpan<byte>, bool) | Writes a value given as TOML text, such as [1, 2] or 0xff. |
| WriteRawValue(string, bool) | Writes a value given as TOML text, such as [1, 2] or 0xff. |
| WriteStartArray() | Writes the start of an array value. |
| WriteStartInlineTable() | Writes the start of an inline table value. |
| WriteStringValue(ReadOnlySpan<byte>, TomlStringStyle) | Writes a UTF-8 string value. |
| WriteStringValue(ReadOnlySpan<char>, TomlStringStyle) | Writes a string value. |
| WriteStringValue(string, TomlStringStyle) | Writes a string value. |
| WriteTableHeader(ReadOnlySpan<byte>) | Writes a table header with a single key, such as [a]. |
| WriteTableHeader(ReadOnlySpan<string>) | Writes a table header, such as [a.b]. |
Dispose()
Utf8TomlWriter.csFlushes the pending bytes and releases the buffer of the writer.
public void Dispose()DisposeAsync()
Utf8TomlWriter.csFlush()
Utf8TomlWriter.csWrites the pending bytes to the destination. The line of the last key/value pair or header is ended first.
public void Flush()FlushAsync(CancellationToken)
Utf8TomlWriter.csWrites the pending bytes to the destination. The line of the last key/value pair or header is ended first.
net10.0
public Task FlushAsync(CancellationToken cancellationToken = default)net11.0
public Task FlushAsync(CancellationToken cancellationToken = default)Reset overloads
Reset()
Utf8TomlWriter.csDiscards the state and the pending bytes, to write a new document to the same destination.
public void Reset()Reset(IBufferWriter<byte>)
Utf8TomlWriter.csDiscards the state and the pending bytes, to write a new document to another destination.
public void Reset(IBufferWriter<byte> bufferWriter)Parameters
bufferWriterIBufferWriter<byte>- The new destination.
Reset(Stream)
Utf8TomlWriter.csWriteArrayOfTablesHeader overloads
WriteArrayOfTablesHeader(ReadOnlySpan<byte>)
Utf8TomlWriter.csWrites the header of an element of an array of tables with a single key, such as [[a]].
public void WriteArrayOfTablesHeader(ReadOnlySpan<byte> utf8Key)Parameters
utf8KeyReadOnlySpan<byte>- The UTF-8 key of the array.
WriteArrayOfTablesHeader(ReadOnlySpan<string>)
Utf8TomlWriter.csWrites the header of an element of an array of tables, such as [[a.b]].
public void WriteArrayOfTablesHeader(params scoped ReadOnlySpan<string> keys)Parameters
keysReadOnlySpan<string>- The segments of the key of the array.
WriteBlankLine()
Utf8TomlWriter.csWrites an empty line, to separate the parts of the document.
public void WriteBlankLine()WriteBooleanValue(bool)
Utf8TomlWriter.csWriteComment overloads
WriteComment(ReadOnlySpan<char>)
Utf8TomlWriter.csWrites a comment, as # followed by text. After a key/value pair or a header, the comment is on the same line; otherwise it is on its own line.
public void WriteComment(ReadOnlySpan<char> text)Parameters
textReadOnlySpan<char>- The text of the comment, which cannot contain new lines or other control characters but tab.
Exceptions
InvalidOperationException- The comment is written in an array, an inline table or after a key: TOML 1.0 does not allow it.
WriteComment(string)
Utf8TomlWriter.csWrites a comment, as # followed by text. After a key/value pair or a header, the comment is on the same line; otherwise it is on its own line.
Parameters
textstring- The text of the comment, which cannot contain new lines or other control characters but tab.
Exceptions
InvalidOperationException- The comment is written in an array, an inline table or after a key: TOML 1.0 does not allow it.
WriteDateTimeValue overloads
WriteDateTimeValue(TomlDateTime)
Utf8TomlWriter.csWrites a date or a time value.
public void WriteDateTimeValue(TomlDateTime value)Parameters
valueTomlDateTime- The value.
WriteDateTimeValue(DateOnly)
Utf8TomlWriter.csWriteDateTimeValue(DateTimeOffset)
Utf8TomlWriter.csWrites an offset date-time value.
public void WriteDateTimeValue(DateTimeOffset value)Parameters
valueDateTimeOffset- The value.
WriteDateTimeValue(TimeOnly)
Utf8TomlWriter.csWriteDottedPropertyName(ReadOnlySpan<string>)
Utf8TomlWriter.csWrites a dotted key, such as a.b.c. The value that follows completes the key/value pair.
public void WriteDottedPropertyName(params scoped ReadOnlySpan<string> segments)Parameters
segmentsReadOnlySpan<string>- The segments of the key.
WriteEndArray()
Utf8TomlWriter.csWrites the end of an array value.
public void WriteEndArray()WriteEndInlineTable()
Utf8TomlWriter.csWrites the end of an inline table value.
public void WriteEndInlineTable()WriteFloatValue overloads
WriteFloatValue(decimal)
Utf8TomlWriter.csWriteFloatValue(double)
Utf8TomlWriter.csWriteFloatValue(float)
Utf8TomlWriter.csWriteIntegerValue(long, TomlIntegerFormat)
Utf8TomlWriter.csWrites an integer value.
public void WriteIntegerValue(long value, TomlIntegerFormat format = TomlIntegerFormat.Decimal)Parameters
valuelong- The value.
formatTomlIntegerFormat- The base of the integer. A negative value is written in decimal.
WritePropertyName overloads
WritePropertyName(ReadOnlySpan<byte>)
Utf8TomlWriter.csWrites a UTF-8 key. The value that follows completes the key/value pair.
public void WritePropertyName(ReadOnlySpan<byte> utf8Name)Parameters
utf8NameReadOnlySpan<byte>- The UTF-8 key.
WritePropertyName(ReadOnlySpan<char>)
Utf8TomlWriter.csWrites a key. The value that follows completes the key/value pair.
public void WritePropertyName(ReadOnlySpan<char> name)Parameters
nameReadOnlySpan<char>- The key.
WritePropertyName(string)
Utf8TomlWriter.csWriteRawValue overloads
WriteRawValue(ReadOnlySpan<byte>, bool)
Utf8TomlWriter.csWrites a value given as TOML text, such as [1, 2] or 0xff.
public void WriteRawValue(ReadOnlySpan<byte> utf8Value, bool skipInputValidation = false)Parameters
utf8ValueReadOnlySpan<byte>- The UTF-8 TOML text of a single value.
skipInputValidationbooltrueto write the text without checking that it is a single valid value.
Exceptions
TomlException- The text is not a single valid TOML value.
WriteRawValue(string, bool)
Utf8TomlWriter.csWrites a value given as TOML text, such as [1, 2] or 0xff.
Parameters
valuestring- The TOML text of a single value.
skipInputValidationbooltrueto write the text without checking that it is a single valid value.
Exceptions
TomlException- The text is not a single valid TOML value.
WriteStartArray()
Utf8TomlWriter.csWrites the start of an array value.
public void WriteStartArray()WriteStartInlineTable()
Utf8TomlWriter.csWrites the start of an inline table value.
public void WriteStartInlineTable()WriteStringValue overloads
WriteStringValue(ReadOnlySpan<byte>, TomlStringStyle)
Utf8TomlWriter.csWrites a UTF-8 string value.
public void WriteStringValue(ReadOnlySpan<byte> utf8Value, TomlStringStyle style = TomlStringStyle.Basic)Parameters
utf8ValueReadOnlySpan<byte>- The UTF-8 value.
styleTomlStringStyle- How to write the string. A literal style the value cannot use, such as a literal string with a quote, falls back to a basic string.
WriteStringValue(ReadOnlySpan<char>, TomlStringStyle)
Utf8TomlWriter.csWrites a string value.
public void WriteStringValue(ReadOnlySpan<char> value, TomlStringStyle style = TomlStringStyle.Basic)Parameters
valueReadOnlySpan<char>- The value.
styleTomlStringStyle- How to write the string. A literal style the value cannot use, such as a literal string with a quote, falls back to a basic string.
WriteStringValue(string, TomlStringStyle)
Utf8TomlWriter.csWrites a string value.
public void WriteStringValue(string value, TomlStringStyle style = TomlStringStyle.Basic)Parameters
valuestring- The value.
styleTomlStringStyle- How to write the string. A literal style the value cannot use, such as a literal string with a quote, falls back to a basic string.
WriteTableHeader overloads
WriteTableHeader(ReadOnlySpan<byte>)
Utf8TomlWriter.csWrites a table header with a single key, such as [a].
public void WriteTableHeader(ReadOnlySpan<byte> utf8Key)Parameters
utf8KeyReadOnlySpan<byte>- The UTF-8 key of the table.
WriteTableHeader(ReadOnlySpan<string>)
Utf8TomlWriter.csWrites a table header, such as [a.b].
public void WriteTableHeader(params scoped ReadOnlySpan<string> keys)Parameters
keysReadOnlySpan<string>- The segments of the key of the table.