Skip to content

Miista/TinyCsvParser.Optional

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

License: MIT NuGet version

TinyCsvParser.Optional

Adding support for parsing options.

Supports .NET Core (.NET Standard 2+)

Installation

PM> Install-Package TinyCsvParser.Optional

Usage

The only thing you need to keep in mind when using this extension is that your mapping class must have a constructor taking in an instance of ITypeConverterProvider and passing it on to its base constructor. See example below.

// Entity
private class Person
{
    public string FirstName { get; set; }
    
    public string LastName { get; set; }
    
    public Option<DateTime> BirthDate { get; set; }
}

// Mapping
private class CsvPersonMapping : CsvMapping<Person>
{
    // Need to take in ITypeConverterProvider
    public CsvPersonMapping(ITypeConverterProvider typeConverterProvider) : base(typeConverterProvider)
    {
        MapProperty(0, x => x.FirstName);
        MapProperty(1, x => x.LastName);
        MapProperty(2, x => x.BirthDate);
    }
}

// Parsing
var options = new CsvParserOptions(skipHeader: false, fieldsSeparator: ',');
var typeConverterProvider = new TypeConverterProvider().AddOptional(); // <-- This line
var parser = new CsvParser<Person>(options, new CsvPersonMapping(typeConverterProvider));
var result = parser.ReadFromString(readerOptions, $"Philipp,Wagner,null").ToList();

Console.WriteLine(result[0].Result.FirstName); // Writes Philipp
Console.WriteLine(result[0].Result.LastName); // Writes Wagner
Console.WriteLine(result[0].Result.BirthDate); // Writes None

License

This project is licensed under the MIT license. See the LICENSE file for more info.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages