NewtonSoft add JSONIGNORE at runTime

后端 未结 5 2271
一个人的身影
一个人的身影 2020-11-27 05:05

Am looking to Serialize a list using NewtonSoft JSON and i need to ignore one of the property while Serializing and i got the below code

pub         


        
5条回答
  •  星月不相逢
    2020-11-27 05:45

    No need to do the complicated stuff explained in the other answer.

    NewtonSoft JSON has a built-in feature for that:

    public bool ShouldSerializeINSERT_YOUR_PROPERTY_NAME_HERE()
    {
        if(someCondition){
            return true;
        }else{
            return false;
        }
    }
    

    It is called "conditional property serialization" and the documentation can be found here.

    Warning: first of all, it is important to get rid of [JsonIgnore] above your {get;set;} property. Otherwise it will overwrite the ShouldSerializeXYZ behavior.

提交回复
热议问题