cJSON
cJSON copied to clipboard
Return output length from cJSON_PrintPreallocated
Adjust cJSON_PrintPreallocated() to return a size_t that provides the length of the output string. On error, it will return 0; so legacy behavior of interpreting its return as a boolean will still work as expected. In addition, a NULL buffer and zero length can be passed in to get the length of what would be the resulting string, so the needed buffer size is this plus 5 (extra that cJSON needs for printing).
Any feedback on this? I think it could be useful...
This will be supper useful on all Print functions
This will be supper useful on all Print functions
Yeah, except it would unfortunately break the API of the other print functions. An additional size_t pointer argument could be added to the other print procedures to pass back the size (if pointer provided), but then we are substantially changing API or adding API.
This change can tweak the API of cJSON_PrintPreallocated to return the length instead of a bool because a valid JSON object (even an empty one) should print to at least 2 characters (open and close brace), therefore a value of zero can be interpreted as an error. This makes this tweak backwards compatible with the bool API, potentially with the small caveat of if someone is directly comparing the return value with literal TRUE (but porting such a case is straight forward).
Updated README.md. Also passes all tests:
~/Projects/cJSON/build/tests$ make test
Running tests...
Test project /home/bowe/Projects/cJSON/build/tests
Start 1: parse_examples
1/18 Test #1: parse_examples ................... Passed 0.65 sec
Start 2: parse_number
2/18 Test #2: parse_number ..................... Passed 0.57 sec
Start 3: parse_hex4
3/18 Test #3: parse_hex4 ....................... Passed 1.00 sec
Start 4: parse_string
4/18 Test #4: parse_string ..................... Passed 0.55 sec
Start 5: parse_array
5/18 Test #5: parse_array ...................... Passed 0.56 sec
Start 6: parse_object
6/18 Test #6: parse_object ..................... Passed 0.56 sec
Start 7: parse_value
7/18 Test #7: parse_value ...................... Passed 0.56 sec
Start 8: print_string
8/18 Test #8: print_string ..................... Passed 0.55 sec
Start 9: print_number
9/18 Test #9: print_number ..................... Passed 0.61 sec
Start 10: print_array
10/18 Test #10: print_array ...................... Passed 0.58 sec
Start 11: print_object
11/18 Test #11: print_object ..................... Passed 0.58 sec
Start 12: print_value
12/18 Test #12: print_value ...................... Passed 0.59 sec
Start 13: misc_tests
13/18 Test #13: misc_tests ....................... Passed 0.62 sec
Start 14: parse_with_opts
14/18 Test #14: parse_with_opts .................. Passed 0.55 sec
Start 15: compare_tests
15/18 Test #15: compare_tests .................... Passed 0.59 sec
Start 16: cjson_add
16/18 Test #16: cjson_add ........................ Passed 0.57 sec
Start 17: readme_examples
17/18 Test #17: readme_examples .................. Passed 0.60 sec
Start 18: minify_tests
18/18 Test #18: minify_tests ..................... Passed 0.55 sec
100% tests passed, 0 tests failed out of 18
Total Test time (real) = 10.86 sec
We can have a wrapper functions to strictly preserve the compatability and cJSON_PrintPreallocatedEx that reports the size
Is cJSON still maintained?