Skip to content

Interface IConfigurationSerializer

Namespace: Void.Proxy.Api.Configurations.Serializer
Assembly: Void.Proxy.Api.dll

public interface IConfigurationSerializer

Methods

Deserialize<TConfiguration>(string)

Deserializes configuration text into an instance of TConfiguration.

TConfiguration Deserialize<TConfiguration>(string source) where TConfiguration : notnull

Parameters

source string

The serialized configuration text to parse and map; this value must not be null.

Returns

TConfiguration

A configuration instance cast to TConfiguration.

Type Parameters

TConfiguration

The expected configuration type.

Examples

var value = serializer.Deserialize<NetworkConfiguration>(sourceText);

Remarks

This member is a typed wrapper around that passes typeof(TConfiguration) as the target type.

The built-in serializer implementation parses TOML and maps it with configured options, then validates that the resulting object can be cast to TConfiguration.

Exceptions

InvalidConfigurationException

Thrown when source cannot be parsed, when it cannot be mapped to TConfiguration, or when the mapped result is not assignable to TConfiguration.

See Also

IConfigurationSerializer.Deserialize(string, Type)

Deserialize(string, Type)

Deserializes configuration text into an instance of a runtime-specified configuration type.

object Deserialize(string source, Type configurationType)

Parameters

source string

The serialized configuration text to parse and map; this value must not be null.

configurationType Type

The target configuration to materialize.

Returns

object

The deserialized configuration object typed as .

Examples

var value = serializer.Deserialize(sourceText, typeof(NetworkConfiguration));

Remarks

The built-in serializer implementation first parses source as TOML, then maps the parsed document to configurationType using serializer options.

This overload performs no compile-time type checks, so callers are responsible for casting the returned value to the expected runtime type.

Exceptions

InvalidConfigurationException

Thrown when parsing fails or when the serialized content cannot be converted to configurationType.

See Also

IConfigurationSerializer.Deserialize<TConfiguration>(string), Type

Serialize<TConfiguration>()

Serializes a default-value instance of TConfiguration to configuration text.

string Serialize<TConfiguration>() where TConfiguration : notnull

Returns

string

A containing the serialized configuration text for a freshly constructed TConfiguration instance populated with its default property values.

Type Parameters

TConfiguration

The configuration type whose default representation is serialized. Must be a non-nullable reference or value type.

Examples

// Produce a template TOML configuration file with all default values.
string template = serializer.Serialize<NetworkConfiguration>();
await File.WriteAllTextAsync("network.toml", template);

Remarks

This overload is a convenience wrapper that calls with null as the configuration argument, which in turn delegates to with typeof(TConfiguration).

Because no existing instance is provided, the underlying serializer constructs a fresh TConfiguration instance using its default constructor and fills all properties with their default values before serializing. The result is therefore useful for generating template or scaffold configuration files.

Exceptions

InvalidConfigurationException

Thrown when the serializer cannot construct a default TConfiguration instance or cannot convert it to the target configuration format.

See Also

IConfigurationSerializer.Serialize<TConfiguration>(TConfiguration?), IConfigurationSerializer.Serialize(object?, Type)

Serialize(object)

string Serialize(object configuration)

Parameters

configuration object

Returns

string

Serialize<TConfiguration>(TConfiguration?)

string Serialize<TConfiguration>(TConfiguration? configuration) where TConfiguration : notnull

Parameters

configuration TConfiguration?

Returns

string

Type Parameters

TConfiguration

Serialize(object?, Type)

Serializes the given configuration object — or a default instance of configurationType when configuration is null — to configuration text.

string Serialize(object? configuration, Type configurationType)

Parameters

configuration object?

The configuration object to serialize, or null to serialize a freshly constructed default instance of configurationType.

configurationType Type

The that describes the configuration structure. When configuration is non-null, this must match or be a base type of the runtime type of configuration.

Returns

string

A containing the serialized TOML representation of the configuration object.

Examples

// Serialize an existing configuration instance with an explicit type.
string toml = serializer.Serialize(myConfig, typeof(NetworkConfiguration));

// Serialize a default instance by passing null for the configuration argument.
string defaults = serializer.Serialize(null, typeof(NetworkConfiguration));

Remarks

This is the canonical serialization overload to which all other Serialize overloads ultimately delegate. It performs the following steps:

  1. If configuration is null, a new instance of configurationType is created with all properties set to their default values.
  2. The object is mapped to a TomlDocument together with any inline or preceding comments declared via .
  3. The serialized TOML text is returned via TomlDocument.SerializedValue.

Exceptions

InvalidConfigurationException

Thrown when the underlying TOML library raises a TomlException during object mapping or serialization. The exception message includes the original TOML error details.

See Also

IConfigurationSerializer.Serialize<TConfiguration>(), IConfigurationSerializer.Serialize<TConfiguration>(TConfiguration?), IConfigurationSerializer.Deserialize(string, Type)