AWise.SepCsvSourceGenerator 0.1.0-preview006

This is a prerelease version of AWise.SepCsvSourceGenerator.
There is a newer version of this package available.
See the version list below for details.
dotnet add package AWise.SepCsvSourceGenerator --version 0.1.0-preview006
                    
NuGet\Install-Package AWise.SepCsvSourceGenerator -Version 0.1.0-preview006
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AWise.SepCsvSourceGenerator" Version="0.1.0-preview006">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AWise.SepCsvSourceGenerator" Version="0.1.0-preview006" />
                    
Directory.Packages.props
<PackageReference Include="AWise.SepCsvSourceGenerator">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AWise.SepCsvSourceGenerator --version 0.1.0-preview006
                    
#r "nuget: AWise.SepCsvSourceGenerator, 0.1.0-preview006"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package AWise.SepCsvSourceGenerator@0.1.0-preview006
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=AWise.SepCsvSourceGenerator&version=0.1.0-preview006&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=AWise.SepCsvSourceGenerator&version=0.1.0-preview006&prerelease
                    
Install as a Cake Tool

A source generator for Sep CSV parsing

This is a C# source generator for generating strongly typed parsing of CSV files using the excellent Sep CSV library.

Usage

First, add a reference to the Nuget packages:

dotnet add package Sep
dotnet add package AWise.SepCsvSourceGenerator

To use the source generator, define a partial class and a partial static method with the [GenerateCsvParser] attribute. The source generator will generate the implementation of this method.

Here is an example:

using nietras.SeparatedValues;
using AWise.SepCsvSourceGenerator;

public partial class MyRecord
{
    [CsvHeaderName("Name")]
    public required string Name { get; set; }

    [CsvHeaderName("Date")]
    [CsvDateFormat("yyyy-MM-dd")]
    public DateTime Date { get; set; }

    [GenerateCsvParser]
    public static partial IEnumerable<MyRecord> Parse(SepReader reader, CancellationToken ct);
}

You can then use the generated Parse method to parse a CSV file:

const string CSV_CONTENT = "Name,Date\nJohn,2023-01-15\nJane,2023-02-20";

using var reader = Sep.Reader().FromText(CSV_CONTENT);
var records = MyRecord.Parse(reader, CancellationToken.None);

foreach (var record in records)
{
    Console.WriteLine($"Name: {record.Name}, Date: {record.Date.ToShortDateString()}");
}

If a property is is init or required, an exception will be thrown if the CSV file does not have the column. If the property has set, the generated parser will only set the property if the column exists in the CSV file.

The only types that are supported for parsing are types that implement ISpanParsable.

DateTime is given special treatment: it is parsed with DateTime.ParseExact using CultureInfo.InvariantCulture. Specify the date-time format using the CsvDateFormat attribute.

Supported attributes

The following attributes are supported on the properties of the partial class:

  • [CsvHeaderName("...")]: Specifies the name of the column in the CSV file.
  • [CsvDateFormat("...")]: Specifies the date format for DateTime properties.

Async parsing

The source generator also supports generating asynchronous parsing methods. To do this, define a method that returns an IAsyncEnumerable<T>:

[GenerateCsvParser]
public static partial IAsyncEnumerable<MyRecord> ParseAsync(SepReader reader, CancellationToken ct);

TODO

  • Consider not requiring the CsvHeaderName attribute on every property. We could take inspiration from how System.Text.Json does it.
  • Support calling implementations of ISpanParsable that are explicitly implemented.
  • Add support for other date and time types like DateTimeOffset, DateOnly, and TimeOnly.
  • Consider supporting something other than DateTime.ParseExact for date parsing. It is a little annoying to specify the format every time when we could potentially let DateTime.Parse "figure it out".
  • Make the cancellation token optional. Note that you can declare a default value for the cancellation token already, which makes it optional for callers.
  • Consider allowing the parameters to the method be named something other than reader and ct.
  • Consider adding support for list-like types for properties (like arrays or List<T>).
  • Figure out what to do with the AnalyzerReleases.Shipped.md and AnalyzerReleases.Unshipped.md files.
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.3.1-preview003 160 11/1/2025
0.3.0 199 8/10/2025
0.2.1 377 7/20/2025
0.2.0 360 7/20/2025
0.1.0 124 7/19/2025
0.1.0-preview006 125 7/19/2025
0.1.0-preview005 209 7/13/2025
0.1.0-preview003 161 7/13/2025
0.1.0-preview001 158 7/13/2025