Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# JSON.NET convention that follows Ruby property naming conventions?

Tags:

I am using http://json.codeplex.com/ and I am talking to a Ruby based Rest API. Problem is that most of the properties have a ruby underscore naming convention. I am wondering if anyone knows of a way so that I can avoid having to Add lots of JsonProperty.

For example I want to avoid adding the JsonProperty attribute and have a convention built into the serializer settings so that it knows to try and map properties with an underscore in the to the .NET naming convention :)

public class Member {     [JsonProperty(PropertyName = "avatar_url")]     public string AvatarUrl { get; set; }      [JsonProperty(PropertyName = "twitter_screen_name")]     public string TwitterScreenName { get; set; }      [JsonProperty(PropertyName = "website_url")]     public string WebSiteUrl { get; set; } } 
like image 866
superlogical Avatar asked Oct 13 '10 10:10

superlogical


1 Answers

Update - September 2016:

Json.NET 9.0.1 has SnakeCaseNamingStrategy. You can use that to have twitter_screen_name style properties automatically.


Inherit from DefaultContractResolver and override ResolvePropertyName to format property names as you'd like.

CamelCasePropertyNamesContractResolver does a similar global change to property names.

like image 170
James Newton-King Avatar answered Oct 12 '22 19:10

James Newton-King