aboutsummaryrefslogtreecommitdiffhomepage
path: root/sprinter-json.c
blob: c9b683530b08cb97d0d4689efe7d0a3fff1b5e84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <stdbool.h>
#include <stdio.h>
#include <talloc.h>
#include "sprinter.h"

struct sprinter_json {
    struct sprinter vtable;
    FILE *stream;
    /* Top of the state stack, or NULL if the printer is not currently
     * inside any aggregate types. */
    struct json_state *state;

    /* A flag to signify that a separator should be inserted in the
     * output as soon as possible.
     */
    notmuch_bool_t 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;
    /* The character that closes the current aggregate. */
    char close;
};

/* Helper function to set up the stream to print a value.  If this
 * value follows another value, prints a comma. */
static struct sprinter_json *
json_begin_value (struct sprinter *sp)
{
    struct sprinter_json *spj = (struct sprinter_json *) sp;

    if (spj->state) {
	if (! spj->state->first) {
	    fputc (',', spj->stream);
	    if (spj->insert_separator) {
		fputc ('\n', spj->stream);
		spj->insert_separator = FALSE;
	    } else {
		fputc (' ', spj->stream);
	    }
	} else {
	    spj->state->first = FALSE;
	}
    }
    return spj;
}

/* Helper function to begin an aggregate type.  Prints the open
 * character and pushes a new state frame. */
static void
json_begin_aggregate (struct sprinter *sp, char open, char close)
{
    struct sprinter_json *spj = json_begin_value (sp);
    struct json_state *state = talloc (spj, struct json_state);

    fputc (open, spj->stream);
    state->parent = spj->state;
    state->first = TRUE;
    state->close = close;
    spj->state = state;
}

static void
json_begin_map (struct sprinter *sp)
{
    json_begin_aggregate (sp, '{', '}');
}

static void
json_begin_list (struct sprinter *sp)
{
    json_begin_aggregate (sp, '[', ']');
}

static void
json_end (struct sprinter *sp)
{
    struct sprinter_json *spj = (struct sprinter_json *) sp;
    struct json_state *state = spj->state;

    fputc (spj->state->close, spj->stream);
    spj->state = state->parent;
    talloc_free (state);
    if (spj->state == NULL)
	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_len (struct sprinter *sp, const char *val, size_t len)
{
    static const char *const escapes[] = {
	['\"'] = "\\\"", ['\\'] = "\\\\", ['\b'] = "\\b",
	['\f'] = "\\f",  ['\n'] = "\\n",  ['\t'] = "\\t"
    };
    struct sprinter_json *spj = json_begin_value (sp);

    fputc ('"', spj->stream);
    for (; len; ++val, --len) {
	unsigned char ch = *val;
	if (ch < ARRAY_SIZE (escapes) && escapes[ch])
	    fputs (escapes[ch], spj->stream);
	else if (ch >= 32)
	    fputc (ch, spj->stream);
	else
	    fprintf (spj->stream, "\\u%04x", ch);
    }
    fputc ('"', spj->stream);
}

static void
json_string (struct sprinter *sp, const char *val)
{
    json_string_len (sp, val, strlen (val));
}

static void
json_integer (struct sprinter *sp, int val)
{
    struct sprinter_json *spj = json_begin_value (sp);

    fprintf (spj->stream, "%d", val);
}

static void
json_boolean (struct sprinter *sp, notmuch_bool_t val)
{
    struct sprinter_json *spj = json_begin_value (sp);

    fputs (val ? "true" : "false", spj->stream);
}

static void
json_null (struct sprinter *sp)
{
    struct sprinter_json *spj = json_begin_value (sp);

    fputs ("null", spj->stream);
}

static void
json_map_key (struct sprinter *sp, const char *key)
{
    struct sprinter_json *spj = (struct sprinter_json *) sp;

    json_string (sp, key);
    fputs (": ", spj->stream);
    spj->state->first = TRUE;
}

static void
json_set_prefix (unused (struct sprinter *sp), unused (const char *name))
{
}

static void
json_separator (struct sprinter *sp)
{
    struct sprinter_json *spj = (struct sprinter_json *) sp;

    spj->insert_separator = TRUE;
}

struct sprinter *
sprinter_json_create (const void *ctx, FILE *stream)
{
    static const struct sprinter_json template = {
	.vtable = {
	    .begin_map = json_begin_map,
	    .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,
	}
    };
    struct sprinter_json *res;

    res = talloc (ctx, struct sprinter_json);
    if (! res)
	return NULL;

    *res = template;
    res->stream = stream;
    return &res->vtable;
}