Reputation: 1978
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!
Upvotes: 4
Views: 3546
Reputation: 1978
There seems to be no built in functionality in Json.NET that allows me to achieve what is required. I ended up adding a function within the Company class that "cleans up" undesired data before being serialized into json.
Using the previous example:
Public Class Company
Public Property Name As String
Public Property Employees As List(Of Employee)
' Before serializing, call this function
Public Function GetObjectToSerialize() As Company
' Clone the object
Dim cleanObj As Company = CType(Me.MemberwiseClone, Company)
If Me.Employees.Count > 0 Then
cleanObj.Employees = New List(Of Employee)
For Each empl As Employee In Me.Employees
' only include the current employees
If Not empl.Current Then
cleanObj.Employees.Add(empl)
End If
Next
End If
End Function
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
All I have to do now is whenever I'm about to serialize a Company object, I call the GetObjectToSerialize() function and serialize the returned object instead.
Dim objToSave As Company = companyObj.GetObjectToSerialize()
Dim json As String = JsonConvert.SerializeObject(objToSave, Formatting.Indented)
Upvotes: 0
Reputation: 1451
Json.Net supports conditional serialization. Check the below link for the implementation
http://james.newtonking.com/projects/json/help/html/ConditionalProperties.htm
Upvotes: 1