Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Easily map delimited and fixed-width strings to objects.

Notifications You must be signed in to change notification settings

joeeisel/SchemaObjectMapper

Open more actions menu
 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

Easily map delimited and fixed-width strings to objects.

Basic Usage (Delimited Lines)

// Create schema
var personSchema = new DelimitedSchema<Person>();
personSchema.AddMapping(p => p.FirstName, 0);
personSchema.AddMapping(p => p.LastName, 1);
personSchema.AddMapping(p => p.Gender, 2);
personSchema.AddMapping(p => p.DateOfBirth, 3);

// Create mapper
var personMapper = new DelimitedSchemaObjectMapper<Person>(personSchema, "|");

// Map line
var person = personMapper.MapLine("Foo|Bar|M|01/02/2003");

Assert.AreEqual("Foo", person.FirstName);
Assert.AreEqual("Bar", person.LastName);
Assert.AreEqual("M", person.Gender);
Assert.AreEqual(DateTime.Parse("01/02/2003"), person.DateOfBirth);

Basic Usage (Fixed-Width Lines)

// Create schema
var personSchema = new FixedWidthSchema<Person>();
personSchema.AddMapping(p => p.FirstName, 0, 10, trim: true);
personSchema.AddMapping(p => p.LastName, 10, 10, trim: true);
personSchema.AddMapping(p => p.Gender, 24, 1, trim: true);
personSchema.AddMapping(p => p.DateOfBirth, 25, 10, trim: true);

// Create mapper
var mapper = new FixedWidthSchemaObjectMapper<Person>(personSchema);

// Map line
var person = mapper.MapLine("Foo       Bar           M01/02/2003");

Assert.AreEqual("Foo", person.FirstName);
Assert.AreEqual("Bar", person.LastName);
Assert.AreEqual("M", person.Gender);
Assert.AreEqual(DateTime.Parse("01/02/2003"), person.DateOfBirth);

Basic Usage (FileReader)

var schema = new DelimitedSchema<Person>();
schema.AddMapping(s => s.FirstName, 1);
schema.AddMapping(s => s.LastName, 2);
schema.AddMapping(s => s.Gender, 3);
schema.AddMapping(s => s.DateOfBirth, 4);

var mapper = new DelimitedSchemaObjectMapper<Person>(schema, "|");
var persons = new List<Person>();

var fr = new FileReader();
fr.ReadFile("test.txt", line =>
{
	if (line.StartsWith("P"))
	{
		persons.Add(mapper.MapLine(line));
	}
});

License

Licensed under the MIT license.

About

Developed by @pagebrooks.

About

Easily map delimited and fixed-width strings to objects.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%
Morty Proxy This is a proxified and sanitized view of the page, visit original site.