-
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
base: main
Are you sure you want to change the base?
Changes from 16 commits
506bb05
80a7bb6
419b232
ad9f947
34ec615
4f80f13
5bb7f66
195a155
c5ab159
6221944
587f501
2c17fa1
bdf9655
9f0f3fe
c3f4662
91a1ab0
c8dc1d3
5a54b07
fee078f
f41d710
ee3a3f8
6f4714b
deee409
9d4b385
78226c1
3052f6b
361f77e
ca4534d
d00b1e9
309ee17
3086bfc
bee1dd5
ae61d9d
b6e4d51
4fa876b
4d927bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // 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> | ||
| { | ||
| // TODO: Set to false because I think IReadOnlySet<T> is always read-only. | ||
| // However, because we use HashSet<T> as the concrete type, we can still add items to it. | ||
| // So I'm unsure if this should be true or false. | ||
sander1095 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| internal override bool CanPopulate => false; | ||
|
|
||
| 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) | ||
| { | ||
| 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>(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
Check failure on line 45 in src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/IReadOnlySetOfTConverter.cs
|
||
Uh oh!
There was an error while loading. Please reload this page.