Add IReadOnlySet support to System.Text.Json#120306
Add IReadOnlySet support to System.Text.Json#120306eiriktsarpalis merged 45 commits intodotnet:mainfrom
Conversation
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.
There was a problem hiding this comment.
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 toHashSet<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 |
...xt.Json/src/System/Text/Json/Serialization/Converters/Collection/IReadOnlySetOfTConverter.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/tests/Common/TestClasses/TestClasses.cs
Outdated
Show resolved
Hide resolved
...Json/src/System/Text/Json/Serialization/Converters/Collection/IEnumerableConverterFactory.cs
Outdated
Show resolved
Hide resolved
...xt.Json/src/System/Text/Json/Serialization/Converters/Collection/IReadOnlySetOfTConverter.cs
Outdated
Show resolved
Hide resolved
eiriktsarpalis
left a comment
There was a problem hiding this comment.
This would also require updates to the source generator so IReadOnlySet is recognized and relevant code gets generated.
@eiriktsarpalis |
|
Sure, you can get started by looking at how runtime/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs Lines 844 to 848 in ca29ebb |
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.
…gh my editor says it is required
|
@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! |
src/libraries/System.Text.Json/tests/Common/JsonCreationHandlingTests.Generic.cs
Outdated
Show resolved
Hide resolved
Thanks to @huoyaoyuan for their generous help!
...son/tests/System.Text.Json.SourceGeneration.Tests/Serialization/JsonCreationHandlingTests.cs
Outdated
Show resolved
Hide resolved
|
@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.
|
@sander1095 the test failures suggest that you did not add the new test types to one of the JsonSerializerContexts used to drive the tests. |
|
@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! |
eiriktsarpalis
left a comment
There was a problem hiding this comment.
Thank you for this excellent contribution!
src/libraries/System.Text.Json/tests/Common/CollectionTests/CollectionTests.Generic.Write.cs
Outdated
Show resolved
Hide resolved
…llectionTests.Generic.Write.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This pull request adds support for serializing and deserializing
IReadOnlySet<T>collections in theSystem.Text.Jsonlibrary. The changes include updates to the codebase to recognizeIReadOnlySet<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 ofIReadOnlySet<T>with other collection interfaces likeISet<T>andICollection<T>.Minor fixes:
ReturnsJsonConverterFactortytoReturnsJsonConverterFactory.Fixes #91875