I am trying to migrate from Newtonsoft.Json to System.Text.Json However, I ran into a problem since I was using DefaultContractResolver. My "custom" behaviour have these rules for property serialization:
- Skip property serialization if it is marked with ReadOnly attribute
- Skip property serialization in case of null (this is supported)
- Skip property serialization which would serialize into an empty object
Example:
class Car
{
[ReadOnly]
public string Id { get; set; }
public string Name { get; set; }
public Person Owner { get; set; }
}
class Person
{
[ReadOnly]
public string Id { get; set; }
public string Name { get; set; }
}
Now, imagine, we have this data if no rules would apply.
{
"Id":"1234",
"Name":"Skoda",
"Owner":{
"Id":"abcd",
"Name":null
}
}
Now, if I serialize the object, I would like to get this instead.
{
"Name":"Skoda"
}