VB.NET – Conditional Objects in JSON.NET

I have been trying to figure out a way to ignore that certain objects are serialized based on certain conditions. I can only find how to use the ShouldSerialize * method to ignore the properties of the object, not how to ignore The whole object.

This is an example to explain my situation. A company can have multiple employees, and employees can be 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 are serialized as json.

The only way I can think of now is to separate current and non-current employees into two attributes, so that I can use for non-current ones.

Such as:

Public Class Company
Public Property Name As String
Public Property CurrentEmployees As List(Of Employee)

Public Property PastEmployees As List(Of Employee)
End Class

Public Class Employee
Public Property FirstName As List(Of Name)
Public Property LastName A s List(Of Name)
Public Property Current As Boolean
End Class

But I try to avoid this because I have a lot of such things in actual situations so I don’t want to Divide all the lists into two lists that require a lot of code modification. It would be great if it can only be done in terms of json serialization.

Any help is appreciated. Thank you!

Json.Net supports conditional serialization.
Please check the following link to understand the implementation< p>

http://james.newtonking.com/projects/json/help/html/ConditionalProperties.htm

I have been trying to find out A way to ignore that certain objects are serialized based on certain conditions. I can only find how to use the ShouldSerialize* method to ignore the properties of the object, not how to ignore the entire object.

This is one An example to explain my situation. A company can have multiple employees, and employees can be 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 as json.

The only way I can think of now is to separate current and non-current employees into two attributes, so that I can use for non-current ones.

Such as:

Public Class Company
Public Property Name As String
Public Property CurrentEmployees As List(Of Employee)

Public Pr operty 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

But I try to avoid this, because I have a lot of such things in actual situations so I don’t want to split all the lists into two A list that requires a lot of code modification. It would be great if it can only be done in terms of json serialization.

Any help is appreciated. Thank you!

Json.Net supports conditional serialization.
Please check the following link for the implementation status

http:/ /james.newtonking.com/projects/json/help/html/ConditionalProperties.htm

Leave a Comment

Your email address will not be published.