简体   繁体   English

ASP.NET Web API部分响应Json序列化

[英]ASP.NET Web API partial response Json serialization

I am implementing a Web API that supports partial response. 我正在实现支持部分响应的Web API。

/api/users?fields=id,name,age

Given the class User 鉴于用户类

[JsonObject(MemberSerialization.OptIn)]
public partial class User
{
  [JsonProperty]
  public int id { get; set; }

  [JsonProperty]
  public string firstname { get; set; }

  [JsonProperty]
  public string lastname { get; set; }

  [JsonProperty]
  public string name { get { return firstname + " " + lastname; } }

  [JsonProperty]
  public int age { get; set; }
}

The Json formatter works great when serializing the all properties, but I can't manage to modify it at runtime to tell it to ignore some of the properties, depending on the query parameter "fields". 在序列化所有属性时,Json格式化程序工作得很好,但我无法在运行时修改它以告诉它忽略某些属性,具体取决于查询参数“fields”。

I am working with JsonMediaTypeFormatter. 我正在使用JsonMediaTypeFormatter。

I have followed http://tostring.it/2012/07/18/customize-json-result-in-web-api/ in order to customize the formatter, but I can't find any example on how to force the formatter to ignore some properties. 我已经关注http://tostring.it/2012/07/18/customize-json-result-in-web-api/以自定义格式化程序,但我找不到任何关于如何强制格式化程序的示例忽略一些属性。

Create your own IContractResolver to tell JSON.NET which properties need to be serialized. 创建自己的IContractResolver告诉JSON.NET需要序列化哪些属性。 There's an example in official documentation you can take draw inspiration from . 在官方文档中有一个例子,你可以从中获取灵感

Just to add to the responses already here; 只是添加到这里的回复; I found a nuget package that does this for you 我找到了一个为你做这个的nuget包

WebApi.PartialResponse WebApi.PartialResponse

Git hub source code: Git hub源代码:
https://github.com/dotarj/PartialResponse https://github.com/dotarj/PartialResponse

It essentially wraps the formatter discussed above, so that you only have to configure it like this: 它基本上包装了上面讨论的格式化程序,因此您只需要像这样配置它:

GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(new PartialJsonMediaTypeFormatter() { IgnoreCase = true });

Then, you can specify ?fields=<whatever> in your request, and it will return the model with only those fields specified. 然后,您可以在请求中指定?fields=<whatever> ,它将返回仅指定了那些字段的模型。

You can also conditionally serialize a property by adding a boolean method with the same name as the property and then prefixed the method name with ShouldSerialize. 您还可以通过添加与属性同名的布尔方法来有条件地序列化属性,然后使用ShouldSerialize为方法名称添加前缀。 The result of the method determines whether the property is serialized. 方法的结果确定属性是否已序列化。 If the method returns true then the property will be serialized, if it returns false and the property will be skipped. 如果该方法返回true,则该属性将被序列化,如果它返回false并且将跳过该属性。

public class Employee
{
  public string Name { get; set; }
  public Employee Manager { get; set; }

  public bool ShouldSerializeManager()
  {
      // don't serialize the Manager property if an employee is their own manager
      return (Manager != this);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM