3
votes

I am using the Csv Type Provider to read data from a local csv file.

I want to export the data as json, so I am taking each row and serializing it using the json.net Library with JsonConvert.SerializeObject(x).

The problem is that each row is modeled as a tuple, meaning that the column headers do not become property names when serializing. Instead I get Item1="..."; Item2="..." etc.

How can I export to Json without 'hand-rolling' a class/record type to hold the values and maintain the property names?

1
Are you using the type provider from the FSharp.Data library? Its documentation states quite clearly that it generates actual types with properties, not tuples. - piaste
Yep. The item type is CsvProvider<...>.Row. Intellisense shows the named properties so it feels like a record type but serializing suggests otherwise. - Nick

1 Answers

1
votes

The TypeProvider works by providing compile time type safety. The actual code that is compiled maps (at compile time) the nice accessors to tupled values (for performance reasons, I guess). So at run time the JSON serializer sees tuples only.

AFAIK there is no way around hand-rolling records. (That is unless we eventually get type providers that are allowed to take types as parameters which would allow a Lift<T>-type provider or the CSV type provider implementation is adjusted accordingly.)