Object

Object operations consist of the filters, transformers, and bufferings. Each of these are described in Basics and Reference documentation. Pnyx does not automatically convert Row data into Object data. For that, you must implement the IObjectConverterFromRow interface (see example below). However, there is a plug-in library that uses AutoMapper to implement IObjectConverterFromRow for automatically converting Row data into Object data. The rest of this page illustrates some usages these operations.

Custom Converter

By default, Pnyx has no built-in way of converting from Row data to Object data. In order to accomplish this, you need to implement the IObjectConverterFromRow interface. This interface provides hooks for converting to and from rows and objects. The following example illustrates its usage.

Custom Converter:
public class Book
{
    public string? Title { get; set; }
    public string? Author { get; set; }
    public int Revisions { get; set; }
}

public class BookConverter : IObjectConverterFromRow
{
    public object rowToObject(List<string?> row, List<string>? header = null)
    {
        return new Book
        {
            Title = row[0],
            Author = row[1],
            Revisions = int.Parse(row[2]!)
        };
    }

    public List<string?> objectToRow(object obj)
    {
        Book book = (Book)obj;
        return new List<string?>
        {
            book.Title,
            book.Author,
            book.Revisions.ToString()
        };
    }

    public List<string> objectToHeader(object obj)
    {
        return new List<string>
        {
            "Title",
            "Author",
            "Revisions"
        };
    }
}
 
const string input = """
Title,Author,Revisions
"Tale of Two Cities","Charles Dickens",3
"Oliver Twist","Charles Dickens",2
Odyssey,Homer,100
""";
        
await using Pnyx p = new Pnyx();
p.readString(input);
p.parseCsv(hasHeader: true);
p.rowToObject(new BookConverter());
p.objectFilter(x => ((Book)x).Revisions > 2);
p.objectToRow();
p.writeStdout();
// Output:
// Title,Author,Revisions
// Odyssey,Homer,100

Auto Mapper

For quick and dirty object conversion, you can use the AutoMapper. This is a controversial topic and it is not part of the built-in Pnyx library. To use it, you must import AutoMapper and pnyx.automapper into your project. However, for simple, non-production mappings, it can be an extremely efficient tool.

AutoMapper Converter (input):
MapperConfiguration config = new MapperConfiguration(_ => {}, NullLoggerFactory.Instance);

var converter = new AutoMapperObjectConverter<Book>(config.CreateMapper());

const string input = """
Title,Author,Revisions
"Tale of Two Cities","Charles Dickens",3
"Oliver Twist","Charles Dickens",2
Odyssey,Homer,100
""";

await using Pnyx p = new Pnyx();
p.readString(input);
p.parseCsv(hasHeader: true);
p.rowToNameValuePair();
p.nameValuePairToObject(converter);
List<Book> books = await p.processCaptureObject<Book>();
Console.WriteLine($"Read {books.Count} books.");
AutoMapper Converter (output):
MapperConfiguration config = new MapperConfiguration(_ => { }, NullLoggerFactory.Instance);

IObjectConverterFromNameValuePair converter = new AutoMapperObjectConverter<Book>(config.CreateMapper());

List<object> books = new();
// populate from database or from file/Pnyx

await using Pnyx p = new Pnyx();
p.readObject(() => books);
p.objectToNameValuePair(converter);
p.nameValuePairToRow(header: new List<string>{"Title","Author","Revisions"});
p.rowToLine();
string csvOutput = await p.processToString();
Console.WriteLine(csvOutput);

Next

Suggested next steps:
  • Line, learn more about Line operations
  • Row, learn more about Row operations
  • NameValuePair, examples of Name-Value Pair operations
  • Output, learn more about Output operations
  • Settings, learn more about settings