Git Product home page Git Product logo

antonio-leonardo / csvserializer Goto Github PK

View Code? Open in Web Editor NEW
13.0 4.0 4.0 18 KB

C# class giving a simplest with performance serializer functionalities to manipulate CSV strings; several test cases were executed in order to treat the particularities of a sequence of CSV characters; in this publication, is considered stable, it is implemented in productive environments with large scale of data generated or consumed in CSV

License: MIT License

C# 100.00%
csharp c-sharp serialization deserialization csv csv-parser csv-parsing csv-reader csv-reading csv-format

csvserializer's Introduction

C# CSV Serializer

This C# class giving a simplest way with performance serializer functionalities to manipulate CSV strings; several test cases were executed in order to treat the particularities of a sequence of CSV characters; in this publication, is considered stable, it is implemented in productive environments with large scale of data generated or consumed in CSV daily.

Specifications: This 'CsvSerializer' class enjoy best pratices of Design Patterns result in a powerful CSV serialization and deserialization functionality, giving to Developer other data sctructure options like DataTable object or a custom C# Plain Old CLR Object (Entity class) with capacity to define column name and order exihbition. Both methods 'Serialize' and 'Deserialize' contains on important argument named "csvSeparator" to gives flexible generation of CSV.

Bellow are examples of using the 'CsvSerializer':

1) Serialize

1.1) Using a POCO (Plain Old CLR Objects) class, with native .NET DataMemberAtribute (attribute is not mandatory):

public class ExampleModel
{
    [DataMember(Name = "My Property 1", Order = 4)]
    public string MyProperty1 { get; set; }

    [DataMember(Name = "My Property 2", Order = 1)]
    public string MyProperty2 { get; set; }

    [DataMember(Name = "Any Property", Order = 3)]
    public string AnyProperty { get; set; }

    [DataMember(Name = "Property comments change order", Order = 6)]
    public string PropertyWithChangeOrder { get; set; }

    [DataMember(Name = "Some Property", Order = 2)]
    public string SomeProperty { get; set; }

    [DataMember(Name = "Capacity Property", Order = 5)]
    public string CapacityProperty { get; set; }
}

1.2) Create a collection based on Entity class above:

List<ExampleModel> list = new List<ExampleModel>();

list.Add(new ExampleModel()
{
    AnyProperty = "any 1",
    CapacityProperty = "20",
    MyProperty1 = "my 1",
    MyProperty2 = "my 2",
    PropertyWithChangeOrder = "Comments to change and or texts...",
    SomeProperty = "some 1"
});
list.Add(new ExampleModel()
{
    AnyProperty = "any yna",
    CapacityProperty = "45",
    MyProperty1 = "11 my",
    MyProperty2 = "22 my",
    PropertyWithChangeOrder = "Another comment to change contents!",
    SomeProperty = "2 some 1"
});
list.Add(new ExampleModel()
{
    AnyProperty = @"\/any yna\/",
    CapacityProperty = "57",
    MyProperty1 = "1my1",
    MyProperty2 = "2my2",
    PropertyWithChangeOrder = "Texts with comments to rich all tests",
    SomeProperty = "some3"
});

1.3) Execute Serialize method, and Voilà!

string csvSerialized = CsvSerializer<ExampleModel>.Serialize(';', list);

1.4) This is the result of the string bulk:

My Property 2;Some Property;Any Property;My Property 1;Capacity Property;Property comments change order;

my 2;some 1;any 1;my 1;20;Comments to change and or texts...;

22 my;2 some 1;any yna;11 my;45;Another comment to change contents!;

2my2;some3;\/any yna\/;1my1;57;Texts with comments to rich all tests;

1.5) Pear attention: if you want to save '.csv' file, remeber to use Encoding UTF8, according CSV specifications:

File.WriteAllText(@"\\path\to\save\file.csv", csvSerialized, Encoding.UTF8);

2) Deserialize

2.1) The developer may consume any '.csv' file by sugestted instruction:

string csvToDeserialize = File.ReadAllText(@"\\path\to\read\file.csv");

2.2) And, if dont know the data column arrange, may populate a DataTable objet:

DataTable dtDeserialized = CsvSerializer.Deserialize(';', csvToDeserialize);

2.2) Or populate a collection of Entity class (like array or list):

ExampleModel[] arrayCollection = CsvSerializer<ExampleModel>.Deserialize(';', csvToDeserialize).ToArray();
List<ExampleModel> listCollection = CsvSerializer<ExampleModel>.Deserialize(';', csvToDeserialize).ToList();

License

View MIT license

csvserializer's People

Contributors

antonio-leonardo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

csvserializer's Issues

Doesn't compile in .NET Core 3.1

Thx for posting this utility/tool! Just had one issue when trying to use it...getting compile error with this source code.
It looks like you may be missing the code for the extension method GetElementsResult()?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.