C
C#•7mo ago
Tobonautilus

The combination of `StringEnumConverter` and `EnumMember` does not work

I try to have "speaking values" for my enum values in a JSON file. I tried combining the both annotations from the title, but it does not work and I cannot understand why. Maybe you know something I missed out :). Enum
[JsonConverter(typeof(StringEnumConverter))]
public enum Language
{
[EnumMember(Value = "en")]
English,
[EnumMember(Value = "de")]
German,
[EnumMember(Value = "ende")]
EnglishAndGerman
}
[JsonConverter(typeof(StringEnumConverter))]
public enum Language
{
[EnumMember(Value = "en")]
English,
[EnumMember(Value = "de")]
German,
[EnumMember(Value = "ende")]
EnglishAndGerman
}
Model (shortend)
public partial class ContentCreation
{
[JsonProperty("name")]
public required string Name { get; set; }

[JsonProperty("language")]
public required Language Language { get; set; }
}
public partial class ContentCreation
{
[JsonProperty("name")]
public required string Name { get; set; }

[JsonProperty("language")]
public required Language Language { get; set; }
}
Converter call
return JsonConvert.SerializeObject(myContentCreation);
return JsonConvert.SerializeObject(myContentCreation);
15 Replies
Unknown User
Unknown User•7mo ago
Message Not Public
Sign In & Join Server To View
reflectronic
reflectronic•7mo ago
yeah, the converter does not respect the EnumMember attribute if you google, you can find a custom converter which respects it, which you can paste into your project and use instead of StringEnumConverter https://github.com/dotnet/runtime/issues/74385#issuecomment-1705083109
Tobonautilus
TobonautilusOP•7mo ago
@TeBeClone yep, you're right. I need to have a "readable" json file, but I want type-safety in my C# code. @reflectronic thanks for the tip!
Unknown User
Unknown User•7mo ago
Message Not Public
Sign In & Join Server To View
Tobonautilus
TobonautilusOP•7mo ago
I guess you're right from where I come aka Swift, there's a simple build-in string representation for enums, I think I have to learn more than just the code 😄
Unknown User
Unknown User•7mo ago
Message Not Public
Sign In & Join Server To View
Tobonautilus
TobonautilusOP•7mo ago
yep
Unknown User
Unknown User•7mo ago
Message Not Public
Sign In & Join Server To View
Tobonautilus
TobonautilusOP•7mo ago
At least I'm not the onloy one who fall into this "trap": https://github.com/dotnet/runtime/issues/74385#issuecomment-1727384007
GitHub
Support customizing enum member names in System.Text.Json · Issue #...
Issue 31081 was closed in favor of issue 29975, however the scope of the issues differ. Issues 29975 is a discussion regarding the DataContract and DataMember attributes in general. Although JsonSt...
Unknown User
Unknown User•7mo ago
Message Not Public
Sign In & Join Server To View
Tobonautilus
TobonautilusOP•7mo ago
As a swift developer, enums are mighty AF with payloads, type-safety, etc. The C# word is a bit basic 😄
Unknown User
Unknown User•7mo ago
Message Not Public
Sign In & Join Server To View
Tobonautilus
TobonautilusOP•7mo ago
w00t?!
Unknown User
Unknown User•7mo ago
Message Not Public
Sign In & Join Server To View
Tobonautilus
TobonautilusOP•7mo ago
thanks for the insights!
Want results from more Discord servers?
Add your server