I extend the Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver and apply the new resolver in my WebApi:
public static void Register(HttpConfiguration config)
{
var json = config.Formatters.JsonFormatter.SerializerSettings;
json.ContractResolver = new CustomPropertyNamesContractResolver();
json.Formatting = Formatting.Indented;
config.MapHttpAttributeRoutes();
}
and here is the overridden method of my custom name resolver (CustomPropertyNamesContractResolver class):
protected override string ResolvePropertyName(string propertyName)
{
if (propertyName.Equals("ID"))
return "id";
// return the camelCase
propertyName = base.ResolvePropertyName(propertyName);
if (propertyName.EndsWith("ID"))
propertyName = propertyName.Substring(0, propertyName.Length - 1) + "d";
return propertyName;
}
My issue is that the results are indeed in camel case but properties like "QuestionID", are never converted to "questionId" - what I keep on receiving is "questionID".
Plus, my custom ResolvePropertyName() method is never called (tested it with breakpoints), so it seems that only the ResolvePropertyName() method of my parent class (CamelCasePropertyNamesContractResolver) is called somehow.
Now, when I inherit directly from DefaultContractResolver (which is the parent of CamelCasePropertyNamesContractResolver) my custom ResolvePropertyName() method is called.
Can someone explain me what happens here? Am I missing something?