Closed eiriktsarpalis closed 1 year ago
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis See info in area-owners.md if you want to be subscribed.
Author: | eiriktsarpalis |
---|---|
Assignees: | - |
Labels: | `api-suggestion`, `area-System.Text.Json` |
Milestone: | - |
Tagging subscribers to 'size-reduction': @eerhardt, @SamMonoRT, @marek-safar See info in area-owners.md if you want to be subscribed.
Author: | eiriktsarpalis |
---|---|
Assignees: | eiriktsarpalis |
Labels: | `area-System.Text.Json`, `api-ready-for-review`, `size-reduction` |
Milestone: | 8.0.0 |
Related to #77897
Love It! We do end up having quite a few types in these switches (last I checked it was well over a hundred), so this does sound like it should make a noticeable difference, happy to share numbers/deltas from our end once this is available in a preview package and we can try this out! 😄
Based on our expectation of there being a low number of callers, and the method having only been added in the most recent version, we've recommended changing the existing method to allow a null return.
public partial class JsonSerializerOptions
{
- public JsonTypeInfo GetTypeInfo(Type type);
+ public JsonTypeInfo? GetTypeInfo(Type type);
}
FYI @davidfowl @eerhardt ☝️
This will introduce nullability warnings in the aspnetcore components that do call into GetTypeInfo
.
After experimentation, we have concluded that making the existing GetTypeInfo
nullable is not desirable -- it is often the case that users need an accelerator method that guarantees a result or fails with a standardized error message. I've notified FXDC that we wish to go back to the original proposal:
namespace System.Text.Json;
public partial class JsonSerializerOptions
{
public JsonTypeInfo GetTypeInfo(Type type);
+ public bool TryGetTypeInfo(Type type, [NotNullWhen(true)] out JsonTypeInfo? typeInfo);
}
Background and motivation
The STJ source generator's implementation of the
JsonSerializerContext.GetTypeInfo
andIJsonTypeInfoResolver.GetTypeInfo
methods are currently duplicated:The size of these methods increases with the number of generated types, so in the interest of size reduction we would at least want to eliminate the duplication. The first
GetTypeInfo
method could be implemented by replacing it withThere is a however a snag, since
JsonSerializerContext.GetTypeInfo
needs to returnnull
for unsupported types, whereasJsonSerializerOptions.GetTypeInfo
is non-nullable and returnsNotSupportedException
for unsupported types.API Proposal
API Usage
The source generator could then implement the first
GetTypeInfo
as follows:Testing against our own source gen test projects, the change contributes to ~2.2% size reduction to generated source files and just over 1.2% to the test assemblies (which includes test code beyond just the source generated contexts). A minor yet measurable improvement.
Alternative Designs
Do not ship the a new API and instead source generate
Make the existing
GetTypeInfo
method nullable:It would return
null
where the method currently throws, so technically this is not a breaking change but it would trigger nullability warnings in places where it previous didn't apply.Risks
No response