# Failing to serialize 2D arrays \[Elasticsearch.NET\]

**URL:** <https://discuss.elastic.co/t/failing-to-serialize-2d-arrays-elasticsearch-net/360094>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [May 23, 2024, 5:20pm UTC](https://discuss.elastic.co/t/failing-to-serialize-2d-arrays-elasticsearch-net/360094 "2024-05-23T17:20:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Motsols](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/motsols/32/134658_2.png) [@Motsols](https://discuss.elastic.co/u/Motsols)\
**Post date:** [May 23, 2024, 5:20pm UTC](https://discuss.elastic.co/t/failing-to-serialize-2d-arrays-elasticsearch-net/360094/1 "2024-05-23T17:20:15Z")

</div>

A simple 2D array (aka multi-dimensional array) for a linestring such as this fails to serialize into json:

```auto
new[,]
{
    { 10.0, 10.0 },
    {15.0, 15.0 }
}

```

With the exception message

> Elastic.Transport.UnexpectedTransportException: 'Serialization and deserialization of 'System.Double[,]' instances is not supported. Path: $.Coordinates.'

This seems odd that geojson support has disappeared all of a sudden, as that was a main reason for choosing this DB many years ago. Not everyone uses Elasticsearch just for storing logs.

Reading up on the issue this seems to be because System.Text.Json is now being used, [and that doesn't support 2d arrays according to Microsoft documentation](https://learn.microsoft.com/en-gb/dotnet/standard/serialization/system-text-json/supported-collection-types), so we effectively lost functionality.

**What is now a good way of proceeding to use v 8.13 of the dotnet client for geojson data?**

* * *

Full Program.cs as a demo of the issue

```auto
using Elastic.Clients.Elasticsearch;
using Elastic.Transport;

namespace Testing
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            var settings = new ElasticsearchClientSettings(new Uri("http://localhost:9200"))
                .Authentication(new BasicAuthentication("elastic", "Too4Strong"));

            var client = new ElasticsearchClient(settings);

            var line = new Line
            {
                Type = "LineString",
                Coordinates = new[,]
                {
                    { 10.0, 10.0 },
                    {15.0, 15.0 }
                }
            };

            await client.Indices.CreateAsync("testing");
            var result = await client.IndexAsync(line, (IndexName)"testing");
        }
    }

    public record Line
    {
        public required string Type { get; set; }
        public required double[,] Coordinates { get; set; }
    }
}

```

---

<div class="post-metadata">

**Author:** ![Motsols](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/motsols/32/134658_2.png) [@Motsols](https://discuss.elastic.co/u/Motsols)\
**Post date:** [June 2, 2024, 4:55pm UTC](https://discuss.elastic.co/t/failing-to-serialize-2d-arrays-elasticsearch-net/360094/2 "2024-06-02T16:55:52Z")

</div>

I believe I've found the solution to this question.  
The trick is to create a converter for the data type ourselves and then register that converter. I hope Microsoft resolves this on their side in the future.

The converter itself worked straight off the bat from this StackOverflow answer [c# - How can I serialize a double[,] 2d array to JSON using System.Text.Json? - Stack Overflow](https://stackoverflow.com/questions/66280645/how-can-i-serialize-a-double-2d-array-to-json-using-system-text-json)

On my API I can't pass in options every time the JSON arrives at the controller so I registered it in startup.cs, it's also a lot cleaner.

```auto
services
    .AddControllers()
    .AddJsonOptions(options => options.JsonSerializerOptions.Converters.Add(new Array2DConverter()));

```

Now I'm able to entirely use system.text.json without mixing in Newtonsoft.
