https://stackoverflow.com/questions/59059989/system-text-json-how-do-i-specify-a-custom-name-for-an-enum-value

CSharp · Console

/Program.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters;
using System.IO;
using System.Data;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Reflection;
using System.Runtime.Serialization;
using System.Collections.ObjectModel;

using System.Text.Json;
using System.Text.Json.Serialization;

using NUnit.Framework; // Throws NUnit.Framework.AssertionException

//https://stackoverflow.com/questions/59059989/system-text-json-how-do-i-specify-a-custom-name-for-an-enum-value

	[Flags]
	public enum Example 
	{
	  Trick = (1<<0),
	  Treat = (1<<1),
	  [EnumMember(Value = "Trick-Or-Treat")]
	   TrickOrTreat = (1<<2),
	}

	public class RootObject
	{
		public Example? Example { get; set; }
	}

	public class CustomJsonStringEnumConverter : JsonConverterFactory
	{
        private readonly JsonNamingPolicy namingPolicy;
        private readonly bool allowIntegerValues;
		private readonly JsonStringEnumConverter baseConverter;

		public CustomJsonStringEnumConverter() : this(null, true) { }

        public CustomJsonStringEnumConverter(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
						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 == null ? name : underlyingNamingPolicy.ConvertName(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);
	}

    class TestClass
    {
        public static void Test()
        {
			var values = Enum.GetValues(typeof(Example)).Cast<Example>().Concat(new [] { Example.Trick | Example.TrickOrTreat, Example.Treat | Example.TrickOrTreat, Example.Trick | Example.Treat | Example.TrickOrTreat }).ToArray();
			
			Test(values, null);
			Test(values, JsonNamingPolicy.CamelCase);
			Test(values, new TestNamingPolicy());

			TestSimple(null);
			TestSimple(JsonNamingPolicy.CamelCase);
			TestSimple(new TestNamingPolicy());

			Console.WriteLine("\nJson.NET output: ");
			var settings = new Newtonsoft.Json.JsonSerializerSettings
			{
				Converters = { new Newtonsoft.Json.Converters.StringEnumConverter(new TestNamingStrategy()) },
				Formatting = Newtonsoft.Json.Formatting.Indented,
			};
			Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(values, settings));
		}	

		static void TestSimple(JsonNamingPolicy namingPolicy)
		{
			var options = new JsonSerializerOptions
			{
				Converters = { new CustomJsonStringEnumConverter(namingPolicy) },
				WriteIndented = false,
			};
			Assert.IsTrue(JsonSerializer.Serialize(Example.TrickOrTreat, options) == "\"Trick-Or-Treat\"");
			
			var test = new RootObject { Example = Example.TrickOrTreat };
			var json = JsonSerializer.Serialize(test, options);
			Assert.IsTrue(json.Contains("Trick-Or-Treat"));
		}
		
        public static void Test<T>(T values, JsonNamingPolicy namingPolicy)
        {
			var options = new JsonSerializerOptions
			{
				Converters = { new CustomJsonStringEnumConverter(namingPolicy) },
				WriteIndented = true,
			};
			var json = JsonSerializer.Serialize(values, options);
			Console.WriteLine(json);
		}	
	}

	public class TestNamingStrategy : Newtonsoft.Json.Serialization.NamingStrategy
	{
        protected override string ResolvePropertyName(string name) => "enum_" + name;
	}

	public class TestNamingPolicy : JsonNamingPolicy
	{
		public override string ConvertName (string name) => "enum_" + name;
	}

	public class Program
	{
		public static void Main()
		{
			Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
			Console.WriteLine("System.Text.Json version: " + typeof(JsonSerializer).Assembly.FullName);
			Console.WriteLine();

			try
			{
				TestClass.Test();
			}
			catch (Exception ex)
			{
				Console.WriteLine("Failed with unhandled exception: ");
				Console.WriteLine(ex);
				throw;
			}
		}
		
		public static string GetNetCoreVersion()
		{
			//https://techblog.dorogin.com/how-to-detect-net-core-version-in-runtime-ecd65ad695be
			//
			var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
			var assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
			int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
			if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
				return assemblyPath[netCoreAppIndex + 1];
			return null;
		}
	}
;