Table of Contents

TomlValue Class

Definition

Namespace
Bodu.Text.Toml.Nodes
Assembly
Bodu.Text.Toml.dll
Package
Bodu.Text.Toml 1.0.0
Source
TomlValue.cs

Represents a scalar TOML value within a node tree - one of the eight TOML scalar kinds: a string, a 64-bit integer, a floating-point number, a Boolean, or one of the four date-time kinds.

public sealed class TomlValue : TomlNode
Inheritance
TomlValue
Inherited Members
Extension Methods

Remarks

The stored kind is fixed at construction and reported by GetValueKind(). A scalar reads back as the CLR type that matches its kind; an integer additionally reads back as any fixed-width integer type through a checked conversion, and a float reads back as either double or float.

// Create wraps each TOML scalar kind - including the four native temporal kinds.
TomlValue text = TomlValue.Create("orders");
TomlValue offset = TomlValue.Create(new DateTimeOffset(2026, 1, 15, 8, 30, 0, TimeSpan.FromHours(10)));
TomlValue local = TomlValue.Create(new DateOnly(2026, 1, 15));
TomlValue time = TomlValue.Create(new TimeOnly(8, 30));

var date = local.GetValue<DateOnly>();   // 2026-01-15

Methods

Create(bool)

Creates a TomlValue holding the supplied Boolean value.

public static TomlValue Create(bool value)

Parameters

value bool

The Boolean value.

Returns

TomlValue

A new Boolean node.

Create(DateOnly)

Creates a TomlValue holding the supplied local date value.

public static TomlValue Create(DateOnly value)

Parameters

value DateOnly

The local date value.

Returns

TomlValue

A new local-date node.

Create(DateTime)

Creates a TomlValue holding the supplied local date-time value.

public static TomlValue Create(DateTime value)

Parameters

value DateTime

The local date-time value.

Returns

TomlValue

A new local-date-time node.

Create(DateTimeOffset)

Creates a TomlValue holding the supplied offset date-time value.

public static TomlValue Create(DateTimeOffset value)

Parameters

value DateTimeOffset

The offset date-time value.

Returns

TomlValue

A new offset-date-time node.

Create(double)

Creates a TomlValue holding the supplied floating-point value.

public static TomlValue Create(double value)

Parameters

value double

The floating-point value.

Returns

TomlValue

A new floating-point node.

Create(int)

Creates a TomlValue holding the supplied 32-bit integer.

public static TomlValue Create(int value)

Parameters

value int

The integer value.

Returns

TomlValue

A new integer-valued node.

Create(long)

Creates a TomlValue holding the supplied 64-bit integer.

public static TomlValue Create(long value)

Parameters

value long

The integer value.

Returns

TomlValue

A new integer-valued node.

Create(string)

Creates a TomlValue holding the supplied string.

public static TomlValue Create(string value)

Parameters

value string

The string value.

Returns

TomlValue

A new string-valued node.

Exceptions

ArgumentNullException

Thrown when value is null.

Create(TimeOnly)

Creates a TomlValue holding the supplied local time value.

public static TomlValue Create(TimeOnly value)

Parameters

value TimeOnly

The local time value.

Returns

TomlValue

A new local-time node.

DeepClone()

Creates a deep copy of this node and its entire subtree.

public override TomlNode DeepClone()

Returns

TomlNode

An independent clone with no parent.

GetValueKind()

Gets the kind of value this node represents.

public override TomlValueKind GetValueKind()

Returns

TomlValueKind

The TomlValueKind of this node.

GetValue<T>()

Returns the scalar value converted to the requested type.

public T GetValue<T>()

Returns

T

The converted scalar value.

Type Parameters

T

The type to convert the scalar value to.

Exceptions

InvalidOperationException

Thrown when the stored value cannot be converted to T.

ToString()

Returns a string representation of this node.

public override string ToString()

Returns

string

A textual rendering of this node.

TryGetValue<T>(out T)

Attempts to convert the scalar value to the requested type.

public bool TryGetValue<T>(out T value)

Parameters

value T

When this method returns true, the converted value; otherwise the default value of T.

Returns

bool

true when the stored value was converted to T; otherwise false.

Type Parameters

T

The type to convert the scalar value to.

Remarks

A string value converts to string; an integer value converts to any fixed-width integer type through a checked conversion that fails when the value is out of range; a float value converts to double or float; a Boolean value converts to bool; and each date-time kind converts to its matching CLR type. Any other combination of stored kind and requested type yields false.

WriteTo(Utf8TomlWriter)

Writes the normalized TOML encoding of this node to the supplied writer.

public override void WriteTo(Utf8TomlWriter writer)

Parameters

writer Utf8TomlWriter

The destination writer.

Remarks

Because a TOML document's root must be a table, the writer produces a document only when the outermost node is a TomlObject. Writing a node whose root is a scalar or array yields no document; use ToUtf8Bytes() to obtain the normalized bytes, which validates the table-root rule.

Exceptions

TomlSerializationException

Thrown when the subtree rooted at this node contains a null entry, which has no TOML representation.

Applies to

ProductVersions
.NET8, 10