Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Tracked by #107787
isukces opened this issue Sep 13, 2024 · 3 comments
Labels
area-System.Text.Json enhancement Product code improvement that does NOT require public API changes/additions
Milestone

Comments

@isukces
Copy link

isukces commented Sep 13, 2024

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

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Sep 13, 2024
Copy link
Contributor

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

@huoyaoyuan
Copy link
Member

It wasn't supported for deserialization. The criteria for serializing and deserializing are different. Serializing requires read access only while deserializing requires way to construct the type.
For ArraySegment<T>, it's serializable because it implements IList<T>, but it isn't deserializable because it doesn't implement the common shape for constructable collections. It's not supported by other features like collection expressions in C# either.
Make it deserializable is a feature request that requires explicit recognition of this type.

@eiriktsarpalis
Copy link
Member

The type doesn't implement one of the standard construction patterns (default constructor with Add method, constructor accepting IEnumerable<T> or CollectionBuilderAttribute annotation). There's a proposal that we add CollectionBuilderAttribute support to STJ tracked by #82642, at which point adding CollectionBuilderAttribute to ArraySegment` would automatically give support to the type.

@eiriktsarpalis eiriktsarpalis added enhancement Product code improvement that does NOT require public API changes/additions and removed untriaged New issue has not been triaged by the area owner labels Sep 13, 2024
@eiriktsarpalis eiriktsarpalis added this to the Future milestone Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Text.Json enhancement Product code improvement that does NOT require public API changes/additions
Projects
None yet
Development

No branches or pull requests

3 participants