Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want my class to be serialized and deserialized using camel case naming convention. I know I can use the JsonConvert.SerializeObject(object, settings) overload as stated here:

var serializerSettings = new JsonSerializerSettings();
serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
var json = JsonConvert.SerializeObject(product, serializerSettings);

Is there any way to apply the same configuration on a class level (via attributes) so that I do not need to override the serialization settings?

I could write a custom converter but that looks like an overkill for such a simple thing.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
369 views
Welcome To Ask or Share your Answers For Others

1 Answer

If you're using Json.NET 9.0.1 or later you can use the NamingStrategyType property on the JsonObjectAttribute to achieve what you want. If you need to pass arguments to the NamingStrategy's constructor then specify them with the NamingStrategyParameters property. Below is an example of how to specify a class with a camel case naming strategy.

[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class Foo
{
    public string Bar;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...