Why adding (char*)0 to JsonArray returns false?
Created by: dyarkovoy
According to the docs, JsonVariant::add() returns
-
trueif the value was successfully added. -
falseif there was not enough memory in the JsonDocument.
However, when I do array.add((char*)0), the returned value is always false, even if the new slot was added successfully. I traced it down to this piece in VariantData.hpp:
template <typename TStoragePolicy>
bool setString(const char *s, TStoragePolicy storage_policy) {
if (s) {
setString(make_not_null(s), storage_policy);
return true;
} else {
setType(VALUE_IS_NULL);
return false;
}
}
I guess this assumes that if s==nullptr, then we could not allocate mem somewhere earlier, But what if we purposefully pass nullptr?
This may seem like a minor glitch, but my use case is to make sure there's always enough space in the doc when populating it, so I check the return of every set() and add() and throw if any returns false, to preserve document consistency.
Maybe there's anoter [proper] way to assign null to a JsonVariant?
If I'm not overlooking something and this is a glitch indeed, then it raises another question, maybe we should have a special-case-null-variant static, or separate setNull() / addNull() methods, like the isNull() we have already?