I'm using an MVC 4 web API and asp.net web forms 4.0 to build a rest API. It's working great:
[HttpGet]
public HttpResponseMessage Me(string hash)
{
HttpResponseMessage httpResponseMessage;
List<Something> somethings = ...
httpResponseMessage = Request.CreateResponse(HttpStatusCode.OK,
new { result = true, somethings = somethings });
return httpResponseMessage;
}
Now I need to prevent some properties to be serialized. I know I can use some LINQ over the list and get only the properties I need, and generally it's a good approach, but in the present scenario the something
object is too complex, and I need a different set of properties in different methods, so it's easier to mark, at runtime, each property to be ignored.
Is there a way to do that?
ASP.NET Web API uses
Json.Net
as default formatter, so if your application just only uses JSON as data format, you can use[JsonIgnore]
to ignore property for serialization:But, this way does not support XML format. So, in case your application has to support XML format more (or only support XML), instead of using
Json.Net
, you should use[DataContract]
which supports both JSON and XML:For more understanding, you can read the official article.
Instead of letting everything get serialized by default, you can take the "opt-in" approach. In this scenario, only the properties you specify are allowed to be serialized. You do this with the
DataContractAttribute
andDataMemberAttribute
, found in the System.Runtime.Serialization namespace.The
DataContactAttribute
is applied to the class, and theDataMemberAttribute
is applied to each member you want to be serialized:Dare I say this is a better approach because it forces you to make explicit decisions about what will or will not make it through serialization. It also allows your model classes to live in a project by themselves, without taking a dependency on JSON.net just because somewhere else you happen to be serializing them with JSON.net.
Try using
IgnoreDataMember
propertyI will show you 2 ways to accomplish what you want:
First way: Decorate your field with JsonProperty attribute in order to skip the serialization of that field if it is null.
Second way: If you are negotiation with some complex scenarios then you could use the Web Api convention ("ShouldSerialize") in order to skip serialization of that field depending of some specific logic.
WebApi uses JSON.Net and it use reflection to serialization so when it has detected (for instance) the ShouldSerializeFieldX() method the field with name FieldX will not be serialized.
This worked for me: Create a custom contract resolver which has a public property called AllowList of string array type. In your action, modify that property depending on what the action needs to return.
1. create a custom contract resolver:
2. use custom contract resolver in action
This approach allowed me to allow/disallow for specific request instead of modifying the class definition. And if you don't need XML serialization, don't forget to turn it off in your
App_Start\WebApiConfig.cs
or your API will return blocked properties if the client requests xml instead of json.Almost same as greatbear302's answer, but i create ContractResolver per request.
1) Create a custom ContractResolver
2) Use custom contract resolver in action
Edit:
It didn't work as expected(isolate resolver per request). I'll use anonymous objects.