DaveGamble/cJSON

Feature Request: Support for Strings Containing `\0` Characters

Opened this issue · 1 comments

Thank you for maintaining cJSON, it has been very helpful in many of our projects!

However, I’ve encountered a limitation regarding how strings are handled within the library. As the documentation mentions, cJSON currently doesn't support strings that contain null characters (\0) because it relies on null-terminated strings.

This limitation makes it difficult to work with data that contains embedded null characters, especially when dealing with binary data or certain encodings. I understand that this might be a fundamental design choice due to how strings are managed in C, but I was wondering:

Are there any plans to support strings with null characters (\0) in future versions of cJSON?

If not, would there be any recommended approaches for handling such data within cJSON?
Having the ability to handle these strings, possibly through a function that allows specifying the string length explicitly (similar to strncpy), would greatly expand the library’s utility in various use cases where binary data is involved.

Thanks in advance for considering this request, and I look forward to your response!

Best regards

A \0 isn't really a valid string character. So to introduce it, as you suggested, you're really talking about binary data. According to the JSON specification, the minimum encoded value of a character in a JSON string is 0x20. If you want to pass an arbitrary buffer of bytes, you would need to pass it, for example, as a hex encoded ASCII string. So "hello\0world" would have to be: { "foo": "68656c6c6f00776f726c64" }. Or use some other encoding that ensures the string meets the JSON spec.