TomlReader Class

Reads TOML tokens for use by TomlConverter implementations.

public sealed class TomlReader

Remarks

The reader parses the whole document on the first TomlReader.Read, and returns the keys of a table defined in several places (for example an array of tables reopened after another table) as a single table.

Properties

NameDescription
ColumnGets the current column number (1-based).
CurrentSpanGets the optional source span associated with the current token.
IsInlineContainerGets a value indicating whether the current TomlTokenType.StartTable or TomlTokenType.StartArray token starts an inline table ({) or an inline array ([).
LineGets the current line number (1-based).
OptionsGets the serializer options associated with this reader instance.
PropertyNameGets the current property name when TomlReader.TokenType is TomlTokenType.PropertyName.
SourceNameGets the optional source name associated with the TOML payload (for example, a file path).
TokenTypeGets the current token type.

Gets the current column number (1-based).

public int Column { get; }

Property value

int

CurrentSpan

TomlReader.cs

Gets the optional source span associated with the current token.

public TomlSourceSpan? CurrentSpan { get; }

Property value

TomlSourceSpan?

IsInlineContainer

TomlReader.cs

Gets a value indicating whether the current TomlTokenType.StartTable or TomlTokenType.StartArray token starts an inline table ({) or an inline array ([).

public bool IsInlineContainer { get; }

Property value

bool

Remarks

It is false for a table or an array of tables opened by a header or a dotted key, which a later header or dotted key can extend, and for any other token.

Gets the current line number (1-based).

public int Line { get; }

Property value

int

Gets the serializer options associated with this reader instance.

public TomlSerializerOptions Options { get; }

Property value

TomlSerializerOptions

PropertyName

TomlReader.cs

Gets the current property name when TomlReader.TokenType is TomlTokenType.PropertyName.

public string? PropertyName { get; }

Property value

string?

SourceName

TomlReader.cs

Gets the optional source name associated with the TOML payload (for example, a file path).

public string? SourceName { get; }

Property value

string?

TokenType

TomlReader.cs

Gets the current token type.

public TomlTokenType TokenType { get; }

Property value

TomlTokenType

Methods

NameDescription
Create(TextReader, TomlSerializerOptions?)Creates a TOML reader over a text reader.
Create(string, TomlSerializerOptions?)Creates a TOML reader over a string payload.
CreateException(string)Creates a TomlException with the current source location (when available).
GetBoolean()Gets the current boolean value.
GetDecimal()Gets the current floating value as decimal.
GetDouble()Gets the current floating value.
GetInt64()Gets the current integer value.
GetRawText()Gets the raw source text represented by the current token.
GetString()Gets the current string value.
GetTomlDateTime()Gets the current TOML datetime value.
PropertyNameEquals(string)Checks whether the current property name matches the expected value, using an ordinal, case-sensitive comparison.
Read()Advances the reader to the next token.
Skip()Skips the current value.
TryGetPropertyNameHash(ulong)Attempts to get a stable hash for the current property name without materializing it.

Create overloads

Create(TextReader, TomlSerializerOptions?)

TomlReader.cs

Creates a TOML reader over a text reader.

public static TomlReader Create(TextReader reader, TomlSerializerOptions? options = null)
Parameters
reader TextReader
options TomlSerializerOptions?
Returns

TomlReader

Exceptions
TomlException
The input is longer than TomlSerializerOptions.MaxInputLength.

Create(string, TomlSerializerOptions?)

TomlReader.cs

Creates a TOML reader over a string payload.

public static TomlReader Create(string toml, TomlSerializerOptions? options = null)
Parameters
toml string
options TomlSerializerOptions?
Returns

TomlReader

Exceptions
TomlException
The input is longer than TomlSerializerOptions.MaxInputLength.

CreateException(string)

TomlReader.cs

Creates a TomlException with the current source location (when available).

public TomlException CreateException(string message)

Parameters

message string
The exception message.

Returns

TomlException

GetBoolean()

TomlReader.cs

Gets the current boolean value.

public bool GetBoolean()

Returns

bool

GetDecimal()

TomlReader.cs

Gets the current floating value as decimal.

public decimal GetDecimal()

Returns

decimal

Exceptions

TomlException
The value is not a number, or is outside the range of Decimal.

GetDouble()

TomlReader.cs

Gets the current floating value.

public double GetDouble()

Returns

double

GetInt64()

TomlReader.cs

Gets the current integer value.

public long GetInt64()

Returns

long

GetRawText()

TomlReader.cs

Gets the raw source text represented by the current token.

public string GetRawText()

Returns

string

Exceptions

TomlException
The current token does not have a source span.

Remarks

This API is intended for custom converters that need to preserve the original literal (for example, to implement exact parsing).

GetString()

TomlReader.cs

Gets the current string value.

public string GetString()

Returns

string

GetTomlDateTime()

TomlReader.cs

Gets the current TOML datetime value.

public TomlDateTime GetTomlDateTime()

PropertyNameEquals(string)

TomlReader.cs

Checks whether the current property name matches the expected value, using an ordinal, case-sensitive comparison.

public bool PropertyNameEquals(string expected)

Parameters

expected string
The expected property name.

Returns

bool true when the property name matches; otherwise false.

Exceptions

ArgumentNullException
expected is null.
TomlException
The current token does not have a source span.

Advances the reader to the next token.

public bool Read()

Returns

bool

Exceptions

TomlException
The TOML document is invalid. The first call reports errors anywhere in the document.

Skips the current value.

public void Skip()

TryGetPropertyNameHash(ulong)

TomlReader.cs

Attempts to get a stable hash for the current property name without materializing it.

public bool TryGetPropertyNameHash(out ulong hash)

Parameters

hash ulong
When this method returns true, contains the property-name hash (case-sensitive) computed by the parser.

Returns

bool true when the current token is a property name; otherwise false.