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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
/*-------------------------------------------------------------------------
*
* test_json_parser_incremental.c
* Test program for incremental JSON parser
*
* Copyright (c) 2024-2025, PostgreSQL Global Development Group
*
* IDENTIFICATION
* src/test/modules/test_json_parser/test_json_parser_incremental.c
*
* This program tests incremental parsing of json. The input is fed into
* the parser in very small chunks. In practice you would normally use
* much larger chunks, but doing this makes it more likely that the
* full range of increment handling, especially in the lexer, is exercised.
* If the "-c SIZE" option is provided, that chunk size is used instead
* of the default of 60.
*
* If the -s flag is given, the program does semantic processing. This should
* just mirror back the json, albeit with white space changes.
*
* If the -o flag is given, the JSONLEX_CTX_OWNS_TOKENS flag is set. (This can
* be used in combination with a leak sanitizer; without the option, the parser
* may leak memory with invalid JSON.)
*
* The argument specifies the file containing the JSON input.
*
*-------------------------------------------------------------------------
*/
#include "postgres_fe.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "common/jsonapi.h"
#include "common/logging.h"
#include "lib/stringinfo.h"
#include "mb/pg_wchar.h"
#include "pg_getopt.h"
#define BUFSIZE 6000
#define DEFAULT_CHUNK_SIZE 60
typedef struct DoState
{
JsonLexContext *lex;
bool elem_is_first;
StringInfo buf;
} DoState;
static void usage(const char *progname);
static void escape_json(StringInfo buf, const char *str);
/* semantic action functions for parser */
static JsonParseErrorType do_object_start(void *state);
static JsonParseErrorType do_object_end(void *state);
static JsonParseErrorType do_object_field_start(void *state, char *fname, bool isnull);
static JsonParseErrorType do_object_field_end(void *state, char *fname, bool isnull);
static JsonParseErrorType do_array_start(void *state);
static JsonParseErrorType do_array_end(void *state);
static JsonParseErrorType do_array_element_start(void *state, bool isnull);
static JsonParseErrorType do_array_element_end(void *state, bool isnull);
static JsonParseErrorType do_scalar(void *state, char *token, JsonTokenType tokentype);
static JsonSemAction sem = {
.object_start = do_object_start,
.object_end = do_object_end,
.object_field_start = do_object_field_start,
.object_field_end = do_object_field_end,
.array_start = do_array_start,
.array_end = do_array_end,
.array_element_start = do_array_element_start,
.array_element_end = do_array_element_end,
.scalar = do_scalar
};
static bool lex_owns_tokens = false;
int
main(int argc, char **argv)
{
char buff[BUFSIZE];
FILE *json_file;
JsonParseErrorType result;
JsonLexContext lex;
StringInfoData json;
int n_read;
size_t chunk_size = DEFAULT_CHUNK_SIZE;
struct stat statbuf;
off_t bytes_left;
const JsonSemAction *testsem = &nullSemAction;
char *testfile;
int c;
bool need_strings = false;
int ret = 0;
pg_logging_init(argv[0]);
while ((c = getopt(argc, argv, "c:os")) != -1)
{
switch (c)
{
case 'c': /* chunksize */
chunk_size = strtou64(optarg, NULL, 10);
if (chunk_size > BUFSIZE)
pg_fatal("chunk size cannot exceed %d", BUFSIZE);
break;
case 'o': /* switch token ownership */
lex_owns_tokens = true;
break;
case 's': /* do semantic processing */
testsem = &sem;
sem.semstate = palloc(sizeof(struct DoState));
((struct DoState *) sem.semstate)->lex = &lex;
((struct DoState *) sem.semstate)->buf = makeStringInfo();
need_strings = true;
break;
}
}
if (optind < argc)
{
testfile = argv[optind];
optind++;
}
else
{
usage(argv[0]);
exit(1);
}
makeJsonLexContextIncremental(&lex, PG_UTF8, need_strings);
setJsonLexContextOwnsTokens(&lex, lex_owns_tokens);
initStringInfo(&json);
if ((json_file = fopen(testfile, PG_BINARY_R)) == NULL)
pg_fatal("error opening input: %m");
if (fstat(fileno(json_file), &statbuf) != 0)
pg_fatal("error statting input: %m");
bytes_left = statbuf.st_size;
for (;;)
{
/* We will break when there's nothing left to read */
if (bytes_left < chunk_size)
chunk_size = bytes_left;
n_read = fread(buff, 1, chunk_size, json_file);
if (n_read < chunk_size)
pg_fatal("error reading input file: %d", ferror(json_file));
appendBinaryStringInfo(&json, buff, n_read);
/*
* Append some trailing junk to the buffer passed to the parser. This
* helps us ensure that the parser does the right thing even if the
* chunk isn't terminated with a '\0'.
*/
appendStringInfoString(&json, "1+23 trailing junk");
bytes_left -= n_read;
if (bytes_left > 0)
{
result = pg_parse_json_incremental(&lex, testsem,
json.data, n_read,
false);
if (result != JSON_INCOMPLETE)
{
fprintf(stderr, "%s\n", json_errdetail(result, &lex));
ret = 1;
goto cleanup;
}
resetStringInfo(&json);
}
else
{
result = pg_parse_json_incremental(&lex, testsem,
json.data, n_read,
true);
if (result != JSON_SUCCESS)
{
fprintf(stderr, "%s\n", json_errdetail(result, &lex));
ret = 1;
goto cleanup;
}
if (!need_strings)
printf("SUCCESS!\n");
break;
}
}
cleanup:
fclose(json_file);
freeJsonLexContext(&lex);
free(json.data);
return ret;
}
/*
* The semantic routines here essentially just output the same json, except
* for white space. We could pretty print it but there's no need for our
* purposes. The result should be able to be fed to any JSON processor
* such as jq for validation.
*/
static JsonParseErrorType
do_object_start(void *state)
{
DoState *_state = (DoState *) state;
printf("{\n");
_state->elem_is_first = true;
return JSON_SUCCESS;
}
static JsonParseErrorType
do_object_end(void *state)
{
DoState *_state = (DoState *) state;
printf("\n}\n");
_state->elem_is_first = false;
return JSON_SUCCESS;
}
static JsonParseErrorType
do_object_field_start(void *state, char *fname, bool isnull)
{
DoState *_state = (DoState *) state;
if (!_state->elem_is_first)
printf(",\n");
resetStringInfo(_state->buf);
escape_json(_state->buf, fname);
printf("%s: ", _state->buf->data);
_state->elem_is_first = false;
return JSON_SUCCESS;
}
static JsonParseErrorType
do_object_field_end(void *state, char *fname, bool isnull)
{
if (!lex_owns_tokens)
free(fname);
return JSON_SUCCESS;
}
static JsonParseErrorType
do_array_start(void *state)
{
DoState *_state = (DoState *) state;
printf("[\n");
_state->elem_is_first = true;
return JSON_SUCCESS;
}
static JsonParseErrorType
do_array_end(void *state)
{
DoState *_state = (DoState *) state;
printf("\n]\n");
_state->elem_is_first = false;
return JSON_SUCCESS;
}
static JsonParseErrorType
do_array_element_start(void *state, bool isnull)
{
DoState *_state = (DoState *) state;
if (!_state->elem_is_first)
printf(",\n");
_state->elem_is_first = false;
return JSON_SUCCESS;
}
static JsonParseErrorType
do_array_element_end(void *state, bool isnull)
{
/* nothing to do */
return JSON_SUCCESS;
}
static JsonParseErrorType
do_scalar(void *state, char *token, JsonTokenType tokentype)
{
DoState *_state = (DoState *) state;
if (tokentype == JSON_TOKEN_STRING)
{
resetStringInfo(_state->buf);
escape_json(_state->buf, token);
printf("%s", _state->buf->data);
}
else
printf("%s", token);
if (!lex_owns_tokens)
free(token);
return JSON_SUCCESS;
}
/* copied from backend code */
static void
escape_json(StringInfo buf, const char *str)
{
const char *p;
appendStringInfoCharMacro(buf, '"');
for (p = str; *p; p++)
{
switch (*p)
{
case '\b':
appendStringInfoString(buf, "\\b");
break;
case '\f':
appendStringInfoString(buf, "\\f");
break;
case '\n':
appendStringInfoString(buf, "\\n");
break;
case '\r':
appendStringInfoString(buf, "\\r");
break;
case '\t':
appendStringInfoString(buf, "\\t");
break;
case '"':
appendStringInfoString(buf, "\\\"");
break;
case '\\':
appendStringInfoString(buf, "\\\\");
break;
default:
if ((unsigned char) *p < ' ')
appendStringInfo(buf, "\\u%04x", (int) *p);
else
appendStringInfoCharMacro(buf, *p);
break;
}
}
appendStringInfoCharMacro(buf, '"');
}
static void
usage(const char *progname)
{
fprintf(stderr, "Usage: %s [OPTION ...] testfile\n", progname);
fprintf(stderr, "Options:\n");
fprintf(stderr, " -c chunksize size of piece fed to parser (default 64)\n");
fprintf(stderr, " -o set JSONLEX_CTX_OWNS_TOKENS for leak checking\n");
fprintf(stderr, " -s do semantic processing\n");
}
|