I tried to figure out how to ignore some of the objects serialized based on some conditions. All I can find is to ignore the properties of the object using the ShouldSerialize * method, but not how to ignore the whole object.
Here is an example explaining my situation. A company can have several employees, and 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 unjustified employees from serialization in json.
The only way I can think now is to separate the current and non-current employees from the two properties, so that I can just use <JsonIgnoreAttribute()> for long-term.
For instance:
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 try to avoid this, as I have several such things in my real situation, so I do not want to split all the lists into two, which will require extensive code modification. It would be nice if this could only be done in json serialization.
Any help appreciated. Thanks!
source share