-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Description
Background and motivation
In #91875 , it's discussed that System.Text.Json doesn't support IReadOnlySet<T>, which was added in .NET 5.0. The team welcomes support, which I am working on, now.
Supporting IReadOnlySet<T> increases the amount of BCL types that System.Text.Json supports. IReadOnlySet being a pretty important interface, part of the System.Collection.Generics namespace, makes me believe that this support is quite important.
Based on the discussions in #91875, I have already started work on the PR: #120306 . There, the helpful @huoyaoyuan told me that this "formal" API proposal is needed, and @eiriktsarpalis would need to have a look at it.
API Proposal
Based on the design proposal for adding support for Memory<T>:
namespace System.Text.Json.Serialization.Metadata;
public partial class JsonMetadataServices
{
#if NET
public static System.Text.Json.Serialization.Metadata.JsonTypeInfo<TCollection> CreateIReadOnlySetInfo<TCollection, TElement>(System.Text.Json.JsonSerializerOptions options, System.Text.Json.Serialization.Metadata.JsonCollectionInfoValues<TCollection> collectionInfo) where TCollection : System.Collections.Generic.IReadOnlySet<TElement> { throw null; }
#endif
#if NET
/// <summary>
/// Creates serialization metadata for types assignable to <see cref="IReadOnlySet{T}"/>.
/// </summary>
/// <typeparam name="TCollection">The generic definition of the type.</typeparam>
/// <typeparam name="TElement">The generic definition of the element type.</typeparam>
/// <param name="options"></param>
/// <param name="collectionInfo">Provides serialization metadata about the collection type.</param>
/// <returns>Serialization metadata for the given type.</returns>
/// <remarks>This API is for use by the output of the System.Text.Json source generator and should not be called directly.</remarks>
public static JsonTypeInfo<TCollection> CreateIReadOnlySetInfo<TCollection, TElement>(
JsonSerializerOptions options,
JsonCollectionInfoValues<TCollection> collectionInfo)
where TCollection : IReadOnlySet<TElement>
=> CreateCore(
options,
collectionInfo,
new IReadOnlySetOfTConverter<TCollection, TElement>());
#endif
}API Usage
The average developer won't notice this, as IEnumerableConverterFactory, an internal class, will add support for IReadOnlySetConverter (another internal class).
I believe most of the API changes will be used by source generators(?)
Alternative Designs
None
Risks
None