-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Description
Most other converters in S.T.J. (as far as I know) are safe because their default constructors are referenced statically. However, if you use JsonStringEnumConverter like this:
[JsonConverter(typeof(JsonStringEnumConverter))]
public enum MyEnum { A, B, C }then there is no static reference to JsonStringEnumConverter's default constructor, so the linker will strip it out. Then at runtime it will fail when JsonSerializerOptions.GetConverterFromAttribute tries to call Activator.CreateInstance(converterType) (where converterType = typeof(JsonStringEnumConverter)). This was originally reported at dotnet/aspnetcore#19086
For Blazor 3.2.0, we'll put in a workaround into Blazor's built-in linker config. However it would make sense to fix the underlying issue in S.T.J. in the longer term.
Suggested fix
Either System.Text.Json.dll should embed its own linker config file preserving the constructors for all converter types, or consider extending the set of types listed in JsonSerializerOptions.DefaultSimpleConverters to include an instantiation of JsonStringEnumConverter (or have any other static reference to its constructor).
This applies to all converter types that may be used at runtime. The only one I know of that's causing a problem is JsonStringEnumConverter, but I didn't exhaustively check that all the others are linker-safe too.