DelimitedSerializer Class
Definition
- Assembly
- Bodu.Text.Delimited.dll
- Package
- Bodu.Text.Delimited 1.0.0
Provides methods for serializing collections of .NET records to delimited (RFC 4180 CSV/TSV) text and deserializing
delimited text into records, shaped after System.Text.Json's JsonSerializer.
public static class DelimitedSerializer
- Inheritance
-
DelimitedSerializer
- Inherited Members
Remarks
A delimited document is an array of records, so the serializer maps an IEnumerable<T> whose element type is a POCO (mapped column-by-column to the header) or a positional string array. Column names honour PropertyNamingPolicy and the PropertyNameAttribute family.
The buffered stream overloads mirror the sibling quartet libraries. The DeserializeAsyncEnumerableAsync<TRecord>(Stream, DelimitedSerializerOptions?, CancellationToken) and the IAsyncEnumerable<T> serialize overload are truly incremental: records are parsed and yielded as stream chunks arrive, and written and flushed in bounded batches, so neither surface materializes the document.
Methods
DeserializeAsyncEnumerableAsync<TRecord>(Stream, DelimitedSerializerOptions?, CancellationToken)
Asynchronously deserializes the delimited content of the supplied stream, reading incrementally and yielding each record as soon as its terminating line ending has been observed.
[RequiresUnreferencedCode("Reflection-based delimited serialization may require members that trimming cannot statically determine.")]
[RequiresDynamicCode("Reflection-based delimited serialization may require runtime code generation.")]
public static IAsyncEnumerable<TRecord> DeserializeAsyncEnumerableAsync<TRecord>(Stream source, DelimitedSerializerOptions? options = null, CancellationToken cancellationToken = default)
Parameters
sourceStreamThe source stream.
optionsDelimitedSerializerOptionsThe serializer options, or null to use the defaults.
cancellationTokenCancellationTokenA token that cancels the operation.
Returns
- IAsyncEnumerable<TRecord>
An asynchronous sequence of records.
Type Parameters
TRecordThe record type.
Remarks
The stream is consumed in segments: only the bytes of records not yet terminated remain buffered, so memory use is bounded by the longest single record rather than the document. A record that ends exactly at the current buffer boundary is held back until the next segment (or the end of the stream) proves it complete, because more fields could still follow. A malformed tail is retried as later segments arrive and only surfaces as a DelimitedFormatException once the end of the stream confirms it.
Exceptions
- ArgumentNullException
Thrown when
sourceis null.- DelimitedFormatException
Thrown when the content is not valid delimited data.
- DelimitedSerializationException
Thrown when a record cannot be mapped.
Deserialize<TRecord>(Stream, DelimitedSerializerOptions?)
Deserializes the delimited content of the supplied stream into a list of records.
[RequiresUnreferencedCode("Reflection-based delimited serialization may require members that trimming cannot statically determine.")]
[RequiresDynamicCode("Reflection-based delimited serialization may require runtime code generation.")]
public static List<TRecord> Deserialize<TRecord>(Stream source, DelimitedSerializerOptions? options = null)
Parameters
sourceStreamThe source stream.
optionsDelimitedSerializerOptionsThe serializer options, or null to use the defaults.
Returns
- List<TRecord>
The list of deserialized records.
Type Parameters
TRecordThe record type.
Exceptions
- ArgumentNullException
Thrown when
sourceis null.- DelimitedFormatException
Thrown when the content is not valid delimited data.
- DelimitedSerializationException
Thrown when a record cannot be mapped.
Deserialize<TRecord>(Stream, IDelimitedRecordFactory<TRecord>, DelimitedSerializerOptions?)
Deserializes the delimited content of the supplied stream into a list of records using a record factory instead of reflection.
public static List<TRecord> Deserialize<TRecord>(Stream source, IDelimitedRecordFactory<TRecord> factory, DelimitedSerializerOptions? options = null)
Parameters
sourceStreamThe source stream.
factoryIDelimitedRecordFactory<TRecord>The record factory that binds decoded rows.
optionsDelimitedSerializerOptionsThe serializer options, or null to use the defaults.
Returns
- List<TRecord>
The list of deserialized records.
Type Parameters
TRecordThe record type.
Exceptions
- ArgumentNullException
Thrown when
sourceorfactoryis null.- DelimitedFormatException
Thrown when the content is not valid delimited data.
Deserialize<TRecord>(ReadOnlySpan<byte>, DelimitedSerializerOptions?)
Deserializes the specified UTF-8 delimited bytes into a list of records.
[RequiresUnreferencedCode("Reflection-based delimited serialization may require members that trimming cannot statically determine.")]
[RequiresDynamicCode("Reflection-based delimited serialization may require runtime code generation.")]
public static List<TRecord> Deserialize<TRecord>(ReadOnlySpan<byte> utf8Delimited, DelimitedSerializerOptions? options = null)
Parameters
utf8DelimitedReadOnlySpan<byte>The delimited source bytes.
optionsDelimitedSerializerOptionsThe serializer options, or null to use the defaults.
Returns
- List<TRecord>
The list of deserialized records.
Type Parameters
TRecordThe record type.
Exceptions
- DelimitedFormatException
Thrown when the bytes are not valid delimited data.
- DelimitedSerializationException
Thrown when a record cannot be mapped.
Deserialize<TRecord>(ReadOnlySpan<byte>, IDelimitedRecordFactory<TRecord>, DelimitedSerializerOptions?)
Deserializes the specified UTF-8 delimited bytes into a list of records using a record factory instead of reflection.
public static List<TRecord> Deserialize<TRecord>(ReadOnlySpan<byte> utf8Delimited, IDelimitedRecordFactory<TRecord> factory, DelimitedSerializerOptions? options = null)
Parameters
utf8DelimitedReadOnlySpan<byte>The delimited source bytes.
factoryIDelimitedRecordFactory<TRecord>The record factory that binds decoded rows.
optionsDelimitedSerializerOptionsThe serializer options, or null to use the defaults.
Returns
- List<TRecord>
The list of deserialized records.
Type Parameters
TRecordThe record type.
Exceptions
- ArgumentNullException
Thrown when
factoryis null.- DelimitedFormatException
Thrown when the bytes are not valid delimited data.
Deserialize<TRecord>(string, DelimitedSerializerOptions?)
Deserializes the specified delimited text into a list of records.
[RequiresUnreferencedCode("Reflection-based delimited serialization may require members that trimming cannot statically determine.")]
[RequiresDynamicCode("Reflection-based delimited serialization may require runtime code generation.")]
public static List<TRecord> Deserialize<TRecord>(string text, DelimitedSerializerOptions? options = null)
Parameters
textstringThe delimited source text.
optionsDelimitedSerializerOptionsThe serializer options, or null to use the defaults.
Returns
- List<TRecord>
The list of deserialized records.
Type Parameters
TRecordThe record type.
Exceptions
- ArgumentNullException
Thrown when
textis null.- DelimitedFormatException
Thrown when the text is not valid delimited data.
- DelimitedSerializationException
Thrown when a record cannot be mapped.
Deserialize<TRecord>(string, IDelimitedRecordFactory<TRecord>, DelimitedSerializerOptions?)
Deserializes the specified delimited text into a list of records using a record factory instead of reflection.
public static List<TRecord> Deserialize<TRecord>(string text, IDelimitedRecordFactory<TRecord> factory, DelimitedSerializerOptions? options = null)
Parameters
textstringThe delimited source text.
factoryIDelimitedRecordFactory<TRecord>The record factory that binds decoded rows.
optionsDelimitedSerializerOptionsThe serializer options, or null to use the defaults.
Returns
- List<TRecord>
The list of deserialized records.
Type Parameters
TRecordThe record type.
Exceptions
- ArgumentNullException
Thrown when
textorfactoryis null.- DelimitedFormatException
Thrown when the text is not valid delimited data.
SerializeAsync<TRecord>(Stream, IAsyncEnumerable<TRecord>, DelimitedSerializerOptions?, CancellationToken)
Asynchronously serializes an asynchronous sequence of records as delimited text to the supplied stream, writing incrementally as records arrive.
[RequiresUnreferencedCode("Reflection-based delimited serialization may require members that trimming cannot statically determine.")]
[RequiresDynamicCode("Reflection-based delimited serialization may require runtime code generation.")]
public static ValueTask SerializeAsync<TRecord>(Stream destination, IAsyncEnumerable<TRecord> records, DelimitedSerializerOptions? options = null, CancellationToken cancellationToken = default)
Parameters
destinationStreamThe destination stream.
recordsIAsyncEnumerable<TRecord>The asynchronous record sequence.
optionsDelimitedSerializerOptionsThe serializer options, or null to use the defaults.
cancellationTokenCancellationTokenA token that cancels the operation.
Returns
- ValueTask
A task that completes when all records have been written.
Type Parameters
TRecordThe record type.
Remarks
The sequence is never materialized: each record is encoded as it is produced and flushed to
destination in bounded batches, so memory use is independent of the sequence length. The
header row (for a POCO record type, unless NoHeader is set) is emitted
before the first record.
Exceptions
- ArgumentNullException
Thrown when
destinationorrecordsis null.
SerializeAsync<T>(Stream, T, DelimitedSerializerOptions?, CancellationToken)
Asynchronously serializes the specified collection of records as delimited text to the supplied stream.
[RequiresUnreferencedCode("Reflection-based delimited serialization may require members that trimming cannot statically determine.")]
[RequiresDynamicCode("Reflection-based delimited serialization may require runtime code generation.")]
public static ValueTask SerializeAsync<T>(Stream destination, T value, DelimitedSerializerOptions? options = null, CancellationToken cancellationToken = default)
Parameters
destinationStreamThe destination stream.
valueTThe records to serialize.
optionsDelimitedSerializerOptionsThe serializer options, or null to use the defaults.
cancellationTokenCancellationTokenA token that cancels the final stream write.
Returns
- ValueTask
A task that completes when the delimited text has been written.
Type Parameters
TThe collection type.
Exceptions
- ArgumentNullException
Thrown when
destinationis null.- DelimitedSerializationException
Thrown when
Tis not a collection of records.
Serialize<TRecord>(IBufferWriter<byte>, IEnumerable<TRecord>, IDelimitedRecordFactory<TRecord>, DelimitedSerializerOptions?)
Serializes the specified records as delimited text to the supplied buffer writer using a record factory instead of reflection.
public static void Serialize<TRecord>(IBufferWriter<byte> destination, IEnumerable<TRecord> records, IDelimitedRecordFactory<TRecord> factory, DelimitedSerializerOptions? options = null)
Parameters
destinationIBufferWriter<byte>The buffer writer that receives the delimited bytes.
recordsIEnumerable<TRecord>The records to serialize.
factoryIDelimitedRecordFactory<TRecord>The record factory that supplies headers and field values.
optionsDelimitedSerializerOptionsThe serializer options, or null to use the defaults.
Type Parameters
TRecordThe record type.
Exceptions
- ArgumentNullException
Thrown when
destination,records, orfactoryis null.
Serialize<T>(IBufferWriter<byte>, T, DelimitedSerializerOptions?)
Serializes the specified collection of records as delimited text to the supplied buffer writer.
[RequiresUnreferencedCode("Reflection-based delimited serialization may require members that trimming cannot statically determine.")]
[RequiresDynamicCode("Reflection-based delimited serialization may require runtime code generation.")]
public static void Serialize<T>(IBufferWriter<byte> destination, T value, DelimitedSerializerOptions? options = null)
Parameters
destinationIBufferWriter<byte>The buffer writer that receives the delimited bytes.
valueTThe records to serialize.
optionsDelimitedSerializerOptionsThe serializer options, or null to use the defaults.
Type Parameters
TThe collection type.
Exceptions
- ArgumentNullException
Thrown when
destinationis null.- DelimitedSerializationException
Thrown when
Tis not a collection of records.
Serialize<TRecord>(IEnumerable<TRecord>, IDelimitedRecordFactory<TRecord>, DelimitedSerializerOptions?)
Serializes the specified records to delimited text using a record factory instead of reflection.
public static string Serialize<TRecord>(IEnumerable<TRecord> records, IDelimitedRecordFactory<TRecord> factory, DelimitedSerializerOptions? options = null)
Parameters
recordsIEnumerable<TRecord>The records to serialize.
factoryIDelimitedRecordFactory<TRecord>The record factory that supplies headers and field values.
optionsDelimitedSerializerOptionsThe serializer options, or null to use the defaults.
Returns
- string
The delimited text.
Type Parameters
TRecordThe record type.
Exceptions
- ArgumentNullException
Thrown when
recordsorfactoryis null.
Serialize<T>(T, DelimitedSerializerOptions?)
Serializes the specified collection of records to delimited text.
[RequiresUnreferencedCode("Reflection-based delimited serialization may require members that trimming cannot statically determine.")]
[RequiresDynamicCode("Reflection-based delimited serialization may require runtime code generation.")]
public static string Serialize<T>(T value, DelimitedSerializerOptions? options = null)
Parameters
valueTThe records to serialize.
optionsDelimitedSerializerOptionsThe serializer options, or null to use the defaults.
Returns
- string
The delimited text.
Type Parameters
TThe collection type.
Exceptions
- DelimitedSerializationException
Thrown when
Tis not a collection of records.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |