简体   繁体   English

JSON / XML序列化:忽略/包含基类中的一些字段

[英]JSON/XML serialization: Ignore/include some fields from the base class

In Web API JSON and XML Media-Type Formatters (and especially serializers) could be configured with [JsonIgnore] or [DataMember] decorators. Web API可以使用[JsonIgnore][DataMember]装饰器配置 JSON和XML媒体类型格式化程序(尤其是序列化程序)。

It works for the methods fields only, but what about the base class? 它仅适用于方法字段,但基类怎么样? Is there a way to ignore or include some fields of it? 有没有办法忽略或包含它的某些领域?

And if there is no - what is the best approach to control the visibility of the class fields in the serialized output? 如果没有 - 控制序列化输出中类字段可见性的最佳方法是什么? Defining specified JSON/XML serializers? 定义指定的JSON / XML序列化程序? Converting class object to another class object with selected fields in the Controller ? 使用Controller选定字段将类对象转换为另一个类对象?

You should use a DTO that is specific for your current action and then copy the data from the DTO to the actual object you want to use. 您应该使用特定于当前操作的DTO,然后将数据从DTO复制到您要使用的实际对象。

Benefits of this is you can define validation that are specific to this action and you'll have no attributes on the business object. 这样做的好处是您可以定义特定于此操作的验证,并且您将没有业务对象的属性。 You can use different validation rules per action for each new DTO and still use the same business object. 您可以为每个新DTO的每个操作使用不同的验证规则,并仍然使用相同的业务对象。

If you don't want to write the mapping code to map from the DTO to the business object you can use AutoMapper . 如果您不想编写映射代码以从DTO映射到业务对象,则可以使用AutoMapper

Example code: 示例代码:

public enum ColourType
{
  Flat,
  Metallic
}

public class Car
{
  public void Repaint(int red, int green, int blue, ColourType colourType)
  {
    // TODO: Add some validation logic and business logic.
  }
}

public class RepainInMetallicModel
{
  [Required]
  [Range(0, 100)]
  public int Red { get; set; }

  [Required]
  [Range(0, 100)]
  public int Green { get; set; }

  [Required]
  [Range(0, 100)]
  public int Blue { get; set; }
}

public class RepaintInFlatModel
{
  [Required]
  [Range(0, 255)]
  public int Red { get; set; }

  [Required]
  [Range(0, 255)]
  public int Green { get; set; }

  [Required]
  [Range(0, 255)]
  public int Blue { get; set; }
}

public class CarController
{
  public ActionResult RepaintInMetallic(RepainInMetallicModel model)
  {
    if (ModelState.IsValid)
    {
      var car = _carsRepository.Find(model.Id);

      var.Repaint(model.Red, model.Green, model.Blue, ColourType.Metallic);

      _carsRepository.Save(car);
    }

    return View();
  }

  public ActionResult RepaintInFlat(RepaintInFlatModel model)
  {
    if (ModelState.IsValid)
    {
      var car = _carsRepository.Find(model.Id);

      var.Repaint(model.Red, model.Green, model.Blue, ColourType.Flat);

      _carsRepository.Save(car);
    }

    return View();
  }
}

The following example doesn't demonstrate the usage of AutoMapper , but it does demonstrate the usage of DTOs. 以下示例未演示AutoMapper的用法,但它确实演示了DTO的用法。

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

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