Utf8TomlWriter Class

A high-performance, forward-only writer of UTF-8 TOML documents.

public sealed class Utf8TomlWriter : IAsyncDisposable, IDisposable

Remarks

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

NameDescription
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.cs

Initializes a new writer that writes to an IBufferWriter<T>.

public Utf8TomlWriter(IBufferWriter<byte> bufferWriter, Utf8TomlWriterOptions options = default)
Parameters
bufferWriter IBufferWriter<byte>
The destination of the UTF-8 text.
options Utf8TomlWriterOptions
The options of the writer.

Utf8TomlWriter(Stream, Utf8TomlWriterOptions)

Utf8TomlWriter.cs

Initializes a new writer that writes to a stream.

public Utf8TomlWriter(Stream utf8Stream, Utf8TomlWriterOptions options = default)
Parameters
utf8Stream Stream
The destination of the UTF-8 text.
options Utf8TomlWriterOptions
The options of the writer.

Properties

NameDescription
BytesCommittedGets the number of bytes flushed to the destination.
BytesPendingGets the number of bytes written and not flushed yet.
CurrentDepthGets the number of arrays and inline tables being written.
OptionsGets the options of the writer.

BytesCommitted

Utf8TomlWriter.cs

Gets the number of bytes flushed to the destination.

public long BytesCommitted { get; }

Property value

long

BytesPending

Utf8TomlWriter.cs

Gets the number of bytes written and not flushed yet.

public int BytesPending { get; }

Property value

int

CurrentDepth

Utf8TomlWriter.cs

Gets the number of arrays and inline tables being written.

public int CurrentDepth { get; }

Property value

int

Gets the options of the writer.

public Utf8TomlWriterOptions Options { get; }

Property value

Utf8TomlWriterOptions

Methods

NameDescription
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].

Flushes the pending bytes and releases the buffer of the writer.

public void Dispose()

DisposeAsync()

Utf8TomlWriter.cs

Flushes the pending bytes and releases the buffer of the writer.

net10.0

public ValueTask DisposeAsync()

net11.0

public ValueTask DisposeAsync()

Returns

ValueTask

Writes 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.cs

Writes 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)

Parameters

cancellationToken CancellationToken
A token to cancel the operation.

Returns

Task

Reset overloads

Discards the state and the pending bytes, to write a new document to the same destination.

public void Reset()

Reset(IBufferWriter<byte>)

Utf8TomlWriter.cs

Discards the state and the pending bytes, to write a new document to another destination.

public void Reset(IBufferWriter<byte> bufferWriter)
Parameters
bufferWriter IBufferWriter<byte>
The new destination.

Reset(Stream)

Utf8TomlWriter.cs

Discards the state and the pending bytes, to write a new document to another destination.

public void Reset(Stream utf8Stream)
Parameters
utf8Stream Stream
The new destination.

WriteArrayOfTablesHeader overloads

WriteArrayOfTablesHeader(ReadOnlySpan<byte>)

Utf8TomlWriter.cs

Writes the header of an element of an array of tables with a single key, such as [[a]].

public void WriteArrayOfTablesHeader(ReadOnlySpan<byte> utf8Key)
Parameters
utf8Key ReadOnlySpan<byte>
The UTF-8 key of the array.

WriteArrayOfTablesHeader(ReadOnlySpan<string>)

Utf8TomlWriter.cs

Writes the header of an element of an array of tables, such as [[a.b]].

public void WriteArrayOfTablesHeader(params scoped ReadOnlySpan<string> keys)
Parameters
keys ReadOnlySpan<string>
The segments of the key of the array.

WriteBlankLine()

Utf8TomlWriter.cs

Writes an empty line, to separate the parts of the document.

public void WriteBlankLine()

WriteBooleanValue(bool)

Utf8TomlWriter.cs

Writes a boolean value.

public void WriteBooleanValue(bool value)

Parameters

value bool
The value.

WriteComment overloads

WriteComment(ReadOnlySpan<char>)

Utf8TomlWriter.cs

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.

public void WriteComment(ReadOnlySpan<char> text)
Parameters
text ReadOnlySpan<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.cs

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.

public void WriteComment(string text)
Parameters
text string
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.cs

Writes a date or a time value.

public void WriteDateTimeValue(TomlDateTime value)
Parameters
value TomlDateTime
The value.

WriteDateTimeValue(DateOnly)

Utf8TomlWriter.cs

Writes a local date value.

public void WriteDateTimeValue(DateOnly value)
Parameters
value DateOnly
The value.

WriteDateTimeValue(DateTimeOffset)

Utf8TomlWriter.cs

Writes an offset date-time value.

