-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Description
Today all JsonSerializer methods accepting JsonSerializerOptions are marked as RequiresUnreferencedCode/RequiresDynamicCode on account of their behavior in a couple of cases:
- If no
JsonSerializerOptionsis specified, it will be defaulted toJsonSerializerOptions.Default. - If a
JsonSerializerOptionsis specified that doesn't explicitly set aTypeInfoResolver, then that field will be silently populated usingJsonSerializerOptions.Default.TypeInfoResolver.
The pervasiveness of the annotations in some of the most commonly used APIs in STJ is creating substantial usability issues for library and Native AOT application authors, who often have to come up with elaborate workarounds to evade warnings.
What appears to be true for both cases is that code paths accessing unreferenced/dynamic code are guarded by the JsonSerializer.IsRefectionEnabledByDefault feature switch:
- Here's the
JsonSerializerOptions.Defaultconstructor and - here is the
TypeInfoResolverpopulating behavior.
It seems to me that this protection should suffice to remove the annotation; the IsRefectionEnabledByDefault feature switch is turned off by default for every application setting PublishTrimmed to true. Granted, a trimmed/Native AOT application could explicitly turn on IsRefectionEnabledByDefault but this is usually a case of misconfiguration. In any case, this is the tactic that has been employed by ASP.NET core since .NET 8 and one that just got rolled out in Microsoft.Extensions.AI so it makes sense that this a tried approach that we could try moving down the stack.