Last active
April 18, 2016 01:55
-
-
Save beachside-project/6885921da9231477d4e1e665c06d2111 to your computer and use it in GitHub Desktop.
jsonDemo_SnakeCaseContractResolver
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
public class SnakeCaseContractResolver : DefaultContractResolver | |
{ | |
private const string SnakeDelimiter = "_"; | |
protected override IList<JsonProperty> CreateProperties(System.Type type, MemberSerialization memberSerialization) | |
=> base.CreateProperties(type, memberSerialization).Select(ConvertSnakeCasePropertyName).ToList(); | |
private static JsonProperty ConvertSnakeCasePropertyName(JsonProperty p) | |
{ | |
var target = p.PropertyName; | |
p.PropertyName = (target.Substring(0, 1) + | |
string.Concat(target.ToCharArray(1, target.Length - 1).Select(c => char.IsUpper(c) ? SnakeDelimiter + c.ToString() : c.ToString())) | |
).ToLower(); | |
return p; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment