matthewp / String-Serializer-Sharp

Serialize/deserialize strings to objects.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

For those who have had the pain of working with flat files, whether delimited or fixed-width, this should make things a bit easier to convert between a string and an object.  Simply provided the position, length, name, and type of your fixed width fields, and the FixedWidthString object can serialize and deserialize for you.

* Type is optional, when the type is not a string.
* Type can be the System.Type or a string representation (as in the example).  This makes it possible to store the schema in a database or xml template.

string input = @"Some Guy                               4550000.00 ";
FixedWidthString fws = new FixedWidthString();
fws.Fields = new FixedWidthField[3];
fws.Fields[0] = new FixedWidthField(0, 30, "Name");
fws.Fields[1] = new FixedWidthField(39, 2, "Age", typeof(int));
fws.Fields[2] = new FixedWidthField(41, 9, "Income", "System.Decimal");

Person person = fws.Deserialize<Person>(input);
Console.WriteLine(String.Format("Name: {0}, Age: {1}, Income {2}", person.Name, person.Age, person.Income));
Console.WriteLine(String.Format("|{0}| <-- Original", input));
Console.WriteLine(String.Format("|{0}| <-- Generated", fws.Serialize(person)));
Console.ReadKey();

About

Serialize/deserialize strings to objects.


Languages

Language:C# 100.0%