dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.96k stars 4.66k forks source link

Add support for parameterized constructors in System.Text.Json contract customization (converters) #71944

Open eiriktsarpalis opened 2 years ago

eiriktsarpalis commented 2 years ago

Taken from https://github.com/dotnet/runtime/issues/63686#issuecomment-1110990139:

The internal converters for collection and dictionary types do provide similar functionality, and we might consider exposing dedicated constructor delegates for each kind:

  • A Func<IList, object>>? CreateEnumerable delegate for JsonTypeInfoKind.Enumerable
  • A Func<IDictionary, object>>? CreateDictionary delegate for JsonTypeInfoKind.Dictionary.
  • A Func<object[], object>? CreateObjectWithParameters delegate for invoking parameterized constructors in JsonTypeInfoKind.Object.

Note that these would require exposing the JsonTypeInfo.ElementType and JsonTypeInfo.KeyType properties, respectively. The parameterized constructor delegate would require exposing the internal [JsonParameterInfo] https://github.com/dotnet/runtime/issues/78098(https://github.com/dotnet/runtime/blob/a49958b4b4f7a147b6cac91d510140863f7f57c5/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonParameterInfo.cs#L12) metadata type.

Related bugs that should be addressed in conjunction with this story:

ghost commented 2 years ago

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis See info in area-owners.md if you want to be subscribed.

Issue Details
Taken from https://github.com/dotnet/runtime/issues/63686#issuecomment-1110990139: > The [internal converters for collection and dictionary types do provide similar functionality](https://github.com/dotnet/runtime/blob/a49958b4b4f7a147b6cac91d510140863f7f57c5/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/ArrayConverter.cs#L26-L30), and we might consider exposing dedicated constructor delegates for each kind: > > * A `Func>? CreateEnumerable` delegate for `JsonTypeInfoKind.Enumerable` > * A `Func>? CreateDictionary` delegate for `JsonTypeInfoKind.Dictionary`. > * A `Func? CreateObjectWithParameters` delegate for invoking parameterized constructors in `JsonTypeInfoKind.Object`. > > Note that these would require exposing the [`JsonTypeInfo.ElementType`](https://github.com/dotnet/runtime/blob/a49958b4b4f7a147b6cac91d510140863f7f57c5/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs#L93) and [`JsonTypeInfo.KeyType`](https://github.com/dotnet/runtime/blob/a49958b4b4f7a147b6cac91d510140863f7f57c5/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs#L135) properties, respectively. The parameterized constructor delegate would require exposing the internal [`JsonParameterInfo`](https://github.com/dotnet/runtime/blob/a49958b4b4f7a147b6cac91d510140863f7f57c5/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonParameterInfo.cs#L12) metadata type.
Author: eiriktsarpalis
Assignees: -
Labels: `area-System.Text.Json`
Milestone: -