Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with cJSON_AddItem* functions #107

Open
dismirlian opened this issue Feb 15, 2017 · 2 comments
Open

Problem with cJSON_AddItem* functions #107

dismirlian opened this issue Feb 15, 2017 · 2 comments
Milestone

Comments

@dismirlian
Copy link

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.

@FSMaxB
Copy link
Collaborator

FSMaxB commented Feb 15, 2017

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).

@FSMaxB FSMaxB added this to the 2.0.0 milestone Feb 15, 2017
@FSMaxB FSMaxB mentioned this issue Feb 15, 2017
11 tasks
@dismirlian
Copy link
Author

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.

Of course, it is not the neatest thing to do....

Diego.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants