mscribellito / FixedWidth

.NET library for deserializing and serializing fixed width files
MIT License
5 stars 4 forks source link
deserialize fixedwidth object serialization serialize string text

FixedWidth

Build status Software License

Released under a MIT license - https://opensource.org/licenses/MIT

NuGet Package

FixedWidth is an easy to use .NET library for working with fixed width (flat formatted) text files. By applying attributes to your code, you can setup the position and format for your data when deserializing/serializing to and from fixed width files.

Documentation

Available on Wiki.

Features

Example

Apply attributes to class members

[TextSerializable]
public class Dog
{

    [TextField(1, 10)]
    public string Name { get; set; }

    [TextField(11, 1)]
    public char Sex { get; set; }

    [TextField(12, 3,
        Padding = '0',
        Alignment = TextAlignment.Right)]
    public int Weight { get; set; }

    [TextField(15, 8,
        FormatterType = typeof(DateFormatter))]
    public DateTime BirthDate { get; set; }

    [TextField(23, 1,
        FormatterType = typeof(BooleanFormatter))]
    public bool SpayedNeutered { get; set; }

}

Create TextSerializer instance

var serializer = new TextSerializer<Dog>();

Deserialize string into object

var deserialized = serializer.Deserialize("Wally     M065201011161");
BirthDate [DateTime]:{11/16/2010 12:00:00 AM}
Name [string]:"Wally"
Sex [char]:77 'M'
SpayedNeutered [bool]:true
Weight [int]:65

Serialize object into string

var serialized = serializer.Serialize(deserialized);
"Wally     M065201011161"