Modern CSV (De)Serializer
This code hasn't seen much production use, please use with caution and report any issues you encounter.
Consult The Wiki™ for documentation, and Cesil's Github Pages for references.
You may be interested in:
- Install the latest Cesil off of Nuget.
- If you want to avoid runtime code generation, you will need to add Cesil.SourceGenerator as well. See Source Generators for more details.
- Add
using Cesil;
to your C# file - Create a configuration (with default Options) with either
Configuration.For<TYourType>()
orConfiguration.ForDynamic()
, as appropriate for your use case - Create a reader or writer using one of the
CreateReader
,CreateAsyncReader
,CreateWriter
, orCreateAsyncWriter
methods on the configuration.- Each of these methods has a number of overloads, supporting using
TextReaders
,Pipes
, and so on.
- Each of these methods has a number of overloads, supporting using
- Use the methods on the
IReader<TRow>
,IAsyncReader<TRow>
,IWriter<TRow>
, orIAsyncWriter<TRow>
interface to read or write your data.
Using a convient method:
using Cesil;
// ...
IEnumerable<MyType> rows = CesilUtils.Enumerate<MyType>(/*Some TextReader*/);
In a more explicit, and configurable, way using explicit configuration and options.
using Cesil;
// ...
Options myOptions = /* ... */
IBoundConfiguration<MyType> myConfig = Configuration.For<MyType>(myOptions);
using(TextReader reader = /* ... */)
using(IReader<MyType> csv = myConfig.CreateReader(reader))
{
IEnumerable<MyType> rows = csv.EnumerateAll();
}
For more detail, see Reading.
Using a convient method:
using Cesil;
// ...
IAsyncEnumerable<MyType> rows = CesilUtils.EnumerateAsync<MyType>(/*Some TextReader*/);
In a more explicit, and configurable, way using explicit configuration and options.
using Cesil;
// ...
Options myOptions = /* ... */
IBoundConfiguration<MyType> myConfig = Configuration.For<MyType>(myOptions);
using(TextReader reader = /* ... */)
await using(IAsyncReader<MyType> csv = myConfig.CreateAsyncReader(reader))
{
IAsyncReader<MyType> rows = csv.EnumerateAllAsync();
}
For more detail, see Reading.
Using a convient method:
using Cesil;
// ...
IEnumerable<MyType> myRows = /* ... */
using(TextWriter writer = /* .. */)
{
CesilUtilities.Write(myRows, writer);
}
In a more explicit, and configurable, way using explicit configuration and options.
using Cesil;
// ...
IEnumerable<MyType> myRows = /* ... */
Options myOptions = /* ... */
IBoundConfiguration<MyType> myConfig = Configuration.For<MyType>(myOptions);
using(TextWriter writer = /* ... */)
using(IWriter<MyType> csv = myConfig.CreateWriter(writer))
{
csv.WriteAll(myRows);
}
For more detail, see Writing.
Using a convient method:
using Cesil;
// ...
// IAsyncEnumerable<MyType> will also work
IEnumerable<MyType> myRows = /* ... */
using(TextWriter writer = /* .. */)
{
await CesilUtilities.WriteAsync(myRows, writer);
}
In a more explicit, and configurable, way using explicit configuration and options.
using Cesil;
// ...
// IAsyncEnumerable<MyType> will also work
IEnumerable<MyType> myRows = /* ... */
Options myOptions = /* ... */
IBoundConfiguration<MyType> myConfig = Configuration.For<MyType>(myOptions);
using(TextWriter writer = /* ... */)
await using(IWriter<MyType> csv = myConfig.CreateAsyncWriter(writer))
{
await csv.WriteAllAsync(myRows);
}
For more detail, see Writing.