Is there a way to ignore get-only properties in Json.NET without using JsonIgnore attributes?

后端 未结 4 1210
不知归路
不知归路 2020-11-29 05:19

Is there a way to ignore get-only properties using the Json.NET serializer but without using JsonIgnore attributes?

For example, I have a class with the

4条回答
  •  天命终不由人
    2020-11-29 05:45

    Json.net does have the ability to conditionally serialize properties without an attribute or contract resolver. This is especially useful if you don't want your project to have a dependency on Json.net.

    As per the Json.net documentation

    To conditionally serialize a property, add a method that returns boolean with the same name as the property and then prefix the method name with ShouldSerialize. The result of the method determines whether the property is serialized. If the method returns true then the property will be serialized, if it returns false then the property will be skipped.

提交回复
热议问题