简体   繁体   中英

How can I serialize a double[,] 2d array to JSON using System.Text.Json?

I need to write a double[,] to a JSON file using System.Text.Json but this serializer doesn't support 2d arrays. I need some help converting my double[,] to a List<List<double>> so I can save it.

Other methods of saving the 2d array using using System.Text.Json are also welcome.

Since you are serializing a 2d array to JSON using System.Text.Json , you can combine the answers from Convert multidimensional array to jagged array in C# and Converting jagged array to 2D array C# to create a JsonConverterFactory to serialize any 2d array from and to JSON as follows:

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);
    }
}

And the use it like so:

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);

Notes:

  • While it is necessary to read the entire JSON into an intermediate List<List<T>> to compute the required 2d array outer dimension correctly, a 2d array can be written directly to JSON without needing to convert to an intermediate representation.

Demo fiddle here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM