-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
The JsonSerializerOptions.TypeInfoResolver
property should be nullable
#71960
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsThe current iteration of runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.cs Lines 168 to 181 in 52d9d0c
is implemented as non-nullable and marked as RequiredUnreferencedCode since by default it will return the reflection-based contract resolver. This has been informed by the existing semantics of the JsonSerializer methods accepting JsonSerializerOptions as an argument: unless a contract resolver is explicitly set the method will revert to using reflection-based serialization.
I believe however that this approach is problematic, for a number of reasons:
I propose we make the following changes to the API: public partial class JsonSerializerOptions
{
- [RequiresUnreferenceCode]
- [RequiresDynamicCode]
- public IJsonTypeInfoResolver TypeInfoResolver { get; set; }
+ public IJsonTypeInfoResolver? TypeInfoResolver { get; set; }
+ [RequiresUnreferenceCode]
+ [RequiresDynamicCode]
public static JsonSerializerOptions Default { get; } // default instance comes populated with reflection resolver
}
|
IMO following behavior which is proposed here feels like designing APIs around linker. In both cases // proposed:
options.TypeInfoResolver = null;
// options.TypeInfoResolver == null
JsonSerializer(..., options);
// options.TypeInfoResolver.GetType() == typeof(DefaultJsonTypeInfoResolver) current behavior: // current behavior:
options.TypeInfoResolver = null;
// options.TypeInfoResolver.GetType() == typeof(DefaultJsonTypeInfoResolver)
JsonSerializer(..., options);
// options.TypeInfoResolver.GetType() == typeof(DefaultJsonTypeInfoResolver) it just feels more consistent the way it is right now |
cc @jkotas @eerhardt who might have opinions on the matter. I don't think "designing APIs around the linker" is the right way to look at this. It is about designing a type that works for scenaria beyond the narrow scope of reflection-based serialization. If we do ship For the record, I don't believe reflection-based |
@eiriktsarpalis 's proposal makes sense to me. |
The current iteration of
JsonSerializerOptions.TypeInfoResolver
runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.cs
Lines 168 to 181 in 52d9d0c
is implemented as non-nullable and marked as
RequiredUnreferencedCode
since by default it will return the reflection-based contract resolver. This has been informed by the existing semantics of theJsonSerializer
methods acceptingJsonSerializerOptions
as an argument: unless a contract resolver is explicitly set the method will revert to using reflection-based serialization.I believe however that this approach is problematic, for a number of reasons:
JsonSerializerOptions
. While we cannot change the semantics of existingJsonSerializer
methods it is conceivable that future serialization APIs can make use of it in a linker-safe manner. Assuming we ship theTypeInfoResolver
property in the current manifestation, we forever coupleJsonSerializerOptions
to the reflection-based serialization and trimmability concerns.I propose we make the following changes to the API:
The text was updated successfully, but these errors were encountered: