1

Does anyone know how to convert VB.net datatable to JSON using ServiceStack JsonSerializer?

On ServiceStack Docs website you can find following example but this is C#, plus I cannot understand the way this works.

string TypeSerializer.SerializeToString<T>(T value);
void TypeSerializer.SerializeToWriter<T>(T value, TextWriter writer);

T TypeSerializer.DeserializeFromString<T>(string value);
T TypeSerializer.DeserializeFromReader<T>(TextReader reader);

Example datatable

Dim table As New DataTable
        table.Columns.Add("Dosage", GetType(Integer))
        table.Columns.Add("Drug", GetType(String))
        table.Columns.Add("Patient", GetType(String))
        table.Columns.Add("Date", GetType(DateTime))
        table.Rows.Add(25, "Indocin", "David", DateTime.Now)
        table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now)
        table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now)
        table.Rows.Add(21, "Combivent", "Janet", DateTime.Now)
        table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now)

Any suggestions much appreciated.

Iladarsda
  • 10,640
  • 39
  • 106
  • 170

1 Answers1

1

ServiceStacks JSON Serializer does not support serializing DataTables directly. You would need to convert them to POCOs and serializer those.

mythz
  • 141,670
  • 29
  • 246
  • 390
  • Can you give me a simple example from which I could work up? Many thanks. – Iladarsda Nov 15 '12 at 13:51
  • 1
    Here are a couple of links explaining how to convert a DataTable into a POCO: http://sharpdevpt.blogspot.com/2010/05/convert-datatable-into-poco-using.html or http://stackoverflow.com/questions/1354034/how-do-i-convert-a-datatable-into-a-poco-object-in-asp-net-mvc – mythz Nov 15 '12 at 13:57
  • In 2022 ServiceStack.Text still doesn't support datatables. Luckily C# now supports `dynamic`. I use an extension method to convert the datatable to a `List` which serializes properly in ServiceStack. https://stackoverflow.com/a/7794962/591285 – clamchoda Dec 19 '22 at 21:11