如何使用C#执行条件序列化-NewtonSoft.Json [英] How to do Conditional Serialization using C# - NewtonSoft.Json
本文介绍了如何使用C#执行条件序列化-NewtonSoft.Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用NewtonSoft.Json做json序列化
I am doing json serialization using NewtonSoft.Json
public class CommonBase
{
[JsonProperty(PropertyName = "u_customer_id")]
public long CustomerId { get; set; }
}
我想进行条件序列化,以便如果CustomerId
的值为0,我想在json序列化期间为CustomerId
设置一个空白值.由于CommonBase
是基类,因此我无法将数据类型从long
更改为string
.
I want to do a conditional serialization so that if CustomerId
value is 0, I want to set a blank value for CustomerId
during json serialization. Since CommonBase
is a base class and I am not able to change data type from long
to string
.
我该如何实现?
推荐答案
我已通过将CustomerId属性更改为可为空来解决此问题.
I have solved this issue by changing CustomerId property as nullable.
public long? CustomerId { get; set; }
这篇关于如何使用C#执行条件序列化-NewtonSoft.Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文