Home >Backend Development >C++ >How to Customize Enum Values in System.Text.Json?

How to Customize Enum Values in System.Text.Json?

Barbara Streisand
Barbara StreisandOriginal
2025-01-14 08:36:44548browse

How to Customize Enum Values in System.Text.Json?

Customize enumeration values ​​in System.Text.Json

Difficulty: Specifying custom values ​​for enumeration values ​​

In .NET 5 and above, we cannot directly use JsonPropertyName to specify custom values ​​for enumeration values ​​like we can with normal properties. This feature is not available out of the box.

Method 1: Customize JsonConverterFactory (recommended)

To solve this problem, we introduced a JsonConverter factory (JsonEnumMemberStringEnumConverter) that leverages JsonStringEnumConverter and adapted it to use a custom [EnumMember(Value="xxx")] for each enum type annotated with JsonNamingPolicy. For example:

<code class="language-csharp">[EnumMember(Value = "Trick-Or-Treat")] // 自定义值
public enum Example { Trick, Treat, TrickOrTreat }</code>

Custom converter registration and usage:

<code class="language-csharp">var options = new JsonSerializerOptions
{
    Converters = { new JsonEnumMemberStringEnumConverter() },
    // ...
};
var json = JsonSerializer.Serialize(values, options);</code>

This converter has the following advantages:

  • Seamless round trip processing of enums with custom values.
  • Preserve custom values ​​when deserializing.
  • handles enums with the same value, consistent with JsonStringEnumConverter.
  • Supports mutable and immutable enumeration types.

Method 2: Macross.Json.Extensions package

Alternatively, we can use the Macross.Json.Extensions package, which provides a JsonStringEnumMemberConverter that after installation allows us to annotate the enumeration as follows:

<code class="language-csharp">[JsonConverter(typeof(System.Text.Json.Serialization.JsonStringEnumMemberConverter))]
public enum Example { Trick, Treat, [EnumMember(Value="Trick-Or-Treat")] TrickOrTreat }</code>

Method Three: Generic Converter Factory with Custom Overrides (Advanced)

If you need finer control, or need to support enums with custom values ​​in versions of .NET earlier than 6.0, you can create a generic converter factory and converter from scratch. This approach is more complex and may require backporting for earlier versions.

Notes

  • Enumeration types with [Flags] attributes may require a modified JsonConverter. See the instructions in the response for more details on handling this situation.
  • In .NET versions prior to 6.0, JsonStringEnumConverter would ignore its JsonNamingPolicy during deserialization. This issue was fixed in pull request 73348.
  • If your enumeration has both [EnumMember] and [JsonPropertyName] attributes, the value of [EnumMember] in the custom converter will take precedence.

The above is the detailed content of How to Customize Enum Values in System.Text.Json?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn