C#C
C#12mo ago
Core

✅ Serialized enum starts with uppercase / camel case is ignored

Hello,
I am using the newer source generated enum serializer (introduced in .NET 8), but for some reason the serialized string starts with uppercase instead of lowercase. https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/source-generation#source-generation-support-in-aspnet-core


c#
[JsonSourceGenerationOptions(UseStringEnumConverter = true, PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
[JsonSerializable(typeof(RedirectType))]
public partial class EnumToStringJsonContext : JsonSerializerContext;


I even tried another configuration, but it had the same outcome

c#
public class CamelCaseEnumConverter<TEnum>() : 
    JsonStringEnumConverter<TEnum>(JsonNamingPolicy.CamelCase) where TEnum : struct, Enum;

[JsonSourceGenerationOptions(
    UseStringEnumConverter = true,
     Converters =
         [
             typeof(CamelCaseEnumConverter<RedirectType>)
         ]),
 JsonSerializable(typeof(RedirectType))
]
public partial class EnumToStringJsonContext : JsonSerializerContext;
Learn how to use source generation in System.Text.Json.
Was this page helpful?