我有一个数据表,其中创建新行的数据表中的某些列的数据表中必须放置此默认值,直到用户更改为止。 我已经使用JSON对保存/加载的数据表进行序列化和反序列化。 使用JSON的序列化/反序列化成功加载数据表中的先前数据,但它不会恢复默认值,并且不会用于创建新行。 有没有办法强制JSON序列化/反序列化数据表的默认值? 谢谢。 示例代码如下:JSON无法序列化/反序列化数据表列的缺省值
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable("ff");
DataColumn col1 = new DataColumn();
col1.ColumnName = "dd";
col1.DataType = typeof(bool);
col1.DefaultValue = true;
dt.Columns.Add(col1);
Console.WriteLine("Before Serialization");
Console.WriteLine(dt.NewRow()["dd"].ToString());//brings True
string serializedStr = Newtonsoft.Json.JsonConvert.SerializeObject(dt, serializationetting);
DataTable deserializedDt = Newtonsoft.Json.JsonConvert.DeserializeObject<DataTable>(serializedStr, serializationetting) as DataTable;
deserializedDt.Rows.Add(deserializedDt.NewRow());
Console.WriteLine("After Serialization");
Console.WriteLine(deserializedDt.NewRow()["dd"]);//brings empty
Console.ReadLine();
}
public static JsonSerializerSettings serializationetting = new JsonSerializerSettings()
{
TypeNameAssemblyFormat = FormatterAssemblyStyle.Full,
TypeNameHandling = TypeNameHandling.Objects,
Formatting = Formatting.Indented,
};
}
}
显示您的代码。 –
我已添加代码。 – all