CamelCasePropertyNamesContractResolver做了多少骆驼套管?

时间:2013-05-09 22:00:07

标签: serialization json.net camelcasing jsonserializer sanity-check

像这样使用JSON.Net:

JsonConvert.SerializeObject(someObject, 
                            Newtonsoft.Json.Formatting.None, 
                            new JsonSerializerSettings() {
                                NullValueHandling = NullValueHandling.Ignore,
                                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                                ContractResolver = new CamelCasePropertyNamesContractResolver()
                            });

JSON.Net做了多少骆驼案例?

它只是从单词的开头开始小写字母吗?

示例:

  • somePropertyId - > somePropertyId
  • somePropertyID - > somePropertyID
  • SOMEPropertyID - > somePropertyID
  • SOMEPROPERTYID - > somepropertyid

1 个答案:

答案 0 :(得分:6)

以下是CamelCasePropertyNamesContractResolver所做的工作,直接来自单元测试:https://github.com/JamesNK/Newtonsoft.Json/blob/95665429a431364327b4bce5332c39fda7819e7b/Src/Newtonsoft.Json.Tests/Utilities/StringUtilsTests.cs#L40-L54

[Test]
public void ToCamelCaseTest()
{
  Assert.AreEqual("urlValue", StringUtils.ToCamelCase("URLValue"));
  Assert.AreEqual("url", StringUtils.ToCamelCase("URL"));
  Assert.AreEqual("id", StringUtils.ToCamelCase("ID"));
  Assert.AreEqual("i", StringUtils.ToCamelCase("I"));
  Assert.AreEqual("", StringUtils.ToCamelCase(""));
  Assert.AreEqual(null, StringUtils.ToCamelCase(null));
  Assert.AreEqual("iPhone", StringUtils.ToCamelCase("iPhone"));
  Assert.AreEqual("person", StringUtils.ToCamelCase("Person"));
  Assert.AreEqual("iPhone", StringUtils.ToCamelCase("IPhone"));
  Assert.AreEqual("i Phone", StringUtils.ToCamelCase("I Phone"));
  Assert.AreEqual(" IPhone", StringUtils.ToCamelCase(" IPhone"));
}