On Tue, 02 May 2023 14:13:27 +0200, Claudio Jeker wrote:

> Add a json_do_string() a function to print a JSON string.
> This function does the needed encoding of control chars and escape chars.
> I skipped the optional encoding of the forward slash (/) since this is
> only needed if the json output is embedded in HTML/SGML/XML.
> People putting JSON into such documents need to pass this through an extra
> step.

Unless you can guarantee that other control characters cannot occur
you probably want to output them in unicode hex form.  For example,
something like:

default:
        const char hex[] = "0123456789abcdef";
        if (iscntrl(c)) {
                /* Escape control characters like \u0000 */
                if (!eb)
                        eb = fprintf(jsonfh, "\\u00%c%c", hex[c >> 4],
                            hex[c & 0x0f]) < 0;
        }

In this case you probably want to assign c as an unsigned char.
E.g.

while ((c = (unsigned char)*v++) != '\0') {
        ...
}

Or better yet just declare c as unsigned char instead of int.

 - todd

Reply via email to