How to Specify Custom Names for Enum Values in System.Text.Json?
Jan 14, 2025 am 10:37 AMSystem.Text.Json: How do I specify a custom name for an enum value?
This capability is not available out of the box in .NET Core 3.0, .NET 5, .NET 6.0, .NET 7.0, or .NET 8.0. Therefore, you will need to create your own JsonConverterFactory that serializes enums with custom value names specified by attributes, or use a NuGet package that does the same, such as Macross.Json.Extensions.
If you are working in .NET 7 or later, or only need to serialize but not deserialize enums with custom names in earlier versions, custom names can be easily supported by creating a JsonConverterFactory that adapts JsonStringEnumConverter by constructing a customized JsonNamingPolicy for each enum with [EnumMember(Value = "xxx")] applied to any enum value.
Here are the steps involved:
- Create a custom converter:
public class JsonEnumMemberStringEnumConverter : JsonConverterFactory { public JsonEnumMemberStringEnumConverter() : this(null, true) { } public JsonEnumMemberStringEnumConverter(JsonNamingPolicy? namingPolicy = null, bool allowIntegerValues = true) { this.namingPolicy = namingPolicy; this.allowIntegerValues = allowIntegerValues; this.baseConverter = new JsonStringEnumConverter(namingPolicy, allowIntegerValues); } public override bool CanConvert(Type typeToConvert) => baseConverter.CanConvert(typeToConvert); public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) { var query = from field in typeToConvert.GetFields(BindingFlags.Public | BindingFlags.Static) let attr = field.GetCustomAttribute<EnumMemberAttribute>() where attr != null &amp;&amp; attr.Value != null select (field.Name, attr.Value); var dictionary = query.ToDictionary(p => p.Item1, p => p.Item2); if (dictionary.Count > 0) return new JsonStringEnumConverter(new DictionaryLookupNamingPolicy(dictionary, namingPolicy), allowIntegerValues).CreateConverter(typeToConvert, options); else return baseConverter.CreateConverter(typeToConvert, options); } } public class JsonNamingPolicyDecorator : JsonNamingPolicy { readonly JsonNamingPolicy? underlyingNamingPolicy; public JsonNamingPolicyDecorator(JsonNamingPolicy? underlyingNamingPolicy) => this.underlyingNamingPolicy = underlyingNamingPolicy; public override string ConvertName(string name) => underlyingNamingPolicy?.ConvertName(name) ?? name; } internal class DictionaryLookupNamingPolicy : JsonNamingPolicyDecorator { readonly Dictionary<string, string> dictionary; public DictionaryLookupNamingPolicy(Dictionary<string, string> dictionary, JsonNamingPolicy? underlyingNamingPolicy) : base(underlyingNamingPolicy) => this.dictionary = dictionary ?? throw new ArgumentNullException(); public override string ConvertName(string name) => dictionary.TryGetValue(name, out var value) ? value : base.ConvertName(name); }
- Decorate your enum:
public enum Example { Trick, Treat, [EnumMember(Value = "Trick-Or-Treat")] TrickOrTreat, }
- Use the converter standalone:
var options = new JsonSerializerOptions { Converters = { new JsonEnumMemberStringEnumConverter() }, // Set other options as required: WriteIndented = true, }; var json = JsonSerializer.Serialize(values, options);
- Register the converter with ASP.NET Core:
Refer to the answer to this question by Mani Gandham for guidance on how to do this.
Notes:
- In .NET 6 and earlier, JsonStringEnumConverter ignores its naming policy during deserialization; this issue was fixed in pull request 73348.
- In .Net Core 3.x, the converter may not work as desired with [Flags] enums. This issue was fixed in Issue #31622 in .NET 5.
- If you need to round-trip an enum with custom value names in .NET 6 or earlier, you will need to create a generic converter converter factory from scratch.
- An alternative solution is to use JsonStringEnumMemberConverter from the Macross.Json.Extensions package. Install the package and then decorate your enum with the attribute [JsonConverter(typeof(System.Text.Json.Serialization.JsonStringEnumMemberConverter))].
I hope this explanation helps! Let me know if you have any other questions.
The above is the detailed content of How to Specify Custom Names for Enum Values in System.Text.Json?. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

C language function format letter case conversion steps

What are the types of values returned by c language functions? What determines the return value?

What are the definitions and calling rules of c language functions and what are the

How does the C Standard Template Library (STL) work?

Where is the return value of the c language function stored in memory?

How do I use algorithms from the STL (sort, find, transform, etc.) efficiently?
