I am doing json serialization using NewtonSoft.Json
public class CommonBase
{
[JsonProperty(PropertyName = \"u_customer_id\")]
public long CustomerId
I have solved this issue by changing CustomerId property as nullable.
public long? CustomerId { get; set; }
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.