Domanda

using JSON.Net like this:

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

How much camel caseing does JSON.Net do?

Does it just lowercase letters starting at the beginning of the word?

Examples:

  • somePropertyId -> somePropertyId
  • somePropertyID -> somePropertyID
  • SOMEPropertyID -> somePropertyID
  • SOMEPROPERTYID -> somepropertyid
È stato utile?

Soluzione

Here is what CamelCasePropertyNamesContractResolver does, direct from the unit tests: 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"));
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top