1

使用JSON.Net这样的:CamelCasePropertyNamesContractResolver做了多少骆驼?

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

多少骆驼caseing不JSON.Net吗?

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

例子:

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

为什么不试试看看?应该很容易做出一个快速一扔控制台应用程序来测试这种行为。 – 2013-05-09 22:34:05

回答

5

这里是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")); 
} 
+0

正是我在找的,谢谢! – Homer 2013-05-10 13:12:19

+2

詹姆斯 - 这是一个耻辱,这是StringUtils类标记为内部。我使用你的库来创建JSON,然后我想要一个HTML助手使用相同的驼峰字符串函数,以便它们始终匹配。不过,我不能这样做,因为它的内部标记 – 2013-06-16 14:27:20