Start Debugging
2023-09-17 更新日 2023-11-05 csharpdotnetdotnet-8 Edit on GitHub

.NET 8 native AOT で JsonStringEnumConverter を使う方法

.NET 8 で追加された JsonStringEnumConverter<TEnum> を使って、System.Text.Json で native AOT 対応の enum シリアライズを行う方法を解説します。

JsonStringEnumConverter は native AOT に対応していません。これを解決するため、.NET 8 では native AOT に対応した新しいコンバーター型 JsonStringEnumConverter が導入されました。

新しい型を使うには、次のように型に注釈を付けるだけです。

[JsonConverter(typeof(JsonStringEnumConverter<MyEnum>))]
public enum MyEnum { Foo, Bar }

[JsonSerializable(typeof(MyEnum))]
public partial class MyJsonSerializerContext : JsonSerializerContext { }

注意点として、enum のデシリアライズは大文字・小文字を区別しません。一方、シリアライズは JsonNamingPolicy でカスタマイズできます。

JsonStringEnumConverter と NativeAOT を組み合わせるとどうなるか?

最初の警告サインはコンパイル時に表示されます。次のような警告が出ます。

Using member ‘System.Text.Json.Serialization.JsonStringEnumConverter.JsonStringEnumConverter()’ which has ‘RequiresDynamicCodeAttribute’ can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.

そして、コンパイル済みのコードを実行すると、次のランタイム例外が発生します。

System.Reflection.MissingMetadataException: ‘System.Text.Json.Serialization.Converters.EnumConverter’ is missing metadata.

Comments

Sign in with GitHub to comment. Reactions and replies thread back to the comments repo.

< 戻る