]> git.notmuchmail.org Git - notmuch/blobdiff - sprinter-json.c
Makefile.global: drop -std=gnu99. C11 (or later) compiler required
[notmuch] / sprinter-json.c
index 4649655179b25b71512504806f1977c4747a7b9c..273bdeca3d736dcb16f90acdf4ee6377efb281d0 100644 (file)
@@ -1,3 +1,4 @@
+#include <inttypes.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <talloc.h>
@@ -13,14 +14,14 @@ struct sprinter_json {
     /* A flag to signify that a separator should be inserted in the
      * output as soon as possible.
      */
-    notmuch_bool_t insert_separator;
+    bool insert_separator;
 };
 
 struct json_state {
     struct json_state *parent;
     /* True if nothing has been printed in this aggregate yet.
      * Suppresses the comma before a value. */
-    notmuch_bool_t first;
+    bool first;
     /* The character that closes the current aggregate. */
     char close;
 };
@@ -37,12 +38,12 @@ json_begin_value (struct sprinter *sp)
            fputc (',', spj->stream);
            if (spj->insert_separator) {
                fputc ('\n', spj->stream);
-               spj->insert_separator = FALSE;
+               spj->insert_separator = false;
            } else {
                fputc (' ', spj->stream);
            }
        } else {
-           spj->state->first = FALSE;
+           spj->state->first = false;
        }
     }
     return spj;
@@ -58,7 +59,7 @@ json_begin_aggregate (struct sprinter *sp, char open, char close)
 
     fputc (open, spj->stream);
     state->parent = spj->state;
-    state->first = TRUE;
+    state->first = true;
     state->close = close;
     spj->state = state;
 }
@@ -88,8 +89,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 +104,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);
@@ -111,15 +117,23 @@ json_string (struct sprinter *sp, const char *val)
 }
 
 static void
-json_integer (struct sprinter *sp, int val)
+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, int64_t val)
 {
     struct sprinter_json *spj = json_begin_value (sp);
 
-    fprintf (spj->stream, "%d", val);
+    fprintf (spj->stream, "%"PRId64, val);
 }
 
 static void
-json_boolean (struct sprinter *sp, notmuch_bool_t val)
+json_boolean (struct sprinter *sp, bool val)
 {
     struct sprinter_json *spj = json_begin_value (sp);
 
@@ -141,7 +155,7 @@ json_map_key (struct sprinter *sp, const char *key)
 
     json_string (sp, key);
     fputs (": ", spj->stream);
-    spj->state->first = TRUE;
+    spj->state->first = true;
 }
 
 static void
@@ -154,7 +168,7 @@ json_separator (struct sprinter *sp)
 {
     struct sprinter_json *spj = (struct sprinter_json *) sp;
 
-    spj->insert_separator = TRUE;
+    spj->insert_separator = true;
 }
 
 struct sprinter *
@@ -166,13 +180,14 @@ 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,
            .map_key = json_map_key,
            .separator = json_separator,
            .set_prefix = json_set_prefix,
-           .is_text_printer = FALSE,
+           .is_text_printer = false,
        }
     };
     struct sprinter_json *res;