Skip to content

SerializableDataTable is a library providing an abstraction class that allows you to serialize and deserialize data to and from a DataTable instance.

License

Notifications You must be signed in to change notification settings

jchristn/SerializableDataTable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

SerializableDataTable

SerializableDataTable is a library providing an abstraction class that allows you to serialize and deserialize data to and from a DataTable instance.

NuGet Version NuGet

Help, Feedback, Contribute

If you have any issues or feedback, please file an issue here in Github. We'd love to have you help by contributing code for new features, optimization to the existing codebase, ideas for future releases, or fixes!

New in v1.0.x

  • Initial release
  • Added markdown support

Example

Refer to the Test project for exercising the library. This is example is using the NuGet package SerializationHelper for simplicity purposes.

using System.Data;
using SerializableDataTables;
using SerializationHelper;

// Create your DataTable and columns
DataTable dt1 = new DataTable("Test");
dt1.Columns.Add("id", typeof(int));
dt1.Columns.Add("name", typeof(String));

// Create your DataRows
dt1.Rows.Add(1, "Hello");
dt1.Rows.Add(2, "World");

SerializableDataTable sdt = SerializableDataTable.FromDataTable(dt);
string json = Serializer.SerializeJson(sdt, true);
Console.WriteLine("JSON: " + Environment.NewLine + json + Environment.NewLine);
/*
{
  "Name": "Test",
  "Columns": [
    {
      "Name": "id",
      "Type": "Int32"
    },
    {
      "Name": "name",
      "Type": "String"
    }
  ],
  "Rows": [
    {
      "id": 1,
      "name": "Hello"
    },
    {
      "id": 2,
      "name": "World"
    }
  ]
}
 */

DataTable dt2 = Serializer.DeserializeJson<SerializableDataTable>(json).ToDataTable();

Supported Data Types

This library supports the following DataTable value types: string, Int32, Int64, decimal, double, float, bool, DateTime, DateTimeOffset, byte, byte[], char, Guid, and Object.

Conversion to Markdown

The SerializableDataTable class has a public method ToMarkdown() which returns a markdown string.

The static class MarkdownConverter can be used with a DataTable or a SerializableDataTable to produce a markdown string.

using System.Data;
using SerializableDataTables;

SerializableDataTable sdt = ...;
string md = sdt.ToMarkdown();

md = MarkdownConverter.Convert(sdt); // full markdown

Console.WriteLine(MarkdownConverter.ConvertHeaders(sdt)); // show only header row and separator row

foreach (string str in MarkdownConverter.IterateRows(sdt))
{
    Console.Write(str); // print each data row, which has a newline separator already added
}

Version History

Refer to CHANGELOG.md for version history.

About

SerializableDataTable is a library providing an abstraction class that allows you to serialize and deserialize data to and from a DataTable instance.

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages