CsvConverter<T> Class

Converts the values of a column of type T to or from text.

public abstract class CsvConverter<T> : CsvConverter

Type parameters

T
The type of the values.

Remarks

Override the UTF-16 methods. The UTF-8 methods, used to read and write UTF-8 data, decode the value and call the UTF-16 methods by default: override them too to avoid the conversion.

When CsvConverter<T>.HandleNull is false and T can be null, an empty value is read as null and a null value is written as an empty value, without calling the converter.

Inheritance
CsvConverter CsvConverter<T>

Constructors

NameDescription
CsvConverter()Initializes a new instance of the CsvConverter<T> class.

CsvConverter()

CsvConverter`1.cs

Initializes a new instance of the CsvConverter<T> class.

protected CsvConverter()

Properties

NameDescription
HandleNullGets a value indicating whether the converter reads empty values and writes null values itself. Default is false.
TypeToConvertGets the type the converter converts, or null for a CsvConverterFactory.

Gets a value indicating whether the converter reads empty values and writes null values itself. Default is false.

public virtual bool HandleNull { get; }

Property value

bool

TypeToConvert

CsvConverter`1.cs

Gets the type the converter converts, or null for a CsvConverterFactory.

public sealed override Type TypeToConvert { get; }

Property value

Type

Methods

NameDescription
CanConvert(Type)Determines whether the converter converts values of the specified type.
Read(ReadOnlySpan<byte>, CsvConverterContext)Converts a UTF-8 value read from the data.
Read(ReadOnlySpan<char>, CsvConverterContext)Converts a value read from the data.
TryFormat(T, Span<byte>, int, CsvConverterContext)Formats a value as UTF-8 to write in the data.
TryFormat(T, Span<char>, int, CsvConverterContext)Formats a value to write in the data.

CanConvert(Type)

CsvConverter`1.cs

Determines whether the converter converts values of the specified type.

public override bool CanConvert(Type typeToConvert)

Parameters

typeToConvert Type
The type of the values.

Returns

bool true when the converter converts values of typeToConvert; otherwise, false.

Read overloads

Read(ReadOnlySpan<byte>, CsvConverterContext)

CsvConverter`1.cs

Converts a UTF-8 value read from the data.

public virtual T Read(ReadOnlySpan<byte> utf8Value, CsvConverterContext context)
Parameters
utf8Value ReadOnlySpan<byte>
The UTF-8 bytes of the value, without its quotes.
context CsvConverterContext
The context of the conversion.
Returns

T The converted value.

Remarks

The default implementation decodes the value and calls CsvConverter<T>.Read.

Read(ReadOnlySpan<char>, CsvConverterContext)

Converts a value read from the data.

public abstract T Read(ReadOnlySpan<char> value, CsvConverterContext context)
Parameters
value ReadOnlySpan<char>
The value, without its quotes.
context CsvConverterContext
The context of the conversion.
Returns

T The converted value.

Exceptions
FormatException
The value is not valid. The serializer reports it as a CsvException with the position of the value.

TryFormat overloads

TryFormat(T, Span<byte>, int, CsvConverterContext)

CsvConverter`1.cs

Formats a value as UTF-8 to write in the data.

public virtual bool TryFormat(T value, Span<byte> utf8Destination, out int bytesWritten, CsvConverterContext context)
Parameters
value T
The value.
utf8Destination Span<byte>
The span to write the UTF-8 bytes to. The writer quotes and escapes the text when needed.
bytesWritten int
The number of bytes written to utf8Destination.
context CsvConverterContext
The context of the conversion.
Returns

bool true when the text fits in utf8Destination; otherwise, false, and the writer calls the method again with a larger span.

Remarks

The default implementation calls CsvConverter<T>.TryFormat and encodes the text.

TryFormat(T, Span<char>, int, CsvConverterContext)

Formats a value to write in the data.

public abstract bool TryFormat(T value, Span<char> destination, out int charsWritten, CsvConverterContext context)
Parameters
value T
The value.
destination Span<char>
The span to write the text to. The writer quotes and escapes the text when needed.
charsWritten int
The number of characters written to destination.
context CsvConverterContext
The context of the conversion.
Returns

bool true when the text fits in destination; otherwise, false, and the writer calls the method again with a larger span.