Code Monkey home page Code Monkey logo

fixedwidth's Introduction

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"

fixedwidth's People

Contributors

mscribellito avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

fixedwidth's Issues

Deserializing int with Padding zeros on only zeros string

Hello again,

When using the padding with zeros option (Padding = '0') to parse "00000" string it raises and exception of wrong string format. I believe this is due to parsing an empty string (all zeros were removed) into integer.

Feature Request: ITextFormatter pass TextFieldAttribute

Hello,
can you pass the TextFieldAttribute when calling Serialize or Deserialize on a custom formatter (ITextFormatter)?
Use-Case might be: custom formatter must know the lenght / decimal places of the TextField-Attribute.

Thanks for your feedback in advance.

Deserializing char with ' '

Hello,
When using a char field with [TextField(__, 1)] attribute the parser considers spaces ' ' as empty '' and raises a format exception.
I'm currently using a custom formatter to avoid this.

Additionally, I liked the exception used on Custom Formatters and missed the exception when testing without custom, it would help troubleshot this kind of formatting errors because it points to the exact field that raised the exception.

Allow formatters to have parameters

It would be very useful if formatters could have parameters so that, for example, an alternative BooleanFormatter could be created which could have the expected true and false values passed in as parameters, rather than only working for true = 1.

Currently this isn't possible, as the Formatter on TextField is internal.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.