I saw in this question: Empty string becomes null when passed from Delphi to C# as a function argument that Delphi's empty string value in reality is just a null-pointer - which I understand the reasoning behind.
I do have an issue though as I am developing a Web API in Delphi and I am having trouble implementing a PATCH
endpoint and I wondered if anyone has had the same issue as me.
If i have a simple resource Person
which looks like this.
{
"firstName": "John",
"lastName": "Doe",
"age": 44
}
and simply want to change his lastName
property using a PATCH
document - I would sent a request that looks like this:
{
"lastName": "Smith"
}
Now - in my api, using Delphis System.JSON
library I would just check if the request has the firstName
and age
properties before setting them in the request handler which sets the properties in an intermediate object PersonDTO
, but later I have to map these values to the actual Person
instance - and here comes my issue:
When mapping between multiple objects I cannot tell if a string is empty because it was never set (and should be treated as null) or was explicitly set to ''
to remove a property from my resource - How do I circumvent this?
if personDTO.FirstName <> '' then
personObject.FirstName := personDTO.FirstName;
Edit: I have considered setting the strings to #0
in the DTO
's constructor to distinguish between null
and ''
but this is a large (1M line) code base, so I would prefer to find a robust generic way of handling these scenarios