gpt4 book ai didi

asp.net-core - 使用当前版本的 System.Text.Json.JsonSerializer 序列化 DataSet

转载 作者:行者123 更新时间:2023-12-05 08:31:13 26 4
gpt4 key购买 nike

对于如何使用 System.Text.Json.JsonSerializer 序列化 DataSet、DataTable,您有什么建议吗?
目前它抛出这个异常:'检测到不支持的可能对象循环。这可能是由于循环或对象深度大于最大允许深度 64。'

最佳答案

目前没有对 DataSet 等类型的内置支持和 DataTableSystem.Text.Json (从 .NET Core 3.1 开始)。为了能够序列化这些类型,您需要实现自己的 JsonConverter<T>对于您需要的类型并在 JsonSerializerOptions 中注册它.为您要求的特定类型编写序列化应该相当容易。

这是一个适用于序列化的示例(省略了反序列化组件):

public class DataTableConverter : JsonConverter<DataTable>
{
public override DataTable Read(ref Utf8JsonReader reader, Type typeToConvert,
JsonSerializerOptions options)
{
throw new NotImplementedException();
}

public override void Write(Utf8JsonWriter writer, DataTable value,
JsonSerializerOptions options)
{
writer.WriteStartArray();

foreach (DataRow row in value.Rows)
{
writer.WriteStartObject();
foreach (DataColumn column in row.Table.Columns)
{
object columnValue = row[column];

// If necessary:
if (options.IgnoreNullValues)
{
// Do null checks on the values here and skip writing.
}

writer.WritePropertyName(column.ColumnName);
JsonSerializer.Serialize(writer, columnValue, options);
}
writer.WriteEndObject();
}

writer.WriteEndArray();
}
}

public class DataSetConverter : JsonConverter<DataSet>
{
public override DataSet Read(ref Utf8JsonReader reader, Type typeToConvert,
JsonSerializerOptions options)
{
throw new NotImplementedException();
}

public override void Write(Utf8JsonWriter writer, DataSet value,
JsonSerializerOptions options)
{
writer.WriteStartObject();
foreach (DataTable table in value.Tables)
{
writer.WritePropertyName(table.TableName);
JsonSerializer.Serialize(writer, table, options);
}
writer.WriteEndObject();
}
}

private static void DataSet_Serialization_WithSystemTextJson()
{
var options = new JsonSerializerOptions()
{
Converters = { new DataTableConverter(), new DataSetConverter() }
};

(DataTable table, DataSet dataSet) = GetDataSetAndTable();

string jsonDataTable = JsonSerializer.Serialize(table, options);
// [{"id":0,"item":"item 0"},{"id":1,"item":"item 1"}]
Console.WriteLine(jsonDataTable);

string jsonDataSet = JsonSerializer.Serialize(dataSet, options);
// {"Table1":[{"id":0,"item":"item 0"},{"id":1,"item":"item 1"}]}
Console.WriteLine(jsonDataSet);

// Local function to create a sample DataTable and DataSet
(DataTable, DataSet) GetDataSetAndTable()
{
dataSet = new DataSet("dataSet");

table = new DataTable();
DataColumn idColumn = new DataColumn("id", typeof(int))
{
AutoIncrement = true
};

DataColumn itemColumn = new DataColumn("item");

table.Columns.Add(idColumn);
table.Columns.Add(itemColumn);

dataSet.Tables.Add(table);

for (int i = 0; i < 2; i++)
{
DataRow newRow = table.NewRow();
newRow["item"] = "item " + i;
table.Rows.Add(newRow);
}

dataSet.AcceptChanges();

return (table, dataSet);
}
}

本文档可能会提供更多指导: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-converters-how-to

关于asp.net-core - 使用当前版本的 System.Text.Json.JsonSerializer 序列化 DataSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59780446/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com