15

I am toying around with serializing and deserializing CSV files and I am wondering if there is an existing library, similar in concept to the XmlSerializer, which can declaratively define objects and (de)serialize them to/from a file or stream. I have looked around a bit but have not found anything focused on serialization. I already have pretty solid code for parsing CSV documents per RFC 4180, but what would be really helpful is the serialization part. What I am not looking for is just a parser, advice to use String.Split(), etc.

Is there an existing project out there, or should I build one?

Bonus etiquette question: if I do end up rolling my own serializer, is it appropriate to answer this question with a link to the codeplex project?

Steve Konves
  • 2,648
  • 3
  • 25
  • 44
  • possible duplicate of [Best practices for serializing objects to a custom string format for use in an output file](http://stackoverflow.com/questions/1179816/best-practices-for-serializing-objects-to-a-custom-string-format-for-use-in-an-o) – Ryan Gates Mar 19 '14 at 15:37
  • To restate and emphasize a portion of my original question, I am not interested in simply traversing a list and joining strings with commas. The question deals specifically with declaratively defining objects (done similarly using XmlElementAttribute) and then performing (de)serialization based on those declarations. – Steve Konves Mar 19 '14 at 22:12

3 Answers3

27

I would highly recommend servicestack.text for this purpose. Avialable on nuget:

Install-package servicestack.text

It suports serialization to many data formats and unlike the built in XmlSerializer, you don't need to decorate all your properties with attributes. Here's an example to serialize to CSV.

using ServiceStack.Text;
...

var csv = CsvSerializer.SerializeToCsv(new[]{
    new Dog () {
    Bark = "Woof!",
    Male = true,
    Size = 10
}});
Will Munn
  • 7,363
  • 4
  • 27
  • 32
  • 4
    Just to save time anyone who needs, deserialization of CSV is not yet implemented in ServiceStack. This isn't clear from the initial documentation. It is on the todo list. – nick66 Nov 22 '14 at 00:08
  • 4
    Note this is AGPL-licensed. – Roy Tinker Jan 09 '15 at 01:42
  • 5
    I believe that the comment of @nick66 is no longer true [see here for deserialization details](https://github.com/ServiceStack/ServiceStack/wiki/CSV-Format#csv-deserialization-support) – user25064 Jul 08 '16 at 16:44
  • 3
    Comment by @RoyTinker is not valid anymore too: [ServiseStack.Text is now completly free for commercial use as well](https://github.com/ServiceStack/ServiceStack/blob/master/docs/2016/v4.0.62.md#servicestacktext-is-now-free) – shytikov Feb 27 '18 at 12:32
  • @shytikov That page you linked to _says_ that, but I don't see that anywhere else. Not [on their repo](https://github.com/ServiceStack/ServiceStack.Text/blob/master/license.txt), not [on NuGet](https://www.nuget.org/packages/ServiceStack.Text) (click the "License" link). It's still either AGPL or limited/paid commercial. – Roy Tinker Feb 27 '18 at 19:27
  • 3
    @RoyTinker on their forum I found a post about this, they also say it is free to use in any commercial project without a license key: https://forums.servicestack.net/t/servicestack-text-license/2952 – Patrick Koorevaar Aug 15 '18 at 12:49
  • @PatrickKoorevaar I would be careful here. A forum post might not be legally binding when it comes to licensing. The actual license on github is still AGPL and in the end this is what counts. Everyone using this in a non-FOSS licensed project should get in contact with the developers and make sure they can use the software in this way. – tseifried Sep 04 '19 at 11:38
6

I've used this project (CsvHelper) in the past, and it works similar to the build in .NET serializer classes in the sense that you use attributes to craft the input/output.

There's really no need to roll your own, since there are tons out there. If you do end up rolling your own, feel free to post it. Most users, when answering a question with something they've written themselves (or are affiliated in some way) usually give a disclaimer saying so as a courtesy.

wsanville
  • 37,158
  • 8
  • 76
  • 101
1

You should take a look into FileHelpers Library.

Some sample code from their site:

using FileHelpers; 

// First declare the record class 
[DelimitedRecord(",")] 
public class SampleType 
{ 
    public string Field1; 
    public int    Field2; 
} 

public void WriteExample() 
{ 
    FileHelperEngine engine = new FileHelperEngine(typeof(SampleType)); 

    SampleType[] records = new SampleType[1]; 

    records[0] = new SampleType(); 
    records[0].Field1 = "Hello World"; 
    records[0].Field2 = 12; 

    engine.WriteFile("destination.txt", records); 

    // Now the file contains the created record in this format: 
    //  
    // Hello World,12 
} 
João Angelo
  • 56,552
  • 12
  • 145
  • 147