With JSON.Net, I see 2 different ways of saying that I want my properties to be serialized in camelCase:
According to this code snippet, both options give the same result since the assertion does not fail:
public class Bar
{
public int SomeValue { get; set; }
}
public class Foo
{
public Bar Bar { get; set; } = new Bar();
public string AnotherValue { get; set; }
}
[Fact]
public void TestBothOptions()
{
var x = new Foo();
x.AnotherValue = "test";
x.Bar.SomeValue = 12;
var serializerSettingsWithNamingStrategy = new JsonSerializerSettings();
serializerSettingsWithNamingStrategy.ContractResolver = new DefaultContractResolver
{
NamingStrategy = new CamelCaseNamingStrategy(),
};
var serializerSettingsWithContractResolver = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
};
var one = JsonConvert.SerializeObject(x, serializerSettingsWithNamingStrategy);
var two = JsonConvert.SerializeObject(x, serializerSettingsWithContractResolver);
Assert.Equal(one, two); // {"bar":{"someValue":12},"anotherValue":"test"}
}
So, does anybody know the difference between the two options?