Tip
The optimal parameters can be determined automatically with
AnalyzeString(String, Header, Int32) - or use
ParseAnalyzedTResult(String, CsvMapping, FuncObject, TResult, Header, Int32).
public static TResult[] Parse<TResult>(
string csv,
CsvMapping mapping,
Func<Object, TResult> conversion,
char delimiter = ',',
bool isHeaderPresent = true,
CsvOpts options = CsvOpts.Default
)
Public Shared Function Parse(Of TResult) (
csv As String,
mapping As CsvMapping,
conversion As Func(Of Object, TResult),
Optional delimiter As Char = ","C,
Optional isHeaderPresent As Boolean = true,
Optional options As CsvOpts = CsvOpts.Default
) As TResult()
public:
generic<typename TResult>
static array<TResult>^ Parse(
String^ csv,
CsvMapping^ mapping,
Func<Object^, TResult>^ conversion,
wchar_t delimiter = L',',
bool isHeaderPresent = true,
CsvOpts options = CsvOpts::Default
)
static member Parse :
csv : string *
mapping : CsvMapping *
conversion : Func<Object, 'TResult> *
?delimiter : char *
?isHeaderPresent : bool *
?options : CsvOpts
(* Defaults:
let _delimiter = defaultArg delimiter ','
let _isHeaderPresent = defaultArg isHeaderPresent true
let _options = defaultArg options CsvOpts.Default
*)
-> 'TResult[]
A function that converts the content of mapping to an instance of TResult.
The function is called for each row in csv and gets the CsvMapping as argument, filled with the current CsvRecord instance. The CsvMapping is passed to the function as dynamic argument: Inside the function the registered DynamicProperty instances can be used like regular .NET properties, but without IntelliSense ("late binding").
Object serialization with CSV:
using FolkerKinzel.CsvTools.Mappings;
using FolkerKinzel.CsvTools.Mappings.TypeConverters;
namespace Benchmarks;
internal static partial class CalculationReader
{
internal static IList<Calculation> ReadDefault(string csv)
{
var doubleConverter = new DoubleConverter();
CsvMapping mapping = CsvMappingBuilder
.Create()
.AddProperty("First", doubleConverter)
.AddProperty("Operator", new CharConverter())
.AddProperty("Second", doubleConverter)
.AddProperty("Result", doubleConverter)
.Build();
return CsvConverter.Parse(
csv,
mapping,
static mapping => new Calculation(mapping.First,
mapping.Operator,
mapping.Second,
mapping.Result));
}
}
ArgumentNullException | csv, or mapping, or conversion is null. |
CsvFormatException | Invalid CSV. The interpretation depends on options. |
FormatException | Parsing fails and Throwing is true. |