We’re updating our architecture to use a single object model for desktop, web and mobile that can be used in the MVVM pattern. I would like to be able to limit the data fields that are serialized through Web API by using interfaces on the controllers. This is required because the model objects for mobile are stored in HTML5 local storage so don’t carry optional data while a thin desktop client would be able to store (and work with) more data.
To achieve this a model will implement the different interfaces that define which data fields should be serialized and there will be a controller specific to the interface.
The problem is that the Web API always serializes every field in the model even if it is not part of the interface being returned. How can we only serialize fields in the returned interface?
I think you can do this with JsonConverter or using ContractResolver
Create
JsonConverterand override theWriteJsonand write your logic to serialise only those data that is needed. Remember to overrideCanConvertand alsoCanWrite.You can have a look at the link Custom conversion of specific objects in JSON.NET to get an idea
For ContractResolver see the link http://quickduck.com/blog/2011/08/08/overriding-the-default-serialization-behavior-in-json-net/. In this instead of using
base.CreateObjectContract( objectType );usenew JsonObjectContract( objectType );Hope this helps