How to Ignoring Fields and Properties Conditionally During Serialization Using JSON.Net? I can't inherit from JsonIgnoreAttribute because it's a sealed class. What should I do?
JsonIgnoreAttribute
sealed
You can use JSON.NET's ShouldSerialize-syntax. There's a good example on JSON.NET site:
http://www.newtonsoft.com/json/help/html/ConditionalProperties.htm
public class Employee { public string Name { get; set; } public Employee Manager { get; set; } public bool ShouldSerializeManager() { // don't serialize the Manager property if an employee is their own manager return (Manager != this); } }
If ShouldSerialize doesn't fit your needs, you can take full control of the serialization with the ContractResolvers: http://www.newtonsoft.com/json/help/html/ContractResolver.htm
1.4m articles
1.4m replys
5 comments
57.0k users