本文介绍了如何使用Object类型的属性ShouldSerialize [MemberName()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图阻止类型对象的财产分配给Newtonsoft.Json使用ShouldSerialize方法其属性没有新的值。但我不知道如何实现它,所以请帮我解决这个...
下面是示例代码
公共类样品1
{
公共字符串名称{;设置;}
公众诠释标识{获取;集;};
}
这是包含上述类作为它的一个属性我的课p>
公共类集装箱
{
公共字符串Cname的{获取;集;}
公共样品1样品{获取;设置;};
公共BOOL ShouldSerializeSample()
{
//我应该写在这里以防止样品财产被序列化时,其属性分配没有新的值。
}
}
解决方案
鉴于你的示例类,我想你,你正在寻找的东西是这样的:
公共BOOL ShouldSerializeSample()
{
回报率(样本= NULL&放大器;!及(Sample.Id = 0 || Sample.name = NULL)!);
}
下面是一个工作演示:
类节目
{
静态无效的主要(字串[] args)
{
名单<集装箱>名单=新名单,LT;集装箱>
{
新集装箱
{
Cname的=会序列样品,因为它有一个名字,
样品=新的样品1 {NAME =样品1}
},
新集装箱
{
Cname的=会序列样品,因为它有一个非零ID,
样品=新样品1 {ID = 2}
},
新集装箱
{
Cname的=会序列样品,因为它有一个名字和一个ID,
样品=新的样品1 {NAME =样品3 ID = 3}
},
新集装箱
{
Cname的=不会序列化样品,因为它有默认值,
样品=新的样品1( )
},
新集装箱
{
Cname的=不会序列化样品,因为它是空,
样品= NULL
}
};
JSON字符串= JsonConvert.SerializeObject(列表,Formatting.Indented);
Console.WriteLine(JSON);
}
}
公共类样品1
{
公共字符串名称{;组; }
公众诠释标识{搞定;组; }
}
公共类集装箱
{
公共字符串{的Cname获得;组; }
公共样品1样品{搞定;组; }
公共BOOL ShouldSerializeSample()
{
返回(样本= NULL&放大器;!&安培;!(Sample.Id = 0 || Sample.name = NULL)) ;
}
}
下面是输出:
[
{
「CNAME:会序列样品,因为它有一个名字,
样本 :{
名:样品1,
标识:0
}
},
{
「CNAME:请问序列化样品,因为它有一个非零ID,
样本:{
名:空,
标识:2
}
} ,
{
「CNAME:会序列样品,因为它有一个名字和一个ID,
样本:{
名:样品3
标识:3
}
},
{
「CNAME:不会序列化样品,因为它有默认值
},
{
「CNAME:不会序列化样品,因为它是空
}
]
I have tried to prevent the property of type object with no new values assigned to its properties using ShouldSerialize Method in Newtonsoft.Json. But I dont know how to implement it, so please help me to solve this...
Here is the sample code
public class Sample1
{
public String name{get;set;}
public int Id{get;set;};
}
And this is my Class containing the above class as one of its properties
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.
}
}
解决方案
Given your example classes, I think you you are looking for something like this:
public bool ShouldSerializeSample()
{
return (Sample != null && (Sample.Id != 0 || Sample.name != null));
}
Here is a working demo:
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));
}
}
Here is the output:
[
{
"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"
}
]
这篇关于如何使用Object类型的属性ShouldSerialize [MemberName()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!