How to do Conditional Serialization using C# - NewtonSoft.Json

穿精又带淫゛_ 提交于 2019-12-01 14:32:41

You almost have the answer in your question title. What you are looking for is Conditional Property Serialization

You just need to add method named like this: ShouldSerialize + PropertyName. In your case method should look like:

public bool ShouldSerializeCustomerId()
{
   return SomeCondition;
}

P.s. if you are creating base class, you probably want to have abstract class.

I have solved this issue by changing CustomerId property as nullable.

   public long? CustomerId { get; set; }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!