IniSerializer Class
Definition
Provides methods for serializing .NET objects to INI text and deserializing INI text into .NET objects, shaped after
System.Text.Json's JsonSerializer.
public static class IniSerializer
- Inheritance
-
IniSerializer
- Inherited Members
Remarks
INI models a two-level object-of-objects, so the serializer maps a root POCO (or string-keyed dictionary) whose scalar-convertible members become global keys and whose object-shaped members - section POCOs or IDictionary<TKey, TValue> values keyed by string - become sections. A member nested beyond the second level is rejected. Property names honour PropertyNamingPolicy and the PropertyNameAttribute family; the callback interfaces (IOnSerializing and its siblings) fire around the mapping.
Deserialization always routes through the normalized document model, applying the options' duplicate-section and duplicate-key policies, because duplicate-section merge declares structure out of source order. The asynchronous stream overloads buffer the entire document rather than streaming it; only the stream copy is asynchronous.
Methods
DeserializeAsync<T>(Stream, IniSerializerOptions?, CancellationToken)
Asynchronously deserializes the INI content of the supplied stream into a value of type
T.
[RequiresUnreferencedCode("Reflection-based INI serialization may require members that trimming cannot statically determine.")]
[RequiresDynamicCode("Reflection-based INI serialization may require runtime code generation.")]
public static ValueTask<T> DeserializeAsync<T>(Stream source, IniSerializerOptions? options = null, CancellationToken cancellationToken = default)
Parameters
sourceStreamThe source stream.
optionsIniSerializerOptionsThe 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.- IniFormatException
Thrown when the content is not valid INI.
- IniSerializationException
Thrown when the document cannot be mapped to
T.
DeserializeSection<TSection>(ReadOnlySpan<byte>, string, IIniSectionFactory<TSection>, IniSerializerOptions?)
Deserializes one section of the specified UTF-8 INI bytes using a section factory instead of reflection.
public static TSection DeserializeSection<TSection>(ReadOnlySpan<byte> utf8Ini, string sectionName, IIniSectionFactory<TSection> factory, IniSerializerOptions? options = null)
Parameters
utf8IniReadOnlySpan<byte>The INI source bytes.
sectionNamestringThe section name, or an empty string to bind the document's global keys.
factoryIIniSectionFactory<TSection>The section factory that binds the entries.
optionsIniSerializerOptionsThe serializer options, or null to use the defaults.
Returns
- TSection
The deserialized section.
Type Parameters
TSectionThe section type.
Exceptions
- ArgumentNullException
Thrown when
sectionNameorfactoryis null.- IniFormatException
Thrown when the bytes are not valid INI, or when a duplicate section, duplicate key, or global-key/section-name collision violates the configured policies.
- IniSerializationException
Thrown when the document does not contain a section named
sectionName.
DeserializeSection<TSection>(string, string, IIniSectionFactory<TSection>, IniSerializerOptions?)
Deserializes one section of the specified INI text using a section factory instead of reflection.
public static TSection DeserializeSection<TSection>(string text, string sectionName, IIniSectionFactory<TSection> factory, IniSerializerOptions? options = null)
Parameters
textstringThe INI source text.
sectionNamestringThe section name, or an empty string to bind the document's global keys.
factoryIIniSectionFactory<TSection>The section factory that binds the entries.
optionsIniSerializerOptionsThe serializer options, or null to use the defaults.
Returns
- TSection
The deserialized section.
Type Parameters
TSectionThe section type.
Exceptions
- ArgumentNullException
Thrown when
text,sectionName, orfactoryis null.- IniFormatException
Thrown when the text is not valid INI.
- IniSerializationException
Thrown when the document does not contain a section named
sectionName.
Deserialize<T>(Stream, IniSerializerOptions?)
Deserializes the INI content of the supplied stream into a value of type T.
[RequiresUnreferencedCode("Reflection-based INI serialization may require members that trimming cannot statically determine.")]
[RequiresDynamicCode("Reflection-based INI serialization may require runtime code generation.")]
public static T Deserialize<T>(Stream source, IniSerializerOptions? options = null)
Parameters
sourceStreamThe source stream.
optionsIniSerializerOptionsThe serializer options, or null to use the defaults.
Returns
- T
The deserialized value.
Type Parameters
TThe target type.
Exceptions
- ArgumentNullException
Thrown when
sourceis null.- IniFormatException
Thrown when the content is not valid INI.
- IniSerializationException
Thrown when the document cannot be mapped to
T.
Deserialize<T>(ReadOnlySpan<byte>, IniSerializerOptions?)
Deserializes the specified UTF-8 INI bytes into a value of type T.
[RequiresUnreferencedCode("Reflection-based INI serialization may require members that trimming cannot statically determine.")]
[RequiresDynamicCode("Reflection-based INI serialization may require runtime code generation.")]
public static T Deserialize<T>(ReadOnlySpan<byte> utf8Ini, IniSerializerOptions? options = null)
Parameters
utf8IniReadOnlySpan<byte>The INI source bytes.
optionsIniSerializerOptionsThe serializer options, or null to use the defaults.
Returns
- T
The deserialized value.
Type Parameters
TThe target type.
Exceptions
- IniFormatException
Thrown when the bytes are not valid INI, or when a duplicate section, duplicate key, or global-key/section-name collision violates the configured policies.
- IniSerializationException
Thrown when the document cannot be mapped to
T.
Deserialize<T>(string, IniSerializerOptions?)
Deserializes the specified INI text into a value of type T.
[RequiresUnreferencedCode("Reflection-based INI serialization may require members that trimming cannot statically determine.")]
[RequiresDynamicCode("Reflection-based INI serialization may require runtime code generation.")]
public static T Deserialize<T>(string text, IniSerializerOptions? options = null)
Parameters
textstringThe INI source text.
optionsIniSerializerOptionsThe serializer options, or null to use the defaults.
Returns
- T
The deserialized value.
Type Parameters
TThe target type.
Exceptions
- ArgumentNullException
Thrown when
textis null.- IniFormatException
Thrown when the text is not valid INI.
- IniSerializationException
Thrown when the document cannot be mapped to
T.
SerializeAsync<T>(Stream, T, IniSerializerOptions?, CancellationToken)
Asynchronously serializes the specified value as INI to the supplied stream.
[RequiresUnreferencedCode("Reflection-based INI serialization may require members that trimming cannot statically determine.")]
[RequiresDynamicCode("Reflection-based INI serialization may require runtime code generation.")]
public static ValueTask SerializeAsync<T>(Stream destination, T value, IniSerializerOptions? options = null, CancellationToken cancellationToken = default)
Parameters
destinationStreamThe destination stream.
valueTThe value to serialize.
optionsIniSerializerOptionsThe serializer options, or null to use the defaults.
cancellationTokenCancellationTokenA token that cancels the final stream write.
Returns
- ValueTask
A task that completes when the INI has been written.
Type Parameters
TThe type of value to serialize.
Exceptions
- ArgumentNullException
Thrown when
destinationis null.- IniSerializationException
Thrown when
Tcannot be mapped to an INI document.
SerializeSection<TSection>(IBufferWriter<byte>, string, TSection, IIniSectionFactory<TSection>, IniSerializerOptions?)
Serializes the specified value as one INI section to the supplied buffer writer using a section factory instead of reflection.
public static void SerializeSection<TSection>(IBufferWriter<byte> destination, string sectionName, TSection value, IIniSectionFactory<TSection> factory, IniSerializerOptions? options = null)
Parameters
destinationIBufferWriter<byte>The buffer writer that receives the INI bytes.
sectionNamestringThe section name, or an empty string to write the entries as global keys.
valueTSectionThe section value to serialize.
factoryIIniSectionFactory<TSection>The section factory that supplies the entries.
optionsIniSerializerOptionsThe serializer options, or null to use the defaults.
Type Parameters
TSectionThe section type.
Exceptions
- ArgumentNullException
Thrown when
destination,sectionName,value, orfactoryis null.
SerializeSection<TSection>(string, TSection, IIniSectionFactory<TSection>, IniSerializerOptions?)
Serializes the specified value as one INI section using a section factory instead of reflection.
public static string SerializeSection<TSection>(string sectionName, TSection value, IIniSectionFactory<TSection> factory, IniSerializerOptions? options = null)
Parameters
sectionNamestringThe section name, or an empty string to write the entries as global keys.
valueTSectionThe section value to serialize.
factoryIIniSectionFactory<TSection>The section factory that supplies the entries.
optionsIniSerializerOptionsThe serializer options, or null to use the defaults.
Returns
- string
The INI text.
Type Parameters
TSectionThe section type.
Exceptions
- ArgumentNullException
Thrown when
sectionName,value, orfactoryis null.
Serialize<T>(IBufferWriter<byte>, T, IniSerializerOptions?)
Serializes the specified value as INI to the supplied buffer writer.
[RequiresUnreferencedCode("Reflection-based INI serialization may require members that trimming cannot statically determine.")]
[RequiresDynamicCode("Reflection-based INI serialization may require runtime code generation.")]
public static void Serialize<T>(IBufferWriter<byte> destination, T value, IniSerializerOptions? options = null)
Parameters
destinationIBufferWriter<byte>The buffer writer that receives the INI bytes.
valueTThe value to serialize.
optionsIniSerializerOptionsThe serializer options, or null to use the defaults.
Type Parameters
TThe type of value to serialize.
Exceptions
- ArgumentNullException
Thrown when
destinationis null.- IniSerializationException
Thrown when
Tcannot be mapped to an INI document.
Serialize<T>(T, IniSerializerOptions?)
Serializes the specified value to INI text.
[RequiresUnreferencedCode("Reflection-based INI serialization may require members that trimming cannot statically determine.")]
[RequiresDynamicCode("Reflection-based INI serialization may require runtime code generation.")]
public static string Serialize<T>(T value, IniSerializerOptions? options = null)
Parameters
valueTThe value to serialize.
optionsIniSerializerOptionsThe serializer options, or null to use the defaults.
Returns
- string
The INI text.
Type Parameters
TThe type of value to serialize.
Exceptions
- IniSerializationException
Thrown when
Tcannot be mapped to an INI document.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |