Skip to content

Unable to deserialize ArraySegment<int> with System.Text.Json.JsonSerializer.Deserialize #107783

Open
@isukces

Description

Description

Unable to deserialize ArraySegment<int> using System.Text.Json.JsonSerializer, even when the value was serialized using System.Text.Json.JsonSerializer.

Reproduction Steps

Use XUnit test below:

[Fact]
public void Should_serialize_with_system_text_json()
{
    int[] src          = [1, 2, 3];
    var   arraySegment = new ArraySegment<int>(src);
    var   json         = System.Text.Json.JsonSerializer.Serialize(arraySegment);
    _testOutputHelper.WriteLine(json);
    var obj = System.Text.Json.JsonSerializer.Deserialize<ArraySegment<int>>(json);
    Assert.Equal(3, obj.Count);
}

Expected behavior

The System.Text.Json.JsonSerializer should be able to successfully deserialize an ArraySegment<int> object that was serialized using the same or other serializer, without any errors or data loss.

Actual behavior

When attempting to deserialize an ArraySegment<int> object using System.Text.Json.JsonSerializer, a NotSupportedException is thrown, indicating that deserialization is not supported for this type, despite it being serialized with the same serializer.

Regression?

No response

Known Workarounds

As a workaround, you can use Newtonsoft.Json.JsonConvert for both serialization and deserialization of ArraySegment<int>. JsonConvert handles this type without throwing exceptions, providing a seamless serialization/deserialization process.

Configuration

Other information

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    area-System.Text.JsonenhancementProduct code improvement that does NOT require public API changes/additions

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions