Skip to content

Problem with cJSON_AddItem* functions #107

@dismirlian

Description

@dismirlian

Hi,

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions