Add JsonSerializerOptions.MakeReadOnly(bool) overload.#90013
Add JsonSerializerOptions.MakeReadOnly(bool) overload.#90013eiriktsarpalis merged 1 commit intodotnet:mainfrom
Conversation
|
Note regarding the This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change. |
|
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis |
...ries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Converters.cs
Show resolved
Hide resolved
...em.Text.Json/tests/System.Text.Json.Tests/TrimmingTests/IsReflectionEnabledByDefaultFalse.cs
Show resolved
Hide resolved
...ries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Converters.cs
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.cs
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.cs
Show resolved
Hide resolved
| { | ||
| if (!_isConfiguredForJsonSerializer) | ||
| { | ||
| ConfigureForJsonSerializer(); |
There was a problem hiding this comment.
ConfigureForJsonSerializer() will throw an exception when JsonSerializer.IsReflectionEnabledByDefault == false && TypeInfoResolver == null saying:
Reflection-based serialization has been disabled for this application. Either use the source generator APIs or explicitly configure the 'JsonSerializerOptions.TypeInfoResolver' property.
I don't think ASP.NET Core will be able to use this new API because of this. There are scenarios where we need to call MakeReadOnly() on a JsonSerializerOptions that doesn't have its TypeInfoResolver set yet. One example is in MVC:
MVC always creates a SystemTextJsonOutputFormatter up front without knowing if the app is actually going to use JSON serialization or not. When the app doesn't touch JSON, we shouldn't be throwing an exception at startup.
There was a problem hiding this comment.
It looks like aspnetcore was able to work around #89830 since it owns its JsonSerializerOptions instances, so we don't need new APIs to unblock that case.
The semantics of the new overload is oriented towards components accepting JsonSerializerOptions instances they don't own but still need to emulate the behavior of the JsonSerializer methods.
Fix #89934.
cc @halter73 @eerhardt