]> git.notmuchmail.org Git - notmuch/blob - sprinter-json.c
NEWS: notmuch search --format=text0
[notmuch] / sprinter-json.c
1 #include <stdbool.h>
2 #include <stdio.h>
3 #include <talloc.h>
4 #include "sprinter.h"
5
6 struct sprinter_json {
7     struct sprinter vtable;
8     FILE *stream;
9     /* Top of the state stack, or NULL if the printer is not currently
10      * inside any aggregate types. */
11     struct json_state *state;
12
13     /* A flag to signify that a separator should be inserted in the
14      * output as soon as possible.
15      */
16     notmuch_bool_t insert_separator;
17 };
18
19 struct json_state {
20     struct json_state *parent;
21     /* True if nothing has been printed in this aggregate yet.
22      * Suppresses the comma before a value. */
23     notmuch_bool_t first;
24     /* The character that closes the current aggregate. */
25     char close;
26 };
27
28 /* Helper function to set up the stream to print a value.  If this
29  * value follows another value, prints a comma. */
30 static struct sprinter_json *
31 json_begin_value (struct sprinter *sp)
32 {
33     struct sprinter_json *spj = (struct sprinter_json *) sp;
34
35     if (spj->state) {
36         if (! spj->state->first) {
37             fputc (',', spj->stream);
38             if (spj->insert_separator) {
39                 fputc ('\n', spj->stream);
40                 spj->insert_separator = FALSE;
41             } else {
42                 fputc (' ', spj->stream);
43             }
44         } else {
45             spj->state->first = FALSE;
46         }
47     }
48     return spj;
49 }
50
51 /* Helper function to begin an aggregate type.  Prints the open
52  * character and pushes a new state frame. */
53 static void
54 json_begin_aggregate (struct sprinter *sp, char open, char close)
55 {
56     struct sprinter_json *spj = json_begin_value (sp);
57     struct json_state *state = talloc (spj, struct json_state);
58
59     fputc (open, spj->stream);
60     state->parent = spj->state;
61     state->first = TRUE;
62     state->close = close;
63     spj->state = state;
64 }
65
66 static void
67 json_begin_map (struct sprinter *sp)
68 {
69     json_begin_aggregate (sp, '{', '}');
70 }
71
72 static void
73 json_begin_list (struct sprinter *sp)
74 {
75     json_begin_aggregate (sp, '[', ']');
76 }
77
78 static void
79 json_end (struct sprinter *sp)
80 {
81     struct sprinter_json *spj = (struct sprinter_json *) sp;
82     struct json_state *state = spj->state;
83
84     fputc (spj->state->close, spj->stream);
85     spj->state = state->parent;
86     talloc_free (state);
87     if (spj->state == NULL)
88         fputc ('\n', spj->stream);
89 }
90
91 /* This implementation supports embedded NULs as allowed by the JSON
92  * specification and Unicode.  Support for *parsing* embedded NULs
93  * varies, but is generally not a problem outside of C-based parsers
94  * (Python's json module and Emacs' json.el take embedded NULs in
95  * stride). */
96 static void
97 json_string_len (struct sprinter *sp, const char *val, size_t len)
98 {
99     static const char *const escapes[] = {
100         ['\"'] = "\\\"", ['\\'] = "\\\\", ['\b'] = "\\b",
101         ['\f'] = "\\f",  ['\n'] = "\\n",  ['\t'] = "\\t"
102     };
103     struct sprinter_json *spj = json_begin_value (sp);
104
105     fputc ('"', spj->stream);
106     for (; len; ++val, --len) {
107         unsigned char ch = *val;
108         if (ch < ARRAY_SIZE (escapes) && escapes[ch])
109             fputs (escapes[ch], spj->stream);
110         else if (ch >= 32)
111             fputc (ch, spj->stream);
112         else
113             fprintf (spj->stream, "\\u%04x", ch);
114     }
115     fputc ('"', spj->stream);
116 }
117
118 static void
119 json_string (struct sprinter *sp, const char *val)
120 {
121     if (val == NULL)
122         val = "";
123     json_string_len (sp, val, strlen (val));
124 }
125
126 static void
127 json_integer (struct sprinter *sp, int val)
128 {
129     struct sprinter_json *spj = json_begin_value (sp);
130
131     fprintf (spj->stream, "%d", val);
132 }
133
134 static void
135 json_boolean (struct sprinter *sp, notmuch_bool_t val)
136 {
137     struct sprinter_json *spj = json_begin_value (sp);
138
139     fputs (val ? "true" : "false", spj->stream);
140 }
141
142 static void
143 json_null (struct sprinter *sp)
144 {
145     struct sprinter_json *spj = json_begin_value (sp);
146
147     fputs ("null", spj->stream);
148 }
149
150 static void
151 json_map_key (struct sprinter *sp, const char *key)
152 {
153     struct sprinter_json *spj = (struct sprinter_json *) sp;
154
155     json_string (sp, key);
156     fputs (": ", spj->stream);
157     spj->state->first = TRUE;
158 }
159
160 static void
161 json_set_prefix (unused (struct sprinter *sp), unused (const char *name))
162 {
163 }
164
165 static void
166 json_separator (struct sprinter *sp)
167 {
168     struct sprinter_json *spj = (struct sprinter_json *) sp;
169
170     spj->insert_separator = TRUE;
171 }
172
173 struct sprinter *
174 sprinter_json_create (const void *ctx, FILE *stream)
175 {
176     static const struct sprinter_json template = {
177         .vtable = {
178             .begin_map = json_begin_map,
179             .begin_list = json_begin_list,
180             .end = json_end,
181             .string = json_string,
182             .string_len = json_string_len,
183             .integer = json_integer,
184             .boolean = json_boolean,
185             .null = json_null,
186             .map_key = json_map_key,
187             .separator = json_separator,
188             .set_prefix = json_set_prefix,
189             .is_text_printer = FALSE,
190         }
191     };
192     struct sprinter_json *res;
193
194     res = talloc (ctx, struct sprinter_json);
195     if (! res)
196         return NULL;
197
198     *res = template;
199     res->stream = stream;
200     return &res->vtable;
201 }