System.Text.Json – Disable reflection-based serialization
Starting with .NET 8 you can disable the default reflection-based serializer that comes with System.Text.Json
. This can be useful in trimmed and native AOT applications where you don’t want to include the reflection components in your build.
You can enable this feature by setting the JsonSerializerIsReflectionEnabledByDefault
property to false
in your .csproj
file.
<JsonSerializerIsReflectionEnabledByDefault>false</JsonSerializerIsReflectionEnabledByDefault>
Code language: HTML, XML (xml)
As a side-effect, you will be required to provide a JsonSerializerOptions
during serialization and deserialization. Failing to do so will result in a NotSupportedException
at runtime.
Along with this option, a new IsReflectionEnabledByDefault
property is introduced on the JsonSerializer
, allowing developers to do a runtime check to see whether the feature is on or off.
2 Comments