Skip to content

.NET Library for Serializing CSV and Fixed Width Files into a Class Model (similar to XmlSerializer)

License

Notifications You must be signed in to change notification settings

NickSchweitzer/TextSerializer

Repository files navigation

Text Serializer Library

AppVeyor AppVeyor tests Nuget Nuget GitHub Read the Docs Codecov

Library for reading and writing CSV and Fixed Width files into a class or struct similar to XmlSerializer.

Table of Contents

Attribute Based Configuration

[TextSerializable]
public class MyCsvRecord
{
    [TextField(0)]
    public int Id { get; set; }

    [TextField(1)]
    public string Name;     // Properties and Fields can be Serialized

    [TextField(2)]
    public string Organization { get; set; }
}

[TextSerializable]
public class MyFixedWidthRecord
{
    [FixedWidthField(0, 5, Padding = '0')]
    public int Id { get; set; }

    [FixedWidthField(1, 20)]
    public string Name { get; set; }

    [FixedWidthField(2, 20)]
    public string Organization { get; set; }
}

// ...

var csvSer = new CsvSerializer<MyCsvRecord>();
IEnumerable<MyCsvRecord> sampleCsv = null;
using (StreamReader reader = new File.OpenText("sample.csv"))
{
    sampleCsv = csvSer.DeserializeArray(reader);
    reader.Close();
}

var fixedSer = new FixedWidthSerializer<MyFixedWidthRecord>();
IEnumerable<MyCsvRecord> sampleFixedWidth = null;
using (StreamReader reader = new File.OpenText("fixedwidth.txt"))
{
    sampleFixedWidth = fixedSer.DeserializeArray(reader);
    reader.Close();
}

Fluent Configuration

The fluent configuration model is new for version 2.0. It allows a POCO to be serialized, or some other class, without additional attributing just for Serialization sake. All of the same "common sense" defaults from Attribute based configuration are used. Fluent configuration should feel very similar to AutoMapper.

public class CsvPocoRecord
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description;      // Properties and Fields are supported
    public double Value { get; set; }
    public bool Enabled { get; set; }
}

// ...
var serializer = new CsvSerializer<CsvPocoRecord>(config => config
    .ForMember(field => field.Id, opt => opt.Position(0))
    .ForMember(field => field.Name, opt => opt.Position(1))
    .ForMember(field => field.Description, opt => opt.Position(2))
    .ForMember(field => field.Value, opt => opt.Position(3))
    .ForMember(field => field.Enabled, opt => opt.Optional().Position(4)));

IEnumerable<CsvPocoRecord> sampleCsv = null;
using (StreamReader reader = new File.OpenText("sample.csv"))
{
    sampleCsv = csvSer.DeserializeArray(reader);
    reader.Close();
}

If your POCO class contains either all Fields or all Properties, then you can use the ByConvention method for CSV files, which defaults all properties to common sense defaults, and takes the fields or properties in the order listed. Because of limitiations of .NET Reflection, this does not work if you have a mix of fields and properties and an exception will be thrown. After calling ByConvention, you can still override any of the defaults on a property by property basis if you'd like, without having to specify all the values for all the rest of the properties.

public class CsvPocoRecord
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public double Value { get; set; }
    public bool Enabled { get; set; }
}

// ...
var serializer = new CsvSerializer<CsvPocoRecord>(config => config.ByConvention());

About

.NET Library for Serializing CSV and Fixed Width Files into a Class Model (similar to XmlSerializer)

Topics

Resources

License

Stars

Watchers

Forks