-2

My question is different from other SO questions, because those deserialization samples are for a typed<Customer> POCO class, in my scenario I don't have any types ahead of time. Files are being dropped in a folder from various sources.

Without, the type or header information, how would I deserialize/read a CSV file with unknown headers to a List<>, JSON Array or Enumerable (I don't need a datatable)

using System;
using System.Data;
using System.Collections;
using System.Linq;
using ChoETL;
using System.Text;

public class Program
{
    public static void Main()
    {
        //headers are not known in the SourceFile.csv - i need a list and headers
        //var csvLisFromCho = new ChoCSVReader(@"SourceFile.csv").WithFirstLineHeader();

        StringBuilder csvIn = new StringBuilder(@"ID,Name
        1, David
        2, Bob");
        using (var r = new ChoCSVReader(csvIn).WithFirstLineHeader())
        {
            var list = r.Select(r1 => r1.Values).ToList();
            Console.WriteLine("Coverted List from Raj");
            list.ForEach(Console.WriteLine);
        }

    }
}
Transformer
  • 6,963
  • 2
  • 26
  • 52
  • 1
    "Deserialize" is probably the wrong word here...or maybe it is "CSV". It would be very odd for CSV data to also be serialized data. That you mention JSON Array as a direct target makes it sound like it may not be a CSV at all. – Ňɏssa Pøngjǣrdenlarp Dec 14 '18 at 01:35
  • 1
    Headers aren't required to parse a CSV last time I looked – OneCricketeer Dec 14 '18 at 01:36
  • Its not about the headers, my csv file may or may not have headers.. I need to get a List<> from the CSV – Transformer Dec 14 '18 at 01:45
  • 1
    What have you tried? There are clear examples on the [FileHelpers](https://www.filehelpers.net/quickstart/) and [Choetl](https://github.com/Cinchoo/ChoETL/wiki/QuickCSVLoad) documentation pages. – shamp00 Dec 14 '18 at 09:16
  • @shamp00 please look at your links and read my question again, those deserialization if so for a typed POCO class, in my scenario I don;t have any types ahead of time. – Transformer Dec 14 '18 at 23:51

1 Answers1

1

Here you go, to get the list from CSV

StringBuilder csvIn = new StringBuilder(@"ID,Name
1, David
2, Bob");

using (var r = new ChoCSVReader(csvIn)
    .WithFirstLineHeader()
    )
{
    var list = r.Select(r1 => r1.Values).ToList();
}

UPDATE:

To get the headers, cast record to IDictionary and use Keys property on it to get the keys.

In order to auto discover the datatypes of CSV columns, you must set the MaxScanRows on parser. Otherwise all columns will be treated as string type.

StringBuilder csvIn = new StringBuilder(@"ID,Name,Date
1, David, 1/1/2018
2, Bob, 2/12/2019");

using (var r = new ChoCSVReader(csvIn)
    .WithFirstLineHeader()
    .WithMaxScanRows(2)
    )
{
    foreach (IDictionary<string, object> rec in r.Take(1))
    {
        foreach (var kvp in rec)
            Console.WriteLine($"{kvp.Key} - {r.Configuration[kvp.Key].FieldType}");
    }
}
Cinchoo
  • 6,088
  • 2
  • 19
  • 34
  • Will it create the types for Dates in the list, or JSON array, and where is the header information stored, is there a get headers function – Transformer Dec 14 '18 at 23:57
  • https://dotnetfiddle.net/LnN18g does not seem to work, with the code, I put it on the fiddle – Transformer Dec 15 '18 at 00:14
  • worked well, marked as a answer, not sure why my question is down voted, will delete. The r.configuration was key :") – Transformer Dec 17 '18 at 07:04