Skip to content

Instantly share code, notes, and snippets.

@duruld
Created March 13, 2018 20:16
Show Gist options
  • Save duruld/a67a80f05cb610fd39f6c2e6ad03844d to your computer and use it in GitHub Desktop.
Save duruld/a67a80f05cb610fd39f6c2e6ad03844d to your computer and use it in GitHub Desktop.
Adding kebab-casing formatting for JsonConvert, Json.NET
using Newtonsoft.Json.Serialization;
using System.Text.RegularExpressions;
public class KebabCaseNamingStrategy : NamingStrategy
{
protected override string ResolvePropertyName(string name)
{
return Regex.Replace(name, "([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))", "$1-").ToLower();
}
}
using Newtonsoft.Json;
[JsonObject(NamingStrategyType = typeof(KebabCaseNamingStrategy))]
public class Model
{
public string HelloWorld { get; set; } // will be hello-world
public int IAmAnInteger { get; set; } // will be i-am-an-integer
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment