-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Add IReadOnlySet support to System.Text.Json #120306
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
Open
sander1095
wants to merge
36
commits into
dotnet:main
Choose a base branch
from
sander1095:add-ireadonlyset-support-to-system_text_json
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 28 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
506bb05
Start adding testcases for IReadOnlySet
sander1095 80a7bb6
Add .NET Framework exclusions for tests around IReadOnlySet<T>.
sander1095 419b232
Add another test
sander1095 ad9f947
Fix typo (Factorty -> Factory)
sander1095 34ec615
Add another #IF to a test case
sander1095 4f80f13
Start working on readonlyset converter
sander1095 5bb7f66
Move IReadOnlySet above ISet so ISet has priority.
sander1095 195a155
Change #IF pre-processors to PR suggested variant
sander1095 c5ab159
Move the #IF preprocessor of IReadOnylSetOfTConverter compilation che…
sander1095 6221944
Start adding support for IReadOnlySetOfT for Source Generators
sander1095 587f501
Add missing newline
sander1095 2c17fa1
Remove missing newline as I do not see it on GitHub either, even thou…
sander1095 bdf9655
Add more code for source generator support
sander1095 9f0f3fe
Add many more tests based on patterns of existing ISet tests
sander1095 c3f4662
Fix part of the build issues
sander1095 91a1ab0
Fix test
sander1095 c8dc1d3
Add whitespace
sander1095 5a54b07
Based on the fact that IReadOnlyList does not support population (aft…
sander1095 fee078f
Delete unit tests that check for CanPopulate support.
f41d710
Remove more tests that fail due to unsupported casts.
ee3a3f8
Remove code that should have been removed in previous commits
6f4714b
Remove another test case because this list isn't filled with read onl…
deee409
Remove CanPopulate false because this is the default
9d4b385
Add _isDeserializable to IReadOnlySetOfTConverter for better error me…
78226c1
Delete more test cases that I believe should fail.
3052f6b
Add another test case for IReadOnlySet<T>
361f77e
Add another test case for IReadOnlySet<T>
ca4534d
Remove unintended formatting changes
d00b1e9
Switch test class to implementing IReadOnlySet<T> instead of ISet<T>
309ee17
Add back ISet test case
3086bfc
Fix .NET 5 reference in comment
bee1dd5
Remove .NET 5 requirement thanks to PR feedback
sander1095 ae61d9d
Remove ifdef comment about .NET >= 5 support due to PR feedback
sander1095 b6e4d51
Remove another comment that I hadnt saved
sander1095 4fa876b
Remove unnecessary #ifdefs for source generators
sander1095 4d927bf
Add extra test for IReadOnlySet
sander1095 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...Json/src/System/Text/Json/Serialization/Converters/Collection/IReadOnlySetOfTConverter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Text.Json.Serialization.Metadata; | ||
|
|
||
| namespace System.Text.Json.Serialization.Converters | ||
| { | ||
| internal sealed class IReadOnlySetOfTConverter<TCollection, TElement> | ||
| : IEnumerableDefaultConverter<TCollection, TElement> | ||
| where TCollection : IReadOnlySet<TElement> | ||
| { | ||
| private readonly bool _isDeserializable = typeof(TCollection).IsAssignableFrom(typeof(HashSet<TElement>)); | ||
|
|
||
| protected override void Add(in TElement value, ref ReadStack state) | ||
| { | ||
| // Directly convert to HashSet<TElement> since IReadOnlySet<T> does not have an Add method. | ||
| HashSet<TElement> collection = (HashSet<TElement>)state.Current.ReturnValue!; | ||
| collection.Add(value); | ||
| if (IsValueType) | ||
| { | ||
| state.Current.ReturnValue = collection; | ||
| } | ||
| } | ||
|
|
||
| protected override void CreateCollection(ref Utf8JsonReader reader, scoped ref ReadStack state, JsonSerializerOptions options) | ||
| { | ||
| if (!_isDeserializable) | ||
| { | ||
| ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(Type, ref reader, ref state); | ||
| } | ||
|
|
||
| state.Current.ReturnValue = new HashSet<TElement>(); | ||
| } | ||
|
|
||
| internal override void ConfigureJsonTypeInfo(JsonTypeInfo jsonTypeInfo, JsonSerializerOptions options) | ||
| { | ||
| // Deserialize as HashSet<TElement> for interface types that support it. | ||
| if (jsonTypeInfo.CreateObject is null && Type.IsAssignableFrom(typeof(HashSet<TElement>))) | ||
| { | ||
| Debug.Assert(Type.IsInterface); | ||
| jsonTypeInfo.CreateObject = () => new HashSet<TElement>(); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.