json-c / json-c

https://github.com/json-c/json-c is the official code repository for json-c. See the wiki for release tarballs for download. API docs at http://json-c.github.io/json-c/
https://github.com/json-c/json-c/wiki
Other
2.9k stars 1.05k forks source link

Allow NULL in json_object_new_string() and json_object_new_string_len() #851

Closed togamid closed 3 months ago

togamid commented 4 months ago

Currently, every time a nullable string field is added to a json_object, the user has to first test if the field is null and call json_object_new_string() or json_object_new_null() based on that.

Checking for NULL in json_object_new_string() and json_object_new_string_len() is a small change that would make the calling code cleaner and json-c more reliable.

hawicz commented 4 months ago

In what situation is it ok for what should be a string to be null instead? ISTM that that is more an indication that something has gone wrong and if we're checking for NULL then it should be as assert(s!=NULL) to trigger an intentional crash.

togamid commented 3 months ago

In my case I get a relatively large struct with multiple fields that can be either a string if they are applicable or NULL if they aren't.

hawicz commented 3 months ago

IMO adding this to the json-c API would encourage sloppy coding that would lead to much less reliability since accidentally passing NULL to these function would silently appear to work until the lack of a string object where there was supposed to be one caused problems later. I'm closing this without any changes to json-c.