CsvConverter<T> Class
Converts the values of a column of type T to or from text.
public abstract class CsvConverter<T> : CsvConverterType 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
CsvConverterCsvConverter<T>
Constructors
| Name | Description |
|---|---|
| CsvConverter() | Initializes a new instance of the CsvConverter<T> class. |
CsvConverter()
CsvConverter`1.csInitializes a new instance of the CsvConverter<T> class.
protected CsvConverter()Properties
| Name | Description |
|---|---|
| HandleNull | Gets a value indicating whether the converter reads empty values and writes null values itself. Default is false. |
| TypeToConvert | Gets the type the converter converts, or null for a CsvConverterFactory. |
HandleNull
CsvConverter`1.csTypeToConvert
CsvConverter`1.csGets the type the converter converts, or null for a CsvConverterFactory.
public sealed override Type TypeToConvert { get; }Property value
Methods
| Name | Description |
|---|---|
| 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.csRead overloads
Read(ReadOnlySpan<byte>, CsvConverterContext)
CsvConverter`1.csConverts a UTF-8 value read from the data.
public virtual T Read(ReadOnlySpan<byte> utf8Value, CsvConverterContext context)Parameters
utf8ValueReadOnlySpan<byte>- The UTF-8 bytes of the value, without its quotes.
contextCsvConverterContext- 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
valueReadOnlySpan<char>- The value, without its quotes.
contextCsvConverterContext- The context of the conversion.
Returns
T The converted value.
Exceptions
FormatException- The value is not valid. The serializer reports it as a
CsvExceptionwith the position of the value.
TryFormat overloads
TryFormat(T, Span<byte>, int, CsvConverterContext)
CsvConverter`1.csFormats 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
valueT- The value.
utf8DestinationSpan<byte>- The span to write the UTF-8 bytes to. The writer quotes and escapes the text when needed.
bytesWrittenint- The number of bytes written to
utf8Destination. contextCsvConverterContext- 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
valueT- The value.
destinationSpan<char>- The span to write the text to. The writer quotes and escapes the text when needed.
charsWrittenint- The number of characters written to
destination. contextCsvConverterContext- 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.