Skip to content

Add IReadOnlySet support to System.Text.Json#120306

Merged
eiriktsarpalis merged 45 commits intodotnet:mainfrom
sander1095:add-ireadonlyset-support-to-system_text_json
Jan 21, 2026
Merged

Add IReadOnlySet support to System.Text.Json#120306
eiriktsarpalis merged 45 commits intodotnet:mainfrom
sander1095:add-ireadonlyset-support-to-system_text_json

Conversation

@sander1095
Copy link
Contributor

@sander1095 sander1095 commented Oct 1, 2025

This pull request adds support for serializing and deserializing IReadOnlySet<T> collections in the System.Text.Json library. The changes include updates to the codebase to recognize IReadOnlySet<T> as a collection type, introduce a dedicated converter for it, and provide comprehensive tests to ensure correct behavior. This enhancement aligns the library's handling of IReadOnlySet<T> with other collection interfaces like ISet<T> and ICollection<T>.

Minor fixes:

  • Corrected a typo in an exception helper method name from ReturnsJsonConverterFactorty to ReturnsJsonConverterFactory.

Fixes #91875

This is based on existing test cases for IReadOnlyList and ISet
I'm pretty sure this is not the right direction, but it helps me with being able to run tests against an initial working implementation I'll work on next.

I'll discuss the current #IF !NETFRAMEWORK code setup with the team when the PR is ready for a first review
This seems useful for code that deserializes to a type that deserializes to a Set type that implements both interfaces.
Copilot AI review requested due to automatic review settings October 1, 2025 20:35
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Oct 1, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for IReadOnlySet<T> serialization/deserialization to System.Text.Json. The implementation creates a new converter that uses HashSet<T> as the concrete type for IReadOnlySet<T> interface deserialization.

  • Adds IReadOnlySetOfTConverter<TCollection, TElement> converter that deserializes JSON arrays to HashSet<T> instances
  • Integrates the new converter into the converter factory with appropriate conditional compilation
  • Adds comprehensive test coverage for both generic and object-typed IReadOnlySet<T> scenarios

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
IReadOnlySetOfTConverter.cs New converter implementation for IReadOnlySet<T> using HashSet<T> as backing type
IEnumerableConverterFactory.cs Integrates IReadOnlySet<T> converter into the factory with conditional compilation
System.Text.Json.csproj Adds the new converter file to the project
ThrowHelper.Serialization.cs Fixes typo in method name from "Factorty" to "Factory"
JsonConverterFactory.cs Updates method call to use corrected spelling
Array.ReadTests.cs Adds test methods for IReadOnlySet<T> deserialization
ReferenceHandlerTests.cs Adds JsonSerializable attributes for IReadOnlySet<T> test classes
TestData.cs Includes IReadOnlySet<T> test cases in success test data
TestClasses.cs Adds test class definitions and minor formatting change to dictionary initialization

@sander1095 sander1095 marked this pull request as draft October 1, 2025 20:37
Copy link
Member

@eiriktsarpalis eiriktsarpalis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would also require updates to the source generator so IReadOnlySet is recognized and relevant code gets generated.

@sander1095
Copy link
Contributor Author

This would also require updates to the source generator so IReadOnlySet is recognized and relevant code gets generated.

@eiriktsarpalis
Any pointers to where I need to get started on this? A nudge to a file would be enough, I can check existing code patterns and call sites to hopefully figure the rest out.

@eiriktsarpalis
Copy link
Member

Sure, you can get started by looking at how ISet<T> is being handled today, presumably IReadOnlySet<T> would get handled identically:

else if ((actualTypeToConvert = type.GetCompatibleGenericBaseType(_knownSymbols.ISetOfTType)) != null)
{
collectionType = CollectionType.ISet;
valueType = actualTypeToConvert.TypeArguments[0];
}

Also exclude the entirety of IReadOnlySetOfTConverter by moving the #IF up.
…ck to csproj, which is what @huoyaoyuan (most likely) meant in his PR feedback.

As far as I can see, there isn't a modern .NET identifier. Therefore, a check for .NETCoreApp is not enough, because that also includes < NET 5.0.

Because of this, I also added a check that we are on .NET 5 or higher. Only then will the file be compiled.
@sander1095
Copy link
Contributor Author

@eiriktsarpalis I've made changes to the source generators. Can you provide a check to see if this was done to your satisfaction? Also: If the unit tests pass, can I then assume the source generator implementation is succesful? I am not really sure how to test source generators this in the repo itself, so any tips on that would be great (if succesful unit tests aren't enough).

Thanks for your time!

@sander1095
Copy link
Contributor Author

@eiriktsarpalis Please provide feedback on this PR when you (or a teammate) can

…nded).

They dont work because of missing population support, and that's because readonly collection dont have population support. This behavior is expected.
@eiriktsarpalis
Copy link
Member

@sander1095 the test failures suggest that you did not add the new test types to one of the JsonSerializerContexts used to drive the tests.

@sander1095
Copy link
Contributor Author

@eiriktsarpalis Thanks for that hint!

I ran all the tests locally, and they all succeed now! Now I'm hoping for a succesful pipeline run!

This PR is fully ready for review by the team! I'm curious for the feedback, and excited to see this merged for .NET 11!

Copy link
Member

@eiriktsarpalis eiriktsarpalis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this excellent contribution!

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 25 out of 25 changed files in this pull request and generated 1 comment.

@eiriktsarpalis eiriktsarpalis merged commit b1d2e4a into dotnet:main Jan 21, 2026
75 of 89 checks passed
@sander1095 sander1095 deleted the add-ireadonlyset-support-to-system_text_json branch January 21, 2026 17:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Text.Json community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

System.Text.Json cannot deserialize to IReadOnlySet

4 participants