gpt4 book ai didi

c# - 如何使用 System.Text.Json 将 double[,] 二维数组序列化为 JSON?

转载 作者:行者123 更新时间:2023-12-05 02:01:49 29 4
gpt4 key购买 nike

我需要写一个 double[,]使用 System.Text.Json 到 JSON 文件,但此序列化程序不支持二维数组。我需要一些帮助来转换我的 double[,]List<List<double>>这样我就可以保存它。

也欢迎使用 System.Text.Json 保存二维数组的其他方法。

最佳答案

由于您使用 System.Text.Json 将二维数组序列化为 JSON ,您可以结合 Convert multidimensional array to jagged array in C# 中的答案 Converting jagged array to 2D array C# 创建一个 JsonConverterFactory 将任何二维数组从 JSON 序列化到 JSON,如下所示:

public class Array2DConverter : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert) => typeToConvert.IsArray && typeToConvert.GetArrayRank() == 2;

public override JsonConverter CreateConverter(Type type, JsonSerializerOptions options) =>
(JsonConverter)Activator.CreateInstance(
typeof(Array2DConverterInner<>).MakeGenericType(new [] { type.GetElementType() }),
BindingFlags.Instance | BindingFlags.Public,
binder: null,
args: new object[] { options },
culture: null);

class Array2DConverterInner<T> : JsonConverter<T [,]>
{
readonly JsonConverter<T> _valueConverter;

public Array2DConverterInner(JsonSerializerOptions options) =>
this._valueConverter = (typeof(T) == typeof(object) ? null : (JsonConverter<T>)options.GetConverter(typeof(T))); // Encountered a bug using the builtin ObjectConverter

public override void Write(Utf8JsonWriter writer, T [,] array, JsonSerializerOptions options)
{
// Adapted from this answer https://stackoverflow.com/a/25995025/3744182
// By https://stackoverflow.com/users/3258160/pedro
// To https://stackoverflow.com/questions/21986909/convert-multidimensional-array-to-jagged-array-in-c-sharp
var rowsFirstIndex = array.GetLowerBound(0);
var rowsLastIndex = array.GetUpperBound(0);
var columnsFirstIndex = array.GetLowerBound(1);
var columnsLastIndex = array.GetUpperBound(1);

writer.WriteStartArray();
for (var i = rowsFirstIndex; i <= rowsLastIndex; i++)
{
writer.WriteStartArray();
for (var j = columnsFirstIndex; j <= columnsLastIndex; j++)
_valueConverter.WriteOrSerialize(writer, array[i, j], options);
writer.WriteEndArray();
}
writer.WriteEndArray();
}

public override T [,] Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
JsonSerializer.Deserialize<List<List<T>>>(ref reader, options)?.To2D();
}
}

public static partial class ArrayExtensions
{
public static T[,] To2D<T>(this List<List<T>> source)
{
// Adapted from this answer https://stackoverflow.com/a/26291720/3744182
// By https://stackoverflow.com/users/3909293/diligent-key-presser
// To https://stackoverflow.com/questions/26291609/converting-jagged-array-to-2d-array-c-sharp
var firstDim = source.Count;
var secondDim = source.Select(row => row.Count).FirstOrDefault();
if (!source.All(row => row.Count == secondDim))
throw new InvalidOperationException();
var result = new T[firstDim, secondDim];
for (var i = 0; i < firstDim; i++)
for (int j = 0, count = source[i].Count; j < count; j++)
result[i, j] = source[i][j];
return result;
}
}

public static class JsonSerializerExtensions
{
public static void WriteOrSerialize<T>(this JsonConverter<T> converter, Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
if (converter != null)
converter.Write(writer, value, options);
else
JsonSerializer.Serialize(writer, value, typeof(T), options);
}
}

然后像这样使用它:

var array1 = new double [,] { {1.1, 1.2, 1.3 }, { 2.1, 2.2, 2.3 } };

var options = new JsonSerializerOptions
{
Converters = { new Array2DConverter() },
};
var json = JsonSerializer.Serialize(array1, options);
var array2 = JsonSerializer.Deserialize<double [,]>(json, options);

注意事项:

  • 虽然有必要将整个 JSON 读入中间 List<List<T>>要正确计算所需的二维数组外部维度,可以将二维数组直接写入 JSON,而无需转换为中间表示形式。

演示 fiddle here .

关于c# - 如何使用 System.Text.Json 将 double[,] 二维数组序列化为 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66280645/

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