X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=blobdiff_plain;f=sprinter-json.c;h=0a077907cd83368c339c320968ef2b8a5e91f7d8;hp=4649655179b25b71512504806f1977c4747a7b9c;hb=3fed6736a7ef8b8b1f05d0fabb136bdd3b5917ee;hpb=36522fca1cac6ca23c2c4c0280e3e20e96f7bfbb;ds=sidebyside diff --git a/sprinter-json.c b/sprinter-json.c index 46496551..0a077907 100644 --- a/sprinter-json.c +++ b/sprinter-json.c @@ -88,8 +88,13 @@ json_end (struct sprinter *sp) fputc ('\n', spj->stream); } +/* This implementation supports embedded NULs as allowed by the JSON + * specification and Unicode. Support for *parsing* embedded NULs + * varies, but is generally not a problem outside of C-based parsers + * (Python's json module and Emacs' json.el take embedded NULs in + * stride). */ static void -json_string (struct sprinter *sp, const char *val) +json_string_len (struct sprinter *sp, const char *val, size_t len) { static const char *const escapes[] = { ['\"'] = "\\\"", ['\\'] = "\\\\", ['\b'] = "\\b", @@ -98,7 +103,7 @@ json_string (struct sprinter *sp, const char *val) struct sprinter_json *spj = json_begin_value (sp); fputc ('"', spj->stream); - for (; *val; ++val) { + for (; len; ++val, --len) { unsigned char ch = *val; if (ch < ARRAY_SIZE (escapes) && escapes[ch]) fputs (escapes[ch], spj->stream); @@ -110,6 +115,14 @@ json_string (struct sprinter *sp, const char *val) fputc ('"', spj->stream); } +static void +json_string (struct sprinter *sp, const char *val) +{ + if (val == NULL) + val = ""; + json_string_len (sp, val, strlen (val)); +} + static void json_integer (struct sprinter *sp, int val) { @@ -166,6 +179,7 @@ sprinter_json_create (const void *ctx, FILE *stream) .begin_list = json_begin_list, .end = json_end, .string = json_string, + .string_len = json_string_len, .integer = json_integer, .boolean = json_boolean, .null = json_null,