https://stackoverflow.com/questions/22908204/how-to-decorate-json-net-stringenumconverter/

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 Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;	

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

//https://stackoverflow.com/questions/22908204/how-to-decorate-json-net-stringenumconverter

    public enum MyEnum
    {
        SomeEnumValue,
        Value,
        SomeOtherValue
    }

    class RootObject<T>
    {
        public List<T> Enums { get; set; }
    }

    class TestClass
    {
        public static void Test()
        {
            Test(new[] { MyEnum.SomeEnumValue, MyEnum.SomeOtherValue, MyEnum.SomeEnumValue });

            var settings = new JsonSerializerSettings
            {
                Converters = { new StringEnumConverter(new KebabCaseNamingStrategy()) },
            };
            var json = JsonConvert.SerializeObject(MyEnum.SomeEnumValue, settings);

            Assert.IsTrue(json == "\"some-enum-value\""); // Passes successfully

            Console.WriteLine(json);

            Console.WriteLine("\nAll tests completed.");
        }

        static void Test<T>(IEnumerable<T> enums)
        {
            var rootObject = new RootObject<T> { Enums = enums.ToList() };

            var settings = new JsonSerializerSettings
            {
                Converters = { new StringEnumConverter(typeof(KebabCaseNamingStrategy)) },
            };
            var json = JsonConvert.SerializeObject(rootObject, Formatting.Indented, settings);

            Console.WriteLine(json);

            var root2 = JsonConvert.DeserializeObject<RootObject<T>>(json, settings);

            Assert.IsTrue(rootObject.Enums.SequenceEqual(root2.Enums));
        }
    }
	public class Program
	{
		public static void Main()
		{
			Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
			Console.WriteLine("Json.NET 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;
		}
	}
;