using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public class KebabCaseNamingStrategy : SnakeCaseNamingStrategy
protected override string ResolvePropertyName(string name)
return base.ResolvePropertyName(name).Replace('_', '-');
public List<T> Enums { get; set; }
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\"");
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);
var root2 = JsonConvert.DeserializeObject<RootObject<T>>(json, settings);
Assert.IsTrue(rootObject.Enums.SequenceEqual(root2.Enums));
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("Failed with unhandled exception: ");
public static string GetNetCoreVersion()
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];