You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a problem with the cJSON_Add[*]ToObject functions.
For example, when using the cJSON_AddStringToObject macro (which uses cJSON_AddItemToObject), I can't be sure that it succeeded.
The problem is that cJSON_CreateString returns NULL in case of error, but cJSON_AddItemToObject ignores the NULL item without any action. My suggestion (in case someone is interested!) would be to add a return value to cJSON_AddItemToObject and similar functions (for example, let cJSON_AddItemToObject return item). In this case, I can write this code:
if (!cJSON_AddStringToObject(obj, "nombre", "pepito")) {
... failed, do something ...
}
Bottom line, the return value of cJSON_Create[*] is thrown away, and the library's user is never informed if the operations succeeded or not. What do you think?
Regards,
Diego.
The text was updated successfully, but these errors were encountered:
Sorry, I can't do this right now because it would break the API, but I will definitely put this in the queue for version 2. (although I still can't give any estimate on when that will be, might take a while).
Hi Max, thanks for your quick answer. I think this could be added in version 1.x, because it only requires a change from return void to return cJSON*. The existing code wouldn't need any changes because it already ignores the return value from cJSON_Add.... .
As an added benefit, if the compiler warns about unused return values, the usage of these functions will be flagged as potential problems.
Hi,
I have a problem with the
cJSON_Add[*]ToObject
functions.For example, when using the
cJSON_AddStringToObject
macro (which usescJSON_AddItemToObject
), I can't be sure that it succeeded.The problem is that
cJSON_CreateString
returns NULL in case of error, butcJSON_AddItemToObject
ignores the NULLitem
without any action. My suggestion (in case someone is interested!) would be to add a return value tocJSON_AddItemToObject
and similar functions (for example, letcJSON_AddItemToObject
returnitem
). In this case, I can write this code:Bottom line, the return value of
cJSON_Create[*]
is thrown away, and the library's user is never informed if the operations succeeded or not. What do you think?Regards,
Diego.
The text was updated successfully, but these errors were encountered: