Skip to content

[API Proposal]: Add IReadOnlySet<T> support to System.Text.Json #121314

@sander1095

Description

@sander1095

EDIT @eiriktsarpalis: this is a fairly straightforward API proposal necessary for including IReadOnlySet serialization support in the source generator.

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>:

#86442 (comment)

namespace System.Text.Json.Serialization.Metadata;

public partial class JsonMetadataServices
{
        public static System.Text.Json.Serialization.Metadata.JsonTypeInfo<TCollection> CreateISetInfo<TCollection, TElement>(System.Text.Json.JsonSerializerOptions options, System.Text.Json.Serialization.Metadata.JsonCollectionInfoValues<TCollection> collectionInfo) where TCollection : System.Collections.Generic.ISet<TElement> { throw null; }
+#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
}

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

Metadata

Metadata

Labels

api-approvedAPI was approved in API review, it can be implementedapi-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.Text.Json

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions