managing property names returned in json from web api
asp.net, asp.net-web-api, c#, rest
Solution
The easy way to do this is through a data contract. Here is an article, but basically, it involves two annotations on your model. It also allows you to ignore anything you don't want serialized.
[DataContract]
public class Foo { //Your model class
[DataMember(Name="bar-none")] //This also allows you to use chars like '-'
public string bar {get; set;}
[IgnoreDataMember] //Don't serialize this one
public List<string> fuzz { get; set;}
}
Problem
I am currently working with ASP.NET web api where I return a Model object like following from my REST operation: Product with properties: `Name, Id, Description, etc.....` When this is converted to a `JSON` object, it outputs it with property names above. To cut down the payload returned from the web api operation, is there any way I can change the properties in the JSON object like for example `Desc` for `Description`. I could change the Model object but the property names would not make sense then!