Setting the JsonSerializerIsReflectionEnabledByDefault property to false currently results in a runtime exception when reflection-based json serialized is encountered. As discussed in https://github.com/dotnet/roslyn-analyzers/issues/7283, it would be nice if the analyzers for System.Text.Json could provide a compile-time warning for code that uses reflection when reflection is disabled through the property.
using System.Text.Json;
JsonSerializer.Serialize(42);
Expected behavior
Compiler warning or error for using reflection-based json serialization when JsonSerializerIsReflectionEnabledByDefault is set to false.
Actual behavior
No warning or error at compile-time. The following exception is thrown at runtime:
Unhandled exception. System.InvalidOperationException: Reflection-based serialization has been disabled for this application. Either use the source generator APIs or explicitly configure the 'JsonSerializerOptions.TypeInfoResolver' property.
at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_JsonSerializerIsReflectionDisabled()
at System.Text.Json.JsonSerializerOptions.ConfigureForJsonSerializer()
at System.Text.Json.JsonSerializer.GetTypeInfo(JsonSerializerOptions options, Type inputType)
at System.Text.Json.JsonSerializer.GetTypeInfo[T](JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Serialize[TValue](TValue value, JsonSerializerOptions options)
at Program.<Main>$(String[] args) in /home/sweemer/JsonTest/Program.cs:line 2
Regression?
No response
Known Workarounds
No response
Configuration
$ dotnet --info
.NET SDK:
Version: 8.0.401
Commit: 811edcc344
Workload version: 8.0.400-manifests.b6724b7a
MSBuild version: 17.11.4+37eb419ad
Runtime Environment:
OS Name: ubuntu
OS Version: 22.04
OS Platform: Linux
RID: linux-x64
Base Path: /usr/share/dotnet/sdk/8.0.401/
.NET workloads installed:
Configured to use loose manifests when installing new manifests.
There are no installed workloads to display.
Host:
Version: 8.0.8
Architecture: x64
Commit: 08338fcaa5
.NET SDKs installed:
8.0.401 [/usr/share/dotnet/sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 8.0.8 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 8.0.8 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
Other architectures found:
None
Environment variables:
Not set
global.json file:
Not found
Description
Setting the
JsonSerializerIsReflectionEnabledByDefault
property tofalse
currently results in a runtime exception when reflection-based json serialized is encountered. As discussed in https://github.com/dotnet/roslyn-analyzers/issues/7283, it would be nice if the analyzers forSystem.Text.Json
could provide a compile-time warning for code that uses reflection when reflection is disabled through the property.Reproduction Steps
Expected behavior
Compiler warning or error for using reflection-based json serialization when
JsonSerializerIsReflectionEnabledByDefault
is set tofalse
.Actual behavior
No warning or error at compile-time. The following exception is thrown at runtime:
Regression?
No response
Known Workarounds
No response
Configuration
Other information
No response