Serializing a conditional object in JSON.NET

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!

+4
source share
2 answers

Json.NET has no built-in functionality that allows me to achieve what is required. I have completed adding a function in the Company class that cleans up unwanted data before serializing in 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 is whenever I am going to serialize the 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) 
0
source

Json.Net supports conditional serialization. Check the link below for implementation

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

+1
source

Source: https://habr.com/ru/post/1481283/


All Articles