Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there such thing as a CSV Serializer? (similar to XmlSerializer)

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?

like image 612
Steve Konves Avatar asked Jun 20 '12 16:06

Steve Konves


People also ask

Is CSV a serialization?

The CSV serialzation is the format how record are serialized in the orientdb 0. and 1. version. Documents are serialized in a proprietary format (as a string) derived from JSON, but more compact.

What serializer does WCF use?

Windows Communication Foundation (WCF) uses the DataContractSerializer as its default serialization engine to convert data into XML and to convert XML back into data.

What is serializing a file?

Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.


1 Answers

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
}});
like image 178
Will Munn Avatar answered Sep 21 '22 14:09

Will Munn