WebApi.Formatting.JsonMask
provides a MediaTypeFormatter implementation for ASP.NET Web API that allows the user to specify specific parts of a JS object, hiding/masking the rest, thereby allowing the user to get a partial response.
To use the JsonMaskMediaTypeFormatter
, add the following code to your Web API Configuration:
FormatterConfig.RegisterFormatters(GlobalConfiguration.Configuration.Formatters);
The FormatterConfig
class looks this:
public class FormatterConfig
{
public static void RegisterFormatters(MediaTypeFormatterCollection formatters)
{
var jsonFormatter = formatters.JsonFormatter;
jsonFormatter.SerializerSettings = new JsonSerializerSettings {
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
// Insert the JsonMask formatter in front of the standard JSON formatter.
var jsonMaskFormatter = new JsonMaskMediaTypeFormatter(formatters.JsonFormatter);
formatters.Insert(0, jsonMaskFormatter);
}
}
If you've used the Google APIs, and provided a ?fields=
querystring to get a
Partial Response, you've
already used this language. The main difference is that this MediaTypeFormatter defaults
to looking for a ?$fields=
querystring field. If you wish to use something else, then
you can specify it when creating the MediaTypeFormatter
var jsonMaskFormatter = new JsonMaskMediatypeFormatter(formatters.JsonFormatter, "fields");
You can install through Nuget
Install-Package WebApi.Formatting.JsonMask
The syntax is loosely based on XPath:
a,b,c
comma-separated list will select multiple fieldsa/b/c
path will select a field from its parenta(b,c)
sub-selection will select many fields from a parenta/*/c
the star*
wildcard will select all items in a field
Identify the fields you want to keep
?$fields=name,address/zip
From this sample object:
{
"name": "John Doe",
"age": 32,
"position": "programmer",
"address": {
"city": "Seattle",
"state": "WA",
"zip": "98101"
}
}
You will get back:
{
"name": "John Doe",
"address": {
"zip": "98101"
}
}