public void WriteDateTimeValue(DateTimeOffset value)
Parameters
value DateTimeOffset
The value.

WriteDateTimeValue(TimeOnly)

Utf8TomlWriter.cs

Writes a local time value.

public void WriteDateTimeValue(TimeOnly value)
Parameters
value TimeOnly
The value.

WriteDottedPropertyName(ReadOnlySpan<string>)

Utf8TomlWriter.cs

Writes 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

segments ReadOnlySpan<string>
The segments of the key.

WriteEndArray()

Utf8TomlWriter.cs

Writes the end of an array value.

public void WriteEndArray()

WriteEndInlineTable()

Utf8TomlWriter.cs

Writes the end of an inline table value.

public void WriteEndInlineTable()

WriteFloatValue overloads

WriteFloatValue(decimal)

Utf8TomlWriter.cs

Writes a Decimal as a float value, with all its digits.

public void WriteFloatValue(decimal value)
Parameters
value decimal
The value.

WriteFloatValue(double)

Utf8TomlWriter.cs

Writes a float value, with the shortest text that reads back as the same value.

public void WriteFloatValue(double value)
Parameters
value double
The value.

WriteFloatValue(float)

Utf8TomlWriter.cs

Writes a float value, with the shortest text that reads back as the same Single.

public void WriteFloatValue(float value)
Parameters
value float
The value.

WriteIntegerValue(long, TomlIntegerFormat)

Utf8TomlWriter.cs

Writes an integer value.

public void WriteIntegerValue(long value, TomlIntegerFormat format = TomlIntegerFormat.Decimal)

Parameters

value long
The value.
format TomlIntegerFormat
The base of the integer. A negative value is written in decimal.

WritePropertyName overloads

WritePropertyName(ReadOnlySpan<byte>)

Utf8TomlWriter.cs

Writes a UTF-8 key. The value that follows completes the key/value pair.

public void WritePropertyName(ReadOnlySpan<byte> utf8Name)
Parameters
utf8Name ReadOnlySpan<byte>
The UTF-8 key.

WritePropertyName(ReadOnlySpan<char>)

Utf8TomlWriter.cs

Writes a key. The value that follows completes the key/value pair.

public void WritePropertyName(ReadOnlySpan<char> name)
Parameters
name ReadOnlySpan<char>
The key.

WritePropertyName(string)

Utf8TomlWriter.cs

Writes a key. The value that follows completes the key/value pair.

public void WritePropertyName(string name)
Parameters
name string
The key.

WriteRawValue overloads

WriteRawValue(ReadOnlySpan<byte>, bool)

Utf8TomlWriter.cs

Writes a value given as TOML text, such as [1, 2] or 0xff.

public void WriteRawValue(ReadOnlySpan<byte> utf8Value, bool skipInputValidation = false)
Parameters
utf8Value ReadOnlySpan<byte>
The UTF-8 TOML text of a single value.
skipInputValidation bool
true to 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.cs

Writes a value given as TOML text, such as [1, 2] or 0xff.

public void WriteRawValue(string value, bool skipInputValidation = false)
Parameters
value string
The TOML text of a single value.
skipInputValidation bool
true to 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.cs

Writes the start of an array value.

public void WriteStartArray()

WriteStartInlineTable()

Utf8TomlWriter.cs

Writes the start of an inline table value.

public void WriteStartInlineTable()

WriteStringValue overloads

WriteStringValue(ReadOnlySpan<byte>, TomlStringStyle)

Utf8TomlWriter.cs

Writes a UTF-8 string value.

public void WriteStringValue(ReadOnlySpan<byte> utf8Value, TomlStringStyle style = TomlStringStyle.Basic)
Parameters
utf8Value ReadOnlySpan<byte>
The UTF-8 value.
style TomlStringStyle
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.cs

Writes a string value.

public void WriteStringValue(ReadOnlySpan<char> value, TomlStringStyle style = TomlStringStyle.Basic)
Parameters
value ReadOnlySpan<char>
The value.
style TomlStringStyle
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.cs

Writes a string value.

public void WriteStringValue(string value, TomlStringStyle style = TomlStringStyle.Basic)
Parameters
value string
The value.
style TomlStringStyle
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.cs

Writes a table header with a single key, such as [a].

public void WriteTableHeader(ReadOnlySpan<byte> utf8Key)
Parameters
utf8Key ReadOnlySpan<byte>
The UTF-8 key of the table.

WriteTableHeader(ReadOnlySpan<string>)

Utf8TomlWriter.cs

Writes a table header, such as [a.b].

public void WriteTableHeader(params scoped ReadOnlySpan<string> keys)
Parameters
keys ReadOnlySpan<string>
The segments of the key of the table.