Skip to content

.NET library for deserializing and serializing fixed width files

License

Notifications You must be signed in to change notification settings

mscribellito/FixedWidth

Repository files navigation

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

  • Serialize an object into a string and deserialize a string into an object
  • Supports most built-in types
    • bool (using BooleanFormatter)
    • char
    • decimal
    • double
    • float
    • int/uint
    • long/ulong
    • short/ushort
    • string
  • Supports custom serialization and deserialization via ITextFormatter
  • Specify field padding and text alignment
  • Handles zero and one based indexes

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"