using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
public static void Main()
ITraceWriter traceWriter = new MemoryTraceWriter();
Console.WriteLine("Deseriialize DataTable to Json with byte[]");
Console.WriteLine("The Original datatable structure");
var settings = new JsonSerializerSettings
TypeNameHandling =TypeNameHandling.Objects,
TraceWriter = traceWriter,
Formatting = Newtonsoft.Json.Formatting.Indented,
var json = JsonConvert.SerializeObject(dt, settings);
var dt2= JsonConvert.DeserializeObject<DataTable>(json, settings);
Console.WriteLine("The generated datatable structure");
Console.WriteLine("***** Json Trace******");
Console.WriteLine(traceWriter);
Console.WriteLine("***** Exception ******");
Console.WriteLine("Deserialization to <DataTable> ,with TypeNameHandling.Objects, raise Exception: {0} ",ex.Message);
Console.WriteLine("***** Exception details******");
public static DataTable GetDataTable()
DataTable dataTable = new DataTable("table");
dataTable.Columns.Add("f1", typeof(byte[]));
dataTable.Columns.Add("f2", typeof(string));
dataTable.Columns.Add("id", typeof(int));
DataRow newRow = dataTable.NewRow();
newRow["f1"] = new byte[]{0xE6,0x10, 0x00,0x24,0x40 };
dataTable.Rows.Add(newRow);
newRow = dataTable.NewRow();
newRow["f1"] = new byte[]{0xE6,0x1a, 0xf0,0x24,0x40 };
newRow["f2"] = "hi world";
dataTable.Rows.Add(newRow);
public static void DumpTableStructure(DataTable dt)
foreach (DataColumn column in dt.Columns)
Console.WriteLine("Name: {0} type: {1}", column.ColumnName, column.DataType);