c# - 如何对 Object 类型的属性使用 ShouldSerialize[MemberName]() 方法?

标签 c# serialization json.net

我尝试使用 Newtonsoft.Json 中的 ShouldSerialize 方法来阻止没有为其属性分配新值的类型对象的属性。但我不知道如何实现它,所以请帮我解决这个问题......

这是示例代码

public class Sample1
 {
   public String name{get;set;}
   public int Id{get;set;}; 
 }

这是我的类,包含上述类作为其属性之一

public class Container
 {
   public String Cname{get;set;}
   public Sample1 Sample{get;set;}; 

   public bool ShouldSerializeSample()
  {
      //What should I write here to prevent the Sample property from being serialized when its properties are assigned no new values.

  }
 }

最佳答案

鉴于您的示例类(class),我认为您正在寻找这样的东西:

public bool ShouldSerializeSample()
{
    return (Sample != null && (Sample.Id != 0 || Sample.name != null));
}

这是一个工作演示:

class Program
{
    static void Main(string[] args)
    {
        List<Container> list = new List<Container>
        {
            new Container
            {
                Cname = "Will serialize Sample because it has a name",
                Sample = new Sample1 { name = "sample 1" }
            },
            new Container
            {
                Cname = "Will serialize Sample because it has a non-zero Id",
                Sample = new Sample1 { Id = 2 }
            },
            new Container
            {
                Cname = "Will serialize Sample because it has a name and an Id",
                Sample = new Sample1 { name = "sample 3", Id = 3 }
            },
            new Container
            {
                Cname = "Will not serialize Sample because it has default values",
                Sample = new Sample1()
            },
            new Container
            {
                Cname = "Will not serialize Sample because it is null",
                Sample = null
            }
        };

        string json = JsonConvert.SerializeObject(list, Formatting.Indented);
        Console.WriteLine(json);
    }
}

public class Sample1
{
    public String name { get; set; }
    public int Id { get; set; }
}

public class Container
{
    public String Cname { get; set; }
    public Sample1 Sample { get; set; }

    public bool ShouldSerializeSample()
    {
        return (Sample != null && (Sample.Id != 0 || Sample.name != null));
    }
}

这是输出:

[
  {
    "Cname": "Will serialize Sample because it has a name",
    "Sample": {
      "name": "sample 1",
      "Id": 0
    }
  },
  {
    "Cname": "Will serialize Sample because it has a non-zero Id",
    "Sample": {
      "name": null,
      "Id": 2
    }
  },
  {
    "Cname": "Will serialize Sample because it has a name and an Id",
    "Sample": {
      "name": "sample 3",
      "Id": 3
    }
  },
  {
    "Cname": "Will not serialize Sample because it has default values"
  },
  {
    "Cname": "Will not serialize Sample because it is null"
  }
]

关于c# - 如何对 Object 类型的属性使用 ShouldSerialize[MemberName]() 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19698844/

相关文章:

c# - 如何发出 GET RESTful 请求

c# - 将实体添加到 Stanford NLP NER 分类器

javascript - 如何序列化和反序列化 JavaScript 对象?

c# - 如何反序列化以日期为键的复杂 JSON C#

c# - 发送一个 JSON 数组以作为 Dictionary<string,string> 接收

c# - WCF 的不可序列化数据成员

c# - 在 Unity 中保存/加载数据

c# - 避免默认构造函数和公共(public)属性 setter

JSON.net 在属性名称中有 @?

c# - 在 C# 窗口中嵌入网页