I am trying to serializing a POCO object into an update request.
My unset values are serialized as null, which causes them to be updated to null.
If I set IgnoreNullValues to true this fixes the problem. However, then there is no way to explicitly update an attribute to null.
I am thinking that the approach to solving this problem is to create a custom null object that serializes to null and ignore the real null values. Is this possible? How else can I solve this problem?
Edit: Added example of POCO request object I am trying to serialize
internal class ContactCreateRequest
{
[JsonPropertyName("contact")]
public ContactEntity Contact { get; set; }
}
internal class ContactEntity
{
public string CompanyName { get; set; }
public DateTime? CreateDateTime { get; set; }
public string CreateId { get; set; }
public string Department { get; set; }
public string DisplayName { get; set; }
public string FirstName { get; set; }
public string GoesBy { get; set; }
public string Id { get; set; }
public string JobTitle { get; set; }
public DateTime? LastEditDateTime { get; set; }
public string LastEditId { get; set; }
public string LastName { get; set; }
public string ParentId { get; set; }
public string Title { get; set; }
public string UpdateAction { get; set; }
}
I would like the following
var request = new ContactCreateRequest
{
Contact = new ContactEntity
{
LastName = "Jane",
JobTitle = null,
UpdateAction = "Update"
}
}
{"contact": {"LastName": "Jane", "JobTitle": null, "UpdateAction": "Update"}}
... but of course this will not work because all the unset fields are also null so they treated the same as JobTitle. That is why I am wondering if I need to use a null object pattern to define that JobTitle should be set null explicitly.
PATCHREST request?{"contact":{"Department":"Marketing","Title":null}}... is that correct?