private static string FormatName(string name, UnderscoreConvention underscoring) { switch (underscoring) { case UnderscoreConvention.Prefix: return(name.Length > 0 && name[0] != '_' ? '_' + name : name); case UnderscoreConvention.Separate: StringBuilder sb = null; for (int i = 1; i < name.Length; ++i) { char ch = name[i]; if (char.IsUpper(ch)) { if (sb == null) { sb = new StringBuilder(); sb.Append(name, 0, i); } sb.Append('_'); } if (sb != null) { sb.Append(ch); } } return(sb != null?sb.ToString() : name); default: return(name); } }
static void TestNamingCase(string baseName, NamingConvention testCase, UnderscoreConvention testUnder, string expected) { var attribute = new JsonMemberNamingConventionAttribute(testCase, testUnder); var property = CreateTestProperty(baseName); IPropertyDescriptorCustomization customization = attribute; customization.Apply(property); Assert.AreEqual(expected, property.CustomizedName); }
void IPropertyDescriptorCustomization.Apply(PropertyDescriptor property) { if (property == null) { throw new ArgumentNullException("property"); } NamingConvention naming = Convention; UnderscoreConvention underscoring = Underscores; if (naming == NamingConvention.None && underscoring == UnderscoreConvention.None) { return; } SetName(property, FormatName(FormatName(property.Name, underscoring), naming)); }
public JsonMemberNamingConventionAttribute(NamingConvention naming, UnderscoreConvention underscores) { _convention = naming; _underscores = underscores; }
private static string FormatName(string name, UnderscoreConvention underscoring) { switch (underscoring) { case UnderscoreConvention.Prefix: return name.Length > 0 && name[0] != '_' ? '_' + name : name; case UnderscoreConvention.Separate: StringBuilder sb = null; for (int i = 1; i < name.Length; ++i) { char ch = name[i]; if (char.IsUpper(ch)) { if (sb == null) { sb = new StringBuilder(); sb.Append(name, 0, i); } sb.Append('_'); } if (sb != null) sb.Append(ch); } return sb != null ? sb.ToString() : name; default: return name; } }
private static void TestNamingCase(string baseName, NamingConvention testCase, UnderscoreConvention testUnder, string expected) { JsonMemberNamingConventionAttribute attribute = new JsonMemberNamingConventionAttribute(testCase, testUnder); TestPropertyDescriptor property = CreateTestProperty(baseName); IPropertyDescriptorCustomization customization = attribute; customization.Apply(property); Assert.AreEqual(expected, property.CustomizedName); }