I've been trying to figure out a way to ignore some objects from being serialized based on some conditions. All I can find is how to ignore properties of an object using the ShouldSerialize* method, but not how to ignore an entire object.
Here's an example that explains my situation. A company can have multiple employees, and the employees can be either current or non-current.
Public Class Company
Public Property Name As String
Public Property Employees As List(Of Employee)
End Class
Public Class Employee
Public Property FirstName As List(Of Name)
Public Property LastName As List(Of Name)
Public Property Current As Boolean
End Class
I want to be able to ignore/exclude non-current employees from being serialized to json.
The only way I can think of right now is to separate current and non-current employees into two properties, so that I can just use the <JsonIgnoreAttribute()> for the non-current one.
Such as:
Public Class Company
Public Property Name As String
Public Property CurrentEmployees As List(Of Employee)
<JsonIgnoreAttribute()>
Public Property PastEmployees As List(Of Employee)
End Class
Public Class Employee
Public Property FirstName As List(Of Name)
Public Property LastName As List(Of Name)
Public Property Current As Boolean
End Class
However I'm trying to avoid this since I have a number of these things in my real situation so I don't want to split up all lists into two that will require extensive code modification. Would be nice if it can be done just in the json serialization side.
Any help appreciated. Thanks!