我有一個數據表,其中創建新行的數據表中的某些列的數據表中必須放置此默認值,直到用戶更改爲止。 我已經使用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