CsvSerializer Class
Reads CSV data as a list of objects, and writes objects as CSV data. Each record is an object; each column is a property or a field.
public static class CsvSerializerRemarks
The overloads that take a CsvSerializerOptions create the metadata of the type with reflection. They are not compatible with trimming and Native AOT: use the overloads that take a CsvTypeInfo<T> from a generated CsvSerializerContext instead.
The overloads that take a Stream read and write UTF-8 data without converting it to UTF-16.
Example
var people = new List<Person>
{
new() { Name = "Alice", Age = 30, BirthDate = new DateOnly(1995, 4, 12) },
new() { Name = "Bob, Jr.", Age = 25 },
};
var csv = CsvSerializer.Serialize(people);
Assert.Equal("Name,Age,BirthDate\r\nAlice,30,1995-04-12\r\n\"Bob, Jr.\",25,\r\n", csv);
var result = CsvSerializer.Deserialize<Person>(csv);
Assert.Equal("Bob, Jr.", result[1].Name);
Assert.Null(result[1].BirthDate); // An empty value is null for a nullable typeProperties
| Name | Description |
|---|---|
| IsReflectionEnabledByDefault | Gets a value indicating whether the overloads that take a CsvSerializerOptions can create metadata with reflection. |
IsReflectionEnabledByDefault
CsvSerializer.csGets a value indicating whether the overloads that take a CsvSerializerOptions can create metadata with reflection.
public static bool IsReflectionEnabledByDefault { get; }Property value
Remarks
This is a feature switch, disabled when publishing with Native AOT. Set the MeziantouFrameworkCsvIsReflectionEnabledByDefault MSBuild property to override it. When disabled, the trimmer removes the reflection-based code.
Methods
Deserialize overloads
Deserialize<T>(Stream, CsvSerializerOptions?)
CsvSerializer.csReads the records of UTF-8 CSV data from a Stream.
[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static List<T> Deserialize<T>(Stream utf8Stream, CsvSerializerOptions? options = null)Type parameters
T- The type of the records.
Parameters
utf8StreamStream- The stream to read the UTF-8 data from. It is not disposed.
optionsCsvSerializerOptions?- The options, or
nullto useCsvSerializerOptions.Default.
Returns
List<T> The records.
Exceptions
CsvException- The data is invalid or a value cannot be converted.
Deserialize<T>(Stream, CsvTypeInfo<T>)
CsvSerializer.csReads the records of UTF-8 CSV data from a Stream.
public static List<T> Deserialize<T>(Stream utf8Stream, CsvTypeInfo<T> typeInfo)Type parameters
T- The type of the records.
Parameters
utf8StreamStream- The stream to read the UTF-8 data from. It is not disposed.
typeInfoCsvTypeInfo<T>- The metadata of
T.
Returns
List<T> The records.
Exceptions
CsvException- The data is invalid or a value cannot be converted.
Deserialize<T>(TextReader, CsvSerializerOptions?)
CsvSerializer.csReads the records of CSV data from a TextReader.
[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static List<T> Deserialize<T>(TextReader reader, CsvSerializerOptions? options = null)Type parameters
T- The type of the records.
Parameters
readerTextReader- The reader to read the data from. It is not disposed.
optionsCsvSerializerOptions?- The options, or
nullto useCsvSerializerOptions.Default.
Returns
List<T> The records.
Exceptions
CsvException- The data is invalid or a value cannot be converted.
Deserialize<T>(TextReader, CsvTypeInfo<T>)
CsvSerializer.csReads the records of CSV data from a TextReader.
public static List<T> Deserialize<T>(TextReader reader, CsvTypeInfo<T> typeInfo)Type parameters
T- The type of the records.
Parameters
readerTextReader- The reader to read the data from. It is not disposed.
typeInfoCsvTypeInfo<T>- The metadata of
T.
Returns
List<T> The records.
Exceptions
CsvException- The data is invalid or a value cannot be converted.
Deserialize<T>(ReadOnlyMemory<byte>, CsvSerializerOptions?)
CsvSerializer.csReads the records of UTF-8 CSV data.
[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static List<T> Deserialize<T>(ReadOnlyMemory<byte> utf8Csv, CsvSerializerOptions? options = null)Type parameters
T- The type of the records.
Parameters
utf8CsvReadOnlyMemory<byte>- The UTF-8 encoded CSV data.
optionsCsvSerializerOptions?- The options, or
nullto useCsvSerializerOptions.Default.
Returns
List<T> The records.
Exceptions
CsvException- The data is invalid or a value cannot be converted.
Deserialize<T>(ReadOnlyMemory<byte>, CsvTypeInfo<T>)
CsvSerializer.csReads the records of UTF-8 CSV data.
public static List<T> Deserialize<T>(ReadOnlyMemory<byte> utf8Csv, CsvTypeInfo<T> typeInfo)Type parameters
T- The type of the records.
Parameters
utf8CsvReadOnlyMemory<byte>- The UTF-8 encoded CSV data.
typeInfoCsvTypeInfo<T>- The metadata of
T.
Returns
List<T> The records.
Exceptions
CsvException- The data is invalid or a value cannot be converted.
Deserialize<T>(string, CsvSerializerOptions?)
CsvSerializer.csReads the records of CSV data.
[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static List<T> Deserialize<T>(string csv, CsvSerializerOptions? options = null)Type parameters
T- The type of the records.
Parameters
csvstring- The CSV data.
optionsCsvSerializerOptions?- The options, or
nullto useCsvSerializerOptions.Default.
Returns
List<T> The records.
Exceptions
CsvException- The data is invalid or a value cannot be converted.
Deserialize<T>(string, CsvTypeInfo<T>)
CsvSerializer.csReads the records of CSV data.
public static List<T> Deserialize<T>(string csv, CsvTypeInfo<T> typeInfo)Type parameters
T- The type of the records.
Parameters
csvstring- The CSV data.
typeInfoCsvTypeInfo<T>- The metadata of
T.
Returns
List<T> The records.
Exceptions
CsvException- The data is invalid or a value cannot be converted.
DeserializeAsync overloads
DeserializeAsync<T>(Stream, CsvSerializerOptions?, CancellationToken)
CsvSerializer.csReads the records of UTF-8 CSV data from a Stream asynchronously.
[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static Task<List<T>> DeserializeAsync<T>(Stream utf8Stream, CsvSerializerOptions? options = null, CancellationToken cancellationToken = default)Type parameters
T- The type of the records.
Parameters
utf8StreamStream- The stream to read the UTF-8 data from. It is not disposed.
optionsCsvSerializerOptions?- The options, or
nullto useCsvSerializerOptions.Default. cancellationTokenCancellationToken- The token to monitor for cancellation requests.
Returns
Task<List<T>> The records.
Exceptions
CsvException- The data is invalid or a value cannot be converted.
DeserializeAsync<T>(Stream, CsvTypeInfo<T>, CancellationToken)
CsvSerializer.csReads the records of UTF-8 CSV data from a Stream asynchronously.
public static Task<List<T>> DeserializeAsync<T>(Stream utf8Stream, CsvTypeInfo<T> typeInfo, CancellationToken cancellationToken = default)Type parameters
T- The type of the records.
Parameters
utf8StreamStream- The stream to read the UTF-8 data from. It is not disposed.
typeInfoCsvTypeInfo<T>- The metadata of
T. cancellationTokenCancellationToken- The token to monitor for cancellation requests.
Returns
Task<List<T>> The records.
Exceptions
CsvException- The data is invalid or a value cannot be converted.
DeserializeAsync<T>(TextReader, CsvSerializerOptions?, CancellationToken)
CsvSerializer.csReads the records of CSV data from a TextReader asynchronously.
[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static Task<List<T>> DeserializeAsync<T>(TextReader reader, CsvSerializerOptions? options = null, CancellationToken cancellationToken = default)Type parameters
T- The type of the records.
Parameters
readerTextReader- The reader to read the data from. It is not disposed.
optionsCsvSerializerOptions?- The options, or
nullto useCsvSerializerOptions.Default. cancellationTokenCancellationToken- The token to monitor for cancellation requests.
Returns
Task<List<T>> The records.
Exceptions
CsvException- The data is invalid or a value cannot be converted.
DeserializeAsync<T>(TextReader, CsvTypeInfo<T>, CancellationToken)
CsvSerializer.csReads the records of CSV data from a TextReader asynchronously.
public static Task<List<T>> DeserializeAsync<T>(TextReader reader, CsvTypeInfo<T> typeInfo, CancellationToken cancellationToken = default)Type parameters
T- The type of the records.
Parameters
readerTextReader- The reader to read the data from. It is not disposed.
typeInfoCsvTypeInfo<T>- The metadata of
T. cancellationTokenCancellationToken- The token to monitor for cancellation requests.
Returns
Task<List<T>> The records.
Exceptions
CsvException- The data is invalid or a value cannot be converted.
DeserializeAsyncEnumerable overloads
DeserializeAsyncEnumerable<T>(Stream, CsvSerializerOptions?, CancellationToken)
CsvSerializer.csReads the records of UTF-8 CSV data from a Stream one by one, asynchronously, as they are enumerated.
[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static IAsyncEnumerable<T> DeserializeAsyncEnumerable<T>(Stream utf8Stream, CsvSerializerOptions? options = null, CancellationToken cancellationToken = default)Type parameters
T- The type of the records.
Parameters
utf8StreamStream- The stream to read the UTF-8 data from. It is not disposed.
optionsCsvSerializerOptions?- The options, or
nullto useCsvSerializerOptions.Default. cancellationTokenCancellationToken- The token to monitor for cancellation requests.
Returns
IAsyncEnumerable<T> The records. The data is read when the sequence is enumerated, and it can be enumerated once.
DeserializeAsyncEnumerable<T>(Stream, CsvTypeInfo<T>, CancellationToken)
CsvSerializer.csReads the records of UTF-8 CSV data from a Stream one by one, asynchronously, as they are enumerated.
public static IAsyncEnumerable<T> DeserializeAsyncEnumerable<T>(Stream utf8Stream, CsvTypeInfo<T> typeInfo, CancellationToken cancellationToken = default)Type parameters
T- The type of the records.
Parameters
utf8StreamStream- The stream to read the UTF-8 data from. It is not disposed.
typeInfoCsvTypeInfo<T>- The metadata of
T. cancellationTokenCancellationToken- The token to monitor for cancellation requests.
Returns
IAsyncEnumerable<T> The records. The data is read when the sequence is enumerated, and it can be enumerated once.
DeserializeAsyncEnumerable<T>(TextReader, CsvSerializerOptions?, CancellationToken)
CsvSerializer.csReads the records of CSV data from a TextReader one by one, asynchronously, as they are enumerated.
[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static IAsyncEnumerable<T> DeserializeAsyncEnumerable<T>(TextReader reader, CsvSerializerOptions? options = null, CancellationToken cancellationToken = default)Type parameters
T- The type of the records.
Parameters
readerTextReader- The reader to read the data from. It is not disposed.
optionsCsvSerializerOptions?- The options, or
nullto useCsvSerializerOptions.Default. cancellationTokenCancellationToken- The token to monitor for cancellation requests.
Returns
IAsyncEnumerable<T> The records. The data is read when the sequence is enumerated, and it can be enumerated once.
DeserializeAsyncEnumerable<T>(TextReader, CsvTypeInfo<T>, CancellationToken)
CsvSerializer.csReads the records of CSV data from a TextReader one by one, asynchronously, as they are enumerated.
public static IAsyncEnumerable<T> DeserializeAsyncEnumerable<T>(TextReader reader, CsvTypeInfo<T> typeInfo, CancellationToken cancellationToken = default)Type parameters
T- The type of the records.
Parameters
readerTextReader- The reader to read the data from. It is not disposed.
typeInfoCsvTypeInfo<T>- The metadata of
T. cancellationTokenCancellationToken- The token to monitor for cancellation requests.
Returns
IAsyncEnumerable<T> The records. The data is read when the sequence is enumerated, and it can be enumerated once.
DeserializeEnumerable overloads
DeserializeEnumerable<T>(Stream, CsvSerializerOptions?)
CsvSerializer.csReads the records of UTF-8 CSV data from a Stream one by one, as they are enumerated.
[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static IEnumerable<T> DeserializeEnumerable<T>(Stream utf8Stream, CsvSerializerOptions? options = null)Type parameters
T- The type of the records.
Parameters
utf8StreamStream- The stream to read the UTF-8 data from. It is not disposed.
optionsCsvSerializerOptions?- The options, or
nullto useCsvSerializerOptions.Default.
Returns
IEnumerable<T> The records. The data is read when the sequence is enumerated, and it can be enumerated once.
DeserializeEnumerable<T>(Stream, CsvTypeInfo<T>)
CsvSerializer.csReads the records of UTF-8 CSV data from a Stream one by one, as they are enumerated.
public static IEnumerable<T> DeserializeEnumerable<T>(Stream utf8Stream, CsvTypeInfo<T> typeInfo)Type parameters
T- The type of the records.
Parameters
utf8StreamStream- The stream to read the UTF-8 data from. It is not disposed.
typeInfoCsvTypeInfo<T>- The metadata of
T.
Returns
IEnumerable<T> The records. The data is read when the sequence is enumerated, and it can be enumerated once.
DeserializeEnumerable<T>(TextReader, CsvSerializerOptions?)
CsvSerializer.csReads the records of CSV data from a TextReader one by one, as they are enumerated.
[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static IEnumerable<T> DeserializeEnumerable<T>(TextReader reader, CsvSerializerOptions? options = null)Type parameters
T- The type of the records.
Parameters
readerTextReader- The reader to read the data from. It is not disposed.
optionsCsvSerializerOptions?- The options, or
nullto useCsvSerializerOptions.Default.
Returns
IEnumerable<T> The records. The data is read when the sequence is enumerated, and it can be enumerated once.
DeserializeEnumerable<T>(TextReader, CsvTypeInfo<T>)
CsvSerializer.csReads the records of CSV data from a TextReader one by one, as they are enumerated.
public static IEnumerable<T> DeserializeEnumerable<T>(TextReader reader, CsvTypeInfo<T> typeInfo)Type parameters
T- The type of the records.
Parameters
readerTextReader- The reader to read the data from. It is not disposed.
typeInfoCsvTypeInfo<T>- The metadata of
T.
Returns
IEnumerable<T> The records. The data is read when the sequence is enumerated, and it can be enumerated once.
Serialize overloads
Serialize<T>(IBufferWriter<byte>, IEnumerable<T>, CsvSerializerOptions?)
CsvSerializer.csWrites records as UTF-8 CSV data to an IBufferWriter<T>.
[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static void Serialize<T>(IBufferWriter<byte> utf8BufferWriter, IEnumerable<T> values, CsvSerializerOptions? options = null)Type parameters
T- The type of the records.
Parameters
utf8BufferWriterIBufferWriter<byte>- The buffer writer to write the UTF-8 data to.
valuesIEnumerable<T>- The records.
optionsCsvSerializerOptions?- The options, or
nullto useCsvSerializerOptions.Default.
Serialize<T>(IBufferWriter<byte>, IEnumerable<T>, CsvTypeInfo<T>)
CsvSerializer.csWrites records as UTF-8 CSV data to an IBufferWriter<T>.
public static void Serialize<T>(IBufferWriter<byte> utf8BufferWriter, IEnumerable<T> values, CsvTypeInfo<T> typeInfo)Type parameters
T- The type of the records.
Parameters
utf8BufferWriterIBufferWriter<byte>- The buffer writer to write the UTF-8 data to.
valuesIEnumerable<T>- The records.
typeInfoCsvTypeInfo<T>- The metadata of
T.
Serialize<T>(IEnumerable<T>, CsvSerializerOptions?)
CsvSerializer.csWrites records as CSV text.
[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static string Serialize<T>(IEnumerable<T> values, CsvSerializerOptions? options = null)Type parameters
T- The type of the records.
Parameters
valuesIEnumerable<T>- The records.
optionsCsvSerializerOptions?- The options, or
nullto useCsvSerializerOptions.Default.
Returns
string The CSV text, with a header row when CsvSerializerOptions.HasHeaderRow is true.
Serialize<T>(IEnumerable<T>, CsvTypeInfo<T>)
CsvSerializer.csWrites records as CSV text.
public static string Serialize<T>(IEnumerable<T> values, CsvTypeInfo<T> typeInfo)Type parameters
T- The type of the records.
Parameters
valuesIEnumerable<T>- The records.
typeInfoCsvTypeInfo<T>- The metadata of
T.
Returns
string The CSV text, with a header row when CsvSerializerOptions.HasHeaderRow is true.
Serialize<T>(Stream, IEnumerable<T>, CsvSerializerOptions?)
CsvSerializer.csWrites records as UTF-8 CSV data to a Stream.
[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static void Serialize<T>(Stream utf8Stream, IEnumerable<T> values, CsvSerializerOptions? options = null)Type parameters
T- The type of the records.
Parameters
utf8StreamStream- The stream to write the UTF-8 data to. It is flushed, not disposed.
valuesIEnumerable<T>- The records.
optionsCsvSerializerOptions?- The options, or
nullto useCsvSerializerOptions.Default.
Serialize<T>(Stream, IEnumerable<T>, CsvTypeInfo<T>)
CsvSerializer.csWrites records as UTF-8 CSV data to a Stream.
public static void Serialize<T>(Stream utf8Stream, IEnumerable<T> values, CsvTypeInfo<T> typeInfo)Type parameters
T- The type of the records.
Parameters
utf8StreamStream- The stream to write the UTF-8 data to. It is flushed, not disposed.
valuesIEnumerable<T>- The records.
typeInfoCsvTypeInfo<T>- The metadata of
T.
Serialize<T>(TextWriter, IEnumerable<T>, CsvSerializerOptions?)
CsvSerializer.csWrites records as CSV text to a TextWriter.
[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static void Serialize<T>(TextWriter writer, IEnumerable<T> values, CsvSerializerOptions? options = null)Type parameters
T- The type of the records.
Parameters
writerTextWriter- The writer to write the data to. It is flushed, not disposed.
valuesIEnumerable<T>- The records.
optionsCsvSerializerOptions?- The options, or
nullto useCsvSerializerOptions.Default.
Serialize<T>(TextWriter, IEnumerable<T>, CsvTypeInfo<T>)
CsvSerializer.csWrites records as CSV text to a TextWriter.
public static void Serialize<T>(TextWriter writer, IEnumerable<T> values, CsvTypeInfo<T> typeInfo)Type parameters
T- The type of the records.
Parameters
writerTextWriter- The writer to write the data to. It is flushed, not disposed.
valuesIEnumerable<T>- The records.
typeInfoCsvTypeInfo<T>- The metadata of
T.
SerializeAsync overloads
SerializeAsync<T>(Stream, IAsyncEnumerable<T>, CsvSerializerOptions?, CancellationToken)
CsvSerializer.csWrites records as UTF-8 CSV data to a Stream asynchronously, as they are produced.
[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static Task SerializeAsync<T>(Stream utf8Stream, IAsyncEnumerable<T> values, CsvSerializerOptions? options = null, CancellationToken cancellationToken = default)Type parameters
T- The type of the records.
Parameters
utf8StreamStream- The stream to write the UTF-8 data to. It is flushed, not disposed.
valuesIAsyncEnumerable<T>- The records.
optionsCsvSerializerOptions?- The options, or
nullto useCsvSerializerOptions.Default. cancellationTokenCancellationToken- The token to monitor for cancellation requests.
Returns
Task A task that represents the asynchronous operation.
SerializeAsync<T>(Stream, IAsyncEnumerable<T>, CsvTypeInfo<T>, CancellationToken)
CsvSerializer.csWrites records as UTF-8 CSV data to a Stream asynchronously, as they are produced.
public static Task SerializeAsync<T>(Stream utf8Stream, IAsyncEnumerable<T> values, CsvTypeInfo<T> typeInfo, CancellationToken cancellationToken = default)Type parameters
T- The type of the records.
Parameters
utf8StreamStream- The stream to write the UTF-8 data to. It is flushed, not disposed.
valuesIAsyncEnumerable<T>- The records.
typeInfoCsvTypeInfo<T>- The metadata of
T. cancellationTokenCancellationToken- The token to monitor for cancellation requests.
Returns
Task A task that represents the asynchronous operation.
SerializeAsync<T>(Stream, IEnumerable<T>, CsvSerializerOptions?, CancellationToken)
CsvSerializer.csWrites records as UTF-8 CSV data to a Stream asynchronously.
[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static Task SerializeAsync<T>(Stream utf8Stream, IEnumerable<T> values, CsvSerializerOptions? options = null, CancellationToken cancellationToken = default)Type parameters
T- The type of the records.
Parameters
utf8StreamStream- The stream to write the UTF-8 data to. It is flushed, not disposed.
valuesIEnumerable<T>- The records.
optionsCsvSerializerOptions?- The options, or
nullto useCsvSerializerOptions.Default. cancellationTokenCancellationToken- The token to monitor for cancellation requests.
Returns
Task A task that represents the asynchronous operation.
SerializeAsync<T>(Stream, IEnumerable<T>, CsvTypeInfo<T>, CancellationToken)
CsvSerializer.csWrites records as UTF-8 CSV data to a Stream asynchronously.
public static Task SerializeAsync<T>(Stream utf8Stream, IEnumerable<T> values, CsvTypeInfo<T> typeInfo, CancellationToken cancellationToken = default)Type parameters
T- The type of the records.
Parameters
utf8StreamStream- The stream to write the UTF-8 data to. It is flushed, not disposed.
valuesIEnumerable<T>- The records.
typeInfoCsvTypeInfo<T>- The metadata of
T. cancellationTokenCancellationToken- The token to monitor for cancellation requests.
Returns
Task A task that represents the asynchronous operation.
SerializeAsync<T>(TextWriter, IAsyncEnumerable<T>, CsvSerializerOptions?, CancellationToken)
CsvSerializer.csWrites records as CSV text to a TextWriter asynchronously, as they are produced.
[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static Task SerializeAsync<T>(TextWriter writer, IAsyncEnumerable<T> values, CsvSerializerOptions? options = null, CancellationToken cancellationToken = default)Type parameters
T- The type of the records.
Parameters
writerTextWriter- The writer to write the data to. It is flushed, not disposed.
valuesIAsyncEnumerable<T>- The records.
optionsCsvSerializerOptions?- The options, or
nullto useCsvSerializerOptions.Default. cancellationTokenCancellationToken- The token to monitor for cancellation requests.
Returns
Task A task that represents the asynchronous operation.
SerializeAsync<T>(TextWriter, IAsyncEnumerable<T>, CsvTypeInfo<T>, CancellationToken)
CsvSerializer.csWrites records as CSV text to a TextWriter asynchronously, as they are produced.
public static Task SerializeAsync<T>(TextWriter writer, IAsyncEnumerable<T> values, CsvTypeInfo<T> typeInfo, CancellationToken cancellationToken = default)Type parameters
T- The type of the records.
Parameters
writerTextWriter- The writer to write the data to. It is flushed, not disposed.
valuesIAsyncEnumerable<T>- The records.
typeInfoCsvTypeInfo<T>- The metadata of
T. cancellationTokenCancellationToken- The token to monitor for cancellation requests.
Returns
Task A task that represents the asynchronous operation.
SerializeAsync<T>(TextWriter, IEnumerable<T>, CsvSerializerOptions?, CancellationToken)
CsvSerializer.csWrites records as CSV text to a TextWriter asynchronously.
[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static Task SerializeAsync<T>(TextWriter writer, IEnumerable<T> values, CsvSerializerOptions? options = null, CancellationToken cancellationToken = default)Type parameters
T- The type of the records.
Parameters
writerTextWriter- The writer to write the data to. It is flushed, not disposed.
valuesIEnumerable<T>- The records.
optionsCsvSerializerOptions?- The options, or
nullto useCsvSerializerOptions.Default. cancellationTokenCancellationToken- The token to monitor for cancellation requests.
Returns
Task A task that represents the asynchronous operation.
SerializeAsync<T>(TextWriter, IEnumerable<T>, CsvTypeInfo<T>, CancellationToken)
CsvSerializer.csWrites records as CSV text to a TextWriter asynchronously.
public static Task SerializeAsync<T>(TextWriter writer, IEnumerable<T> values, CsvTypeInfo<T> typeInfo, CancellationToken cancellationToken = default)Type parameters
T- The type of the records.
Parameters
writerTextWriter- The writer to write the data to. It is flushed, not disposed.
valuesIEnumerable<T>- The records.
typeInfoCsvTypeInfo<T>- The metadata of
T. cancellationTokenCancellationToken- The token to monitor for cancellation requests.
Returns
Task A task that represents the asynchronous operation.
SerializeToUtf8Bytes overloads
SerializeToUtf8Bytes<T>(IEnumerable<T>, CsvSerializerOptions?)
CsvSerializer.csWrites records as UTF-8 CSV data.
[RequiresDynamicCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
[RequiresUnreferencedCode("Reflection-based CSV serialization is not compatible with trimming and Native AOT. Use a generated CsvSerializerContext and the overloads that take a CsvTypeInfo.")]
public static byte[] SerializeToUtf8Bytes<T>(IEnumerable<T> values, CsvSerializerOptions? options = null)Type parameters
T- The type of the records.
Parameters
valuesIEnumerable<T>- The records.
optionsCsvSerializerOptions?- The options, or
nullto useCsvSerializerOptions.Default.
Returns
byte[] The UTF-8 CSV data, without a byte order mark, with a header row when CsvSerializerOptions.HasHeaderRow is true.
Example
var people = new List<Person>
{
new() { Name = "Alice", Age = 30 },
new() { Name = "Bob", Age = 25 },
};
// The values are written as UTF-8 bytes, without creating a string first
byte[] utf8Csv = CsvSerializer.SerializeToUtf8Bytes(people);
Assert.Equal("Name,Age,BirthDate\r\nAlice,30,\r\nBob,25,\r\n"u8.ToArray(), utf8Csv);
var result = CsvSerializer.Deserialize<Person>(utf8Csv);
Assert.Equal("Bob", result[1].Name);SerializeToUtf8Bytes<T>(IEnumerable<T>, CsvTypeInfo<T>)
CsvSerializer.csWrites records as UTF-8 CSV data.
public static byte[] SerializeToUtf8Bytes<T>(IEnumerable<T> values, CsvTypeInfo<T> typeInfo)Type parameters
T- The type of the records.
Parameters
valuesIEnumerable<T>- The records.
typeInfoCsvTypeInfo<T>- The metadata of
T.
Returns
byte[] The UTF-8 CSV data, without a byte order mark, with a header row when CsvSerializerOptions.HasHeaderRow is true.