A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/dotnet/runtime/issues/81833 below:

STJ source generator does not correctly wrap custom converters on nullable properties · Issue #81833 · dotnet/runtime · GitHub

Consider the following code:

var value = new MyPoco { Value = MyEnum.A };

Console.WriteLine(JsonSerializer.Serialize(value)); // works as expected
Console.WriteLine(JsonSerializer.Serialize(value, MyContext.Default.MyPoco));
// System.ArgumentException: GenericArguments[0], 'System.Nullable`1[MyEnum]', on 'System.Text.Json.Serialization.Converters.EnumConverter`1[T]' violates the constraint of type 'T'.
//   at System.RuntimeTypeHandle.Instantiate(RuntimeType inst)
//   at System.RuntimeType.MakeGenericType(Type[] instantiation)
//   -- - End of inner exception stack trace ---
//   at System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e)
//   at System.RuntimeType.MakeGenericType(Type[] instantiation)
//   at System.Text.Json.Serialization.Converters.EnumConverterFactory.Create(Type enumType, EnumConverterOptions converterOptions, JsonNamingPolicy namingPolicy, JsonSerializerOptions options)
//   at MyContext.GetConverterFromFactory(JsonSerializerOptions options, Type type, JsonConverterFactory factory) in C: \Users\eitsarpa\source\repos\ConsoleApp1\ConsoleApp1\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\MyContext.g.cs:line 77
//   at MyContext.GetConverterFromFactory[T] (JsonSerializerOptions options, JsonConverterFactory factory) in C: \Users\eitsarpa\source\repos\ConsoleApp1\ConsoleApp1\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\MyContext.g.cs:line 72

public class MyPoco
{
    [JsonConverter(typeof(JsonStringEnumConverter))]
    public MyEnum? Value { get; set; }
}

public enum MyEnum { A, B, C };

[JsonSerializable(typeof(MyPoco))]
public partial class MyContext : JsonSerializerContext
{ }

By convention, the reflection serializer automatically wraps custom property converters on nullable types. We should update the source generator to do the same.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4