dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.63k stars 4.57k forks source link

The type 'System.Int32[,]' is not supported. #89843

Open Sc7-git opened 11 months ago

Sc7-git commented 11 months ago

Description

            int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
            var array2DJson= System.Text.Json.JsonSerializer.Serialize(array2D);
            var json = "[[\"uhhvle7p55hc\",\"uhhvle7p55hd\",\"uhhvle7p55hh\"]]";
            var array = System.Text.Json.JsonSerializer.Deserialize<string[,]>(json);

Reproduction Steps

            int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
            var array2DJson= System.Text.Json.JsonSerializer.Serialize(array2D);
            var json = "[[\"uhhvle7p55hc\",\"uhhvle7p55hd\",\"uhhvle7p55hh\"]]";
            var array = System.Text.Json.JsonSerializer.Deserialize<string[,]>(json);

Expected behavior

Return serialization result

Actual behavior

The type 'System.Int32[,]' is not supported.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

ghost commented 11 months ago

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis See info in area-owners.md if you want to be subscribed.

Issue Details
### Description ``` int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; var array2DJson= System.Text.Json.JsonSerializer.Serialize(array2D); var json = "[[\"uhhvle7p55hc\",\"uhhvle7p55hd\",\"uhhvle7p55hh\"]]"; var array = System.Text.Json.JsonSerializer.Deserialize(json); ``` ### Reproduction Steps ``` int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; var array2DJson= System.Text.Json.JsonSerializer.Serialize(array2D); var json = "[[\"uhhvle7p55hc\",\"uhhvle7p55hd\",\"uhhvle7p55hh\"]]"; var array = System.Text.Json.JsonSerializer.Deserialize(json); ``` ### Expected behavior Return serialization result ### Actual behavior The type 'System.Int32[,]' is not supported. ### Regression? _No response_ ### Known Workarounds _No response_ ### Configuration _No response_ ### Other information _No response_
Author: Sc7-git
Assignees: -
Labels: `area-System.Text.Json`
Milestone: -
huoyaoyuan commented 11 months ago

Duplicate of #44347. Currently there's no support for MD array.

gregsdennis commented 11 months ago

Three years later, and STJ is significantly more mature. Can we use this issue to have another look at implementing it?

Not sure what a JSON representation of a MD array would look like, though. I wonder if there are any precedents.

eiriktsarpalis commented 11 months ago

Not sure what a JSON representation of a MD array would look like, though.

There doesn't appear to be a standard representation being used anywhere, although using jagged arrays is probably the safest (although likely not the most efficient) representation. I found this article discussing various approaches.

m-gallesio commented 9 months ago

There doesn't appear to be a standard representation being used anywhere, although using jagged arrays is probably the safest (although likely not the most efficient) representation.

Newtonsoft.Json does this too:

int[,] matrix = new int[2, 3]
{
    { 1, 2, 3 },
    { 4, 5, 6 }
};
System.Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(matrix));

[[1,2,3],[4,5,6]]