Table of Contents

DotEnvSerializer Class

Definition

Namespace
Bodu.Text.DotEnv
Assembly
Bodu.Text.DotEnv.dll
Package
Bodu.Text.DotEnv 1.0.0
Source
DotEnvSerializer.Binder.cs

Provides methods for serializing .NET objects to DotEnv text and deserializing DotEnv text into .NET objects, shaped after System.Text.Json's JsonSerializer.

public static class DotEnvSerializer
Inheritance
DotEnvSerializer
Inherited Members

Remarks

DotEnv models a flat, ordered object of string-valued keys, so the serializer maps a POCO (or an IDictionary<TKey, TValue> keyed by string) whose members are strings or types convertible from a string. Property names honour PropertyNamingPolicy and the PropertyNameAttribute family; the callback interfaces (IOnSerializing and its siblings) fire around the mapping.

The asynchronous stream overloads buffer the entire document rather than streaming it; only the stream copy is asynchronous.

Methods

DeserializeAsync<T>(Stream, DotEnvSerializerOptions?, CancellationToken)

Asynchronously deserializes the DotEnv content of the supplied stream into a value of type T.

[RequiresUnreferencedCode("Reflection-based DotEnv serialization may require members that trimming cannot statically determine.")]
[RequiresDynamicCode("Reflection-based DotEnv serialization may require runtime code generation.")]
public static ValueTask<T> DeserializeAsync<T>(Stream source, DotEnvSerializerOptions? options = null, CancellationToken cancellationToken = default)

Parameters

source Stream

The source stream.

options DotEnvSerializerOptions

The serializer options, or null to use the defaults.

cancellationToken CancellationToken

A token that cancels the stream copy.

Returns

ValueTask<T>

A task that yields the deserialized value.

Type Parameters

T

The target type.

Exceptions

ArgumentNullException

Thrown when source is null.

DotEnvFormatException

Thrown when the content is not valid DotEnv.

DotEnvSerializationException

Thrown when the document cannot be mapped to T.

Deserialize<T>(Stream, DotEnvSerializerOptions?)

Deserializes the DotEnv content of the supplied stream into a value of type T.

[RequiresUnreferencedCode("Reflection-based DotEnv serialization may require members that trimming cannot statically determine.")]
[RequiresDynamicCode("Reflection-based DotEnv serialization may require runtime code generation.")]
public static T Deserialize<T>(Stream source, DotEnvSerializerOptions? options = null)

Parameters

source Stream

The source stream.

options DotEnvSerializerOptions

The serializer options, or null to use the defaults.

Returns

T

The deserialized value.

Type Parameters

T

The target type.

Exceptions

ArgumentNullException

Thrown when source is null.

DotEnvFormatException

Thrown when the content is not valid DotEnv.

DotEnvSerializationException

Thrown when the document cannot be mapped to T.

Deserialize<T>(ReadOnlySpan<byte>, DotEnvSerializerOptions?)

Deserializes the specified UTF-8 DotEnv bytes into a value of type T.

[RequiresUnreferencedCode("Reflection-based DotEnv serialization may require members that trimming cannot statically determine.")]
[RequiresDynamicCode("Reflection-based DotEnv serialization may require runtime code generation.")]
public static T Deserialize<T>(ReadOnlySpan<byte> utf8DotEnv, DotEnvSerializerOptions? options = null)

Parameters

utf8DotEnv ReadOnlySpan<byte>

The DotEnv source bytes.

options DotEnvSerializerOptions

The serializer options, or null to use the defaults.

Returns

T

The deserialized value.

Type Parameters

T

The target type.

Exceptions

DotEnvFormatException

Thrown when the bytes are not valid DotEnv.

DotEnvSerializationException

Thrown when the document cannot be mapped to T.

Deserialize<T>(string, DotEnvSerializerOptions?)

Deserializes the specified DotEnv text into a value of type T.

[RequiresUnreferencedCode("Reflection-based DotEnv serialization may require members that trimming cannot statically determine.")]
[RequiresDynamicCode("Reflection-based DotEnv serialization may require runtime code generation.")]
public static T Deserialize<T>(string text, DotEnvSerializerOptions? options = null)

Parameters

text string

The DotEnv source text.

options DotEnvSerializerOptions

The serializer options, or null to use the defaults.

Returns

T

The deserialized value.

Type Parameters

T

The target type.

Exceptions

ArgumentNullException

Thrown when text is null.

DotEnvFormatException

Thrown when the text is not valid DotEnv.

DotEnvSerializationException

Thrown when the document cannot be mapped to T.

SerializeAsync<T>(Stream, T, DotEnvSerializerOptions?, CancellationToken)

Asynchronously serializes the specified value as DotEnv to the supplied stream.

[RequiresUnreferencedCode("Reflection-based DotEnv serialization may require members that trimming cannot statically determine.")]
[RequiresDynamicCode("Reflection-based DotEnv serialization may require runtime code generation.")]
public static ValueTask SerializeAsync<T>(Stream destination, T value, DotEnvSerializerOptions? options = null, CancellationToken cancellationToken = default)

Parameters

destination Stream

The destination stream.

value T

The value to serialize.

options DotEnvSerializerOptions

The serializer options, or null to use the defaults.

cancellationToken CancellationToken

A token that cancels the final stream write.

Returns

ValueTask

A task that completes when the DotEnv has been written.

Type Parameters

T

The type of value to serialize.

Exceptions

ArgumentNullException

Thrown when destination is null.

DotEnvSerializationException

Thrown when T cannot be mapped to a DotEnv object.

Serialize<T>(IBufferWriter<byte>, T, DotEnvSerializerOptions?)

Serializes the specified value as DotEnv to the supplied buffer writer.

[RequiresUnreferencedCode("Reflection-based DotEnv serialization may require members that trimming cannot statically determine.")]
[RequiresDynamicCode("Reflection-based DotEnv serialization may require runtime code generation.")]
public static void Serialize<T>(IBufferWriter<byte> destination, T value, DotEnvSerializerOptions? options = null)

Parameters

destination IBufferWriter<byte>

The buffer writer that receives the DotEnv bytes.

value T

The value to serialize.

options DotEnvSerializerOptions

The serializer options, or null to use the defaults.

Type Parameters

T

The type of value to serialize.

Exceptions

ArgumentNullException

Thrown when destination is null.

DotEnvSerializationException

Thrown when T cannot be mapped to a DotEnv object.

Serialize<T>(T, DotEnvSerializerOptions?)

Serializes the specified value to DotEnv text.

[RequiresUnreferencedCode("Reflection-based DotEnv serialization may require members that trimming cannot statically determine.")]
[RequiresDynamicCode("Reflection-based DotEnv serialization may require runtime code generation.")]
public static string Serialize<T>(T value, DotEnvSerializerOptions? options = null)

Parameters

value T

The value to serialize.

options DotEnvSerializerOptions

The serializer options, or null to use the defaults.

Returns

string

The DotEnv text.

Type Parameters

T

The type of value to serialize.

Exceptions

DotEnvSerializationException

Thrown when T cannot be mapped to a DotEnv object.

Applies to

ProductVersions
.NET8, 10