Created
March 13, 2018 20:16
-
-
Save duruld/a67a80f05cb610fd39f6c2e6ad03844d to your computer and use it in GitHub Desktop.
Adding kebab-casing formatting for JsonConvert, Json.NET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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