C
C#7d 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;
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;
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;
How to use source generation in System.Text.Json - .NET
Learn how to use source generation in System.Text.Json.
1 Reply
Core
CoreOP7d ago
Thank you for trying it out. then it just refuses to work for me:sadge: It is an API project, but it's configured to use the serializer. I'm sure it works, because incoming string is mapped to its corresponding enum value Yeah I just used the good old JsonConverter, this is probably the 2-3rd feature that does not work well with Minimal APIs

Did you find this page helpful?