DotEnvSerializer Class
Definition
- Assembly
- Bodu.Text.DotEnv.dll
- Package
- Bodu.Text.DotEnv 1.0.0
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
sourceStreamThe source stream.
optionsDotEnvSerializerOptionsThe serializer options, or null to use the defaults.
cancellationTokenCancellationTokenA token that cancels the stream copy.
Returns
- ValueTask<T>
A task that yields the deserialized value.
Type Parameters
TThe target type.
Exceptions
- ArgumentNullException
Thrown when
sourceis 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
sourceStreamThe source stream.
optionsDotEnvSerializerOptionsThe serializer options, or null to use the defaults.
Returns
- T
The deserialized value.
Type Parameters
TThe target type.
Exceptions
- ArgumentNullException
Thrown when
sourceis 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
utf8DotEnvReadOnlySpan<byte>The DotEnv source bytes.
optionsDotEnvSerializerOptionsThe serializer options, or null to use the defaults.
Returns
- T
The deserialized value.
Type Parameters
TThe 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
textstringThe DotEnv source text.
optionsDotEnvSerializerOptionsThe serializer options, or null to use the defaults.
Returns
- T
The deserialized value.
Type Parameters
TThe target type.
Exceptions
- ArgumentNullException
Thrown when
textis 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
destinationStreamThe destination stream.
valueTThe value to serialize.
optionsDotEnvSerializerOptionsThe serializer options, or null to use the defaults.
cancellationTokenCancellationTokenA token that cancels the final stream write.
Returns
- ValueTask
A task that completes when the DotEnv has been written.
Type Parameters
TThe type of value to serialize.
Exceptions
- ArgumentNullException
Thrown when
destinationis null.- DotEnvSerializationException
Thrown when
Tcannot 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
destinationIBufferWriter<byte>The buffer writer that receives the DotEnv bytes.
valueTThe value to serialize.
optionsDotEnvSerializerOptionsThe serializer options, or null to use the defaults.
Type Parameters
TThe type of value to serialize.
Exceptions
- ArgumentNullException
Thrown when
destinationis null.- DotEnvSerializationException
Thrown when
Tcannot 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
valueTThe value to serialize.
optionsDotEnvSerializerOptionsThe serializer options, or null to use the defaults.
Returns
- string
The DotEnv text.
Type Parameters
TThe type of value to serialize.
Exceptions
- DotEnvSerializationException
Thrown when
Tcannot be mapped to a DotEnv object.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |