序列化 DataTable 包括 RowError 和 "column errors"
Serialize DataTable including RowError and "column errors"
我想将带有 RowError 和列错误的 .NET DataTable 序列化为 JSON。我这样做:
var dt = new DataTable();
dt.Columns.Add("Col1");
dt.Rows.Add("A");
dt.Rows[0].RowError = "Test row error";
dt.Rows[0].SetColumnError(0, "Test column error");
var json = JsonConvert.SerializeObject(dt);
结果 JSON 如下所示:
[{"Col1":"A"}]
不包含行错误或列错误信息。
有没有办法序列化包含“错误”信息的 DataTable?
谢谢。
I want to serialize to JSON a .NET DataTable with RowError and column errors
如您所见,默认情况下,只有 ColumnName
及其值被序列化。要包含您想要的其他属性,您可以通过编写自己的 JsonConverter
class 来实现,请参阅 Custom JsonConverter 了解更多信息。您需要两个转换器,一个用于 DataTable
,另一个用于 DataRow
。
*请注意,每个 class 都需要有这个命名空间:using Newtonsoft.Json;
CustomDataTableConverter
public class CustomDataTableConverter : JsonConverter
{
public override bool CanConvert(Type objectType) => typeof(DataTable).IsAssignableFrom(objectType);
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
DataTable table = value as DataTable;
CustomDataRowConverter converter = new CustomDataRowConverter();
writer.WriteStartObject();
writer.WritePropertyName("DataRows");
writer.WriteStartArray();
foreach (DataRow row in table.Rows)
{
converter.WriteJson(writer, row, serializer);
}
writer.WriteEndArray();
writer.WriteEndObject();
}
}
CustomDataRowConverter
public class CustomDataRowConverter : JsonConverter
{
public override bool CanConvert(Type objectType) => typeof(DataRow).IsAssignableFrom(objectType);
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
DataRow row = value as DataRow;
JsonSerializer ser = new JsonSerializer();
writer.WriteStartObject();
// Serializes the row error property
writer.WritePropertyName(nameof(row.RowError));
ser.Serialize(writer, row.RowError);
foreach (DataColumn column in row.Table.Columns)
{
writer.WritePropertyName(column.ColumnName);
ser.Serialize(writer, row[column]);
// Serialize the columns error description
writer.WritePropertyName("ColumnError");
ser.Serialize(writer, row.GetColumnError(column));
}
writer.WriteEndObject();
}
}
既然您已经有了所需的转换器,下面是您如何在您的案例中使用它们。请注意,我添加了另一个数据行以显示此处的差异。
var dt = new DataTable();
dt.Columns.Add("Col1");
dt.Rows.Add("A");
dt.Rows.Add("B");
dt.Rows[0].RowError = "Test row error";
dt.Rows[0].SetColumnError(0, "Test column error");
dt.Rows[1].RowError = "Test row error B";
dt.Rows[1].SetColumnError(0, "Test column error B");
JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
jsonSerializerSettings.Converters.Add(new CustomDataTableConverter());
jsonSerializerSettings.Converters.Add(new CustomDataRowConverter());
var json = JsonConvert.SerializeObject(dt, Formatting.Indented, jsonSerializerSettings);
运行时,我的输出如下:
{
"DataRows": [
{
"RowError": "Test row error",
"Col1": "A",
"ColumnError": "Test column error"
},
{
"RowError": "Test row error B",
"Col1": "B",
"ColumnError": "Test column error B"
}
]
}
还有一点要记住,我没有做这个的反序列化部分,只有序列化部分,但它是相似的。
我想将带有 RowError 和列错误的 .NET DataTable 序列化为 JSON。我这样做:
var dt = new DataTable();
dt.Columns.Add("Col1");
dt.Rows.Add("A");
dt.Rows[0].RowError = "Test row error";
dt.Rows[0].SetColumnError(0, "Test column error");
var json = JsonConvert.SerializeObject(dt);
结果 JSON 如下所示:
[{"Col1":"A"}]
不包含行错误或列错误信息。
有没有办法序列化包含“错误”信息的 DataTable?
谢谢。
I want to serialize to JSON a .NET DataTable with RowError and column errors
如您所见,默认情况下,只有 ColumnName
及其值被序列化。要包含您想要的其他属性,您可以通过编写自己的 JsonConverter
class 来实现,请参阅 Custom JsonConverter 了解更多信息。您需要两个转换器,一个用于 DataTable
,另一个用于 DataRow
。
*请注意,每个 class 都需要有这个命名空间:using Newtonsoft.Json;
CustomDataTableConverter
public class CustomDataTableConverter : JsonConverter
{
public override bool CanConvert(Type objectType) => typeof(DataTable).IsAssignableFrom(objectType);
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
DataTable table = value as DataTable;
CustomDataRowConverter converter = new CustomDataRowConverter();
writer.WriteStartObject();
writer.WritePropertyName("DataRows");
writer.WriteStartArray();
foreach (DataRow row in table.Rows)
{
converter.WriteJson(writer, row, serializer);
}
writer.WriteEndArray();
writer.WriteEndObject();
}
}
CustomDataRowConverter
public class CustomDataRowConverter : JsonConverter
{
public override bool CanConvert(Type objectType) => typeof(DataRow).IsAssignableFrom(objectType);
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
DataRow row = value as DataRow;
JsonSerializer ser = new JsonSerializer();
writer.WriteStartObject();
// Serializes the row error property
writer.WritePropertyName(nameof(row.RowError));
ser.Serialize(writer, row.RowError);
foreach (DataColumn column in row.Table.Columns)
{
writer.WritePropertyName(column.ColumnName);
ser.Serialize(writer, row[column]);
// Serialize the columns error description
writer.WritePropertyName("ColumnError");
ser.Serialize(writer, row.GetColumnError(column));
}
writer.WriteEndObject();
}
}
既然您已经有了所需的转换器,下面是您如何在您的案例中使用它们。请注意,我添加了另一个数据行以显示此处的差异。
var dt = new DataTable();
dt.Columns.Add("Col1");
dt.Rows.Add("A");
dt.Rows.Add("B");
dt.Rows[0].RowError = "Test row error";
dt.Rows[0].SetColumnError(0, "Test column error");
dt.Rows[1].RowError = "Test row error B";
dt.Rows[1].SetColumnError(0, "Test column error B");
JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
jsonSerializerSettings.Converters.Add(new CustomDataTableConverter());
jsonSerializerSettings.Converters.Add(new CustomDataRowConverter());
var json = JsonConvert.SerializeObject(dt, Formatting.Indented, jsonSerializerSettings);
运行时,我的输出如下:
{
"DataRows": [
{
"RowError": "Test row error",
"Col1": "A",
"ColumnError": "Test column error"
},
{
"RowError": "Test row error B",
"Col1": "B",
"ColumnError": "Test column error B"
}
]
}
还有一点要记住,我没有做这个的反序列化部分,只有序列化部分,但它是相似的。