似乎在序列化中缺少System.Data.DataSet的DataSetName属性。其他东西,例如嵌入式表和行,看起来也不错。
以下是一个VB.Net单元测试,用于演示非序列化的属性以及各种正确序列化的内容。
Dim ds = New DataSet, str as String
Assert.AreEqual("NewDataSet", ds.DataSetName)
ds.DataSetName = "MyDS"
Dim dt = New DataTable
dt.TableName = "MyDT"
Dim dc = New DataColumn
dc.ColumnName = "MyDC"
dc.DataType = GetType(String)
dt.Columns.Add(dc)
ds.Tables.Add(dt)
Dim dr = dt.NewRow
dr.ItemArray = New Object() {"MyValue"}
dt.Rows.Add(dr)
Assert.AreEqual(1, ds.Tables.Count)
Assert.AreEqual("MyDT", ds.Tables(0).TableName)
Assert.AreEqual(1, ds.Tables(0).Columns.Count)
Assert.AreEqual("MyDC", ds.Tables(0).Columns(0).ColumnName)
Assert.AreEqual(1, ds.Tables(0).Rows.Count)
Assert.AreEqual("MyValue", ds.Tables(0).Rows(0)(ds.Tables(0).Columns(0)))
Assert.AreEqual("MyDS", ds.DataSetName)
str = Newtonsoft.Json.JsonConvert.SerializeObject(ds)
ds = Newtonsoft.Json.JsonConvert.DeserializeObject(Of DataSet)(str)
Assert.AreEqual(1, ds.Tables.Count)
Assert.AreEqual("MyDT", ds.Tables(0).TableName)
Assert.AreEqual(1, ds.Tables(0).Columns.Count)
Assert.AreEqual("MyDC", ds.Tables(0).Columns(0).ColumnName)
Assert.AreEqual(1, ds.Tables(0).Rows.Count)
Assert.AreEqual("MyValue", ds.Tables(0).Rows(0)(ds.Tables(0).Columns(0)))
Assert.AreEqual("MyDS", ds.DataSetName, "FAILS - still 'NewDataSet'")
顺便说一句,生成的序列化字符串是
{“ MyDT”:[{“ MyDC”:“ MyValue”}]}
BTW2版本是
Assembly Newtonsoft.Json,版本= 12.0.0.0,文化=中性,PublicKeyToken = 30ad4fe6b2a6aeed