Is there a way to specify which properties to include or exclude when Serializing a class using System.Text.Json.Serialization?
I know that there is the member attribute which I can use (JsonIgnoreAttribute, JsonIgnoreCondition
). But I need a way to include some properties in some scenario and exclude other properties in another scenario.
For example:
- in scenario 'A' I need to include property
foo.name
- in scenario 'B' I need to exclude property
foo.name
JsonIgnoreAttribute will always exclude a property. JsonIgnoreCondition is base on Property value.
In the Newtonsoft JSON library there is the ContractResolver
that do this job. but I really wish to not use 2 JSon serialization engine in my project.