PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test_json_parser_incremental.c File Reference
#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"
Include dependency graph for test_json_parser_incremental.c:

Go to the source code of this file.

Data Structures

struct  DoState
 

Macros

#define BUFSIZE   6000
 
#define DEFAULT_CHUNK_SIZE   60
 

Typedefs

typedef struct DoState DoState
 

Functions

static void usage (const char *progname)
 
static void escape_json (StringInfo buf, const char *str)
 
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)
 
int main (int argc, char **argv)
 

Variables

static JsonSemAction sem
 
static bool lex_owns_tokens = false
 

Macro Definition Documentation

◆ BUFSIZE

#define BUFSIZE   6000

Definition at line 48 of file test_json_parser_incremental.c.

◆ DEFAULT_CHUNK_SIZE

#define DEFAULT_CHUNK_SIZE   60

Definition at line 49 of file test_json_parser_incremental.c.

Typedef Documentation

◆ DoState

typedef struct DoState DoState

Function Documentation

◆ do_array_element_end()

static JsonParseErrorType do_array_element_end ( void *  state,
bool  isnull 
)
static

Definition at line 330 of file test_json_parser_incremental.c.

331{
332 /* nothing to do */
333
334 return JSON_SUCCESS;
335}
@ JSON_SUCCESS
Definition: jsonapi.h:36

References JSON_SUCCESS.

◆ do_array_element_start()

static JsonParseErrorType do_array_element_start ( void *  state,
bool  isnull 
)
static

Definition at line 318 of file test_json_parser_incremental.c.

319{
320 DoState *_state = (DoState *) state;
321
322 if (!_state->elem_is_first)
323 printf(",\n");
324 _state->elem_is_first = false;
325
326 return JSON_SUCCESS;
327}
#define printf(...)
Definition: port.h:245
Definition: regguts.h:323

References DoState::elem_is_first, JSON_SUCCESS, and printf.

◆ do_array_end()

static JsonParseErrorType do_array_end ( void *  state)
static

Definition at line 307 of file test_json_parser_incremental.c.

308{
309 DoState *_state = (DoState *) state;
310
311 printf("\n]\n");
312 _state->elem_is_first = false;
313
314 return JSON_SUCCESS;
315}

References DoState::elem_is_first, JSON_SUCCESS, and printf.

◆ do_array_start()

static JsonParseErrorType do_array_start ( void *  state)
static

Definition at line 296 of file test_json_parser_incremental.c.

297{
298 DoState *_state = (DoState *) state;
299
300 printf("[\n");
301 _state->elem_is_first = true;
302
303 return JSON_SUCCESS;
304}

References DoState::elem_is_first, JSON_SUCCESS, and printf.

◆ do_object_end()

static JsonParseErrorType do_object_end ( void *  state)
static

Definition at line 261 of file test_json_parser_incremental.c.

262{
263 DoState *_state = (DoState *) state;
264
265 printf("\n}\n");
266 _state->elem_is_first = false;
267
268 return JSON_SUCCESS;
269}

References DoState::elem_is_first, JSON_SUCCESS, and printf.

◆ do_object_field_end()

static JsonParseErrorType do_object_field_end ( void *  state,
char *  fname,
bool  isnull 
)
static

Definition at line 287 of file test_json_parser_incremental.c.

288{
289 if (!lex_owns_tokens)
290 free(fname);
291
292 return JSON_SUCCESS;
293}
#define free(a)
Definition: header.h:65
static bool lex_owns_tokens

References free, JSON_SUCCESS, and lex_owns_tokens.

◆ do_object_field_start()

static JsonParseErrorType do_object_field_start ( void *  state,
char *  fname,
bool  isnull 
)
static

Definition at line 272 of file test_json_parser_incremental.c.

273{
274 DoState *_state = (DoState *) state;
275
276 if (!_state->elem_is_first)
277 printf(",\n");
278 resetStringInfo(_state->buf);
279 escape_json(_state->buf, fname);
280 printf("%s: ", _state->buf->data);
281 _state->elem_is_first = false;
282
283 return JSON_SUCCESS;
284}
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:126
static void escape_json(StringInfo buf, const char *str)

References DoState::buf, StringInfoData::data, DoState::elem_is_first, escape_json(), JSON_SUCCESS, printf, and resetStringInfo().

◆ do_object_start()

static JsonParseErrorType do_object_start ( void *  state)
static

Definition at line 250 of file test_json_parser_incremental.c.

251{
252 DoState *_state = (DoState *) state;
253
254 printf("{\n");
255 _state->elem_is_first = true;
256
257 return JSON_SUCCESS;
258}

References DoState::elem_is_first, JSON_SUCCESS, and printf.

◆ do_scalar()

static JsonParseErrorType do_scalar ( void *  state,
char *  token,
JsonTokenType  tokentype 
)
static

Definition at line 338 of file test_json_parser_incremental.c.

339{
340 DoState *_state = (DoState *) state;
341
342 if (tokentype == JSON_TOKEN_STRING)
343 {
344 resetStringInfo(_state->buf);
345 escape_json(_state->buf, token);
346 printf("%s", _state->buf->data);
347 }
348 else
349 printf("%s", token);
350
351 if (!lex_owns_tokens)
352 free(token);
353
354 return JSON_SUCCESS;
355}
@ JSON_TOKEN_STRING
Definition: jsonapi.h:20

References DoState::buf, StringInfoData::data, escape_json(), free, JSON_SUCCESS, JSON_TOKEN_STRING, lex_owns_tokens, printf, and resetStringInfo().

◆ escape_json()

static void escape_json ( StringInfo  buf,
const char *  str 
)
static

Definition at line 360 of file test_json_parser_incremental.c.

361{
362 const char *p;
363
365 for (p = str; *p; p++)
366 {
367 switch (*p)
368 {
369 case '\b':
371 break;
372 case '\f':
374 break;
375 case '\n':
377 break;
378 case '\r':
380 break;
381 case '\t':
383 break;
384 case '"':
386 break;
387 case '\\':
389 break;
390 default:
391 if ((unsigned char) *p < ' ')
392 appendStringInfo(buf, "\\u%04x", (int) *p);
393 else
395 break;
396 }
397 }
399}
const char * str
static char * buf
Definition: pg_test_fsync.c:72
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:145
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:230
#define appendStringInfoCharMacro(str, ch)
Definition: stringinfo.h:231

References appendStringInfo(), appendStringInfoCharMacro, appendStringInfoString(), buf, and str.

Referenced by do_object_field_start(), and do_scalar().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 87 of file test_json_parser_incremental.c.

88{
89 char buff[BUFSIZE];
90 FILE *json_file;
91 JsonParseErrorType result;
92 JsonLexContext *lex;
93 StringInfoData json;
94 int n_read;
95 size_t chunk_size = DEFAULT_CHUNK_SIZE;
96 bool run_chunk_ranges = false;
97 struct stat statbuf;
98 const JsonSemAction *testsem = &nullSemAction;
99 char *testfile;
100 int c;
101 bool need_strings = false;
102 int ret = 0;
103
104 pg_logging_init(argv[0]);
105
106 lex = calloc(1, sizeof(JsonLexContext));
107 if (!lex)
108 pg_fatal("out of memory");
109
110 while ((c = getopt(argc, argv, "r:c:os")) != -1)
111 {
112 switch (c)
113 {
114 case 'r': /* chunk range */
115 run_chunk_ranges = true;
116 /* fall through */
117 case 'c': /* chunk size */
118 chunk_size = strtou64(optarg, NULL, 10);
119 if (chunk_size > BUFSIZE)
120 pg_fatal("chunk size cannot exceed %d", BUFSIZE);
121 break;
122 case 'o': /* switch token ownership */
123 lex_owns_tokens = true;
124 break;
125 case 's': /* do semantic processing */
126 testsem = &sem;
127 sem.semstate = palloc(sizeof(struct DoState));
128 ((struct DoState *) sem.semstate)->lex = lex;
129 ((struct DoState *) sem.semstate)->buf = makeStringInfo();
130 need_strings = true;
131 break;
132 }
133 }
134
135 if (optind < argc)
136 {
137 testfile = argv[optind];
138 optind++;
139 }
140 else
141 {
142 usage(argv[0]);
143 exit(1);
144 }
145
146 initStringInfo(&json);
147
148 if ((json_file = fopen(testfile, PG_BINARY_R)) == NULL)
149 pg_fatal("error opening input: %m");
150
151 if (fstat(fileno(json_file), &statbuf) != 0)
152 pg_fatal("error statting input: %m");
153
154 do
155 {
156 /*
157 * This outer loop only repeats in -r mode. Reset the parse state and
158 * our position in the input file for the inner loop, which performs
159 * the incremental parsing.
160 */
161 off_t bytes_left = statbuf.st_size;
162 size_t to_read = chunk_size;
163
166
167 rewind(json_file);
168 resetStringInfo(&json);
169
170 for (;;)
171 {
172 /* We will break when there's nothing left to read */
173
174 if (bytes_left < to_read)
175 to_read = bytes_left;
176
177 n_read = fread(buff, 1, to_read, json_file);
178 if (n_read < to_read)
179 pg_fatal("error reading input file: %d", ferror(json_file));
180
181 appendBinaryStringInfo(&json, buff, n_read);
182
183 /*
184 * Append some trailing junk to the buffer passed to the parser.
185 * This helps us ensure that the parser does the right thing even
186 * if the chunk isn't terminated with a '\0'.
187 */
188 appendStringInfoString(&json, "1+23 trailing junk");
189 bytes_left -= n_read;
190 if (bytes_left > 0)
191 {
192 result = pg_parse_json_incremental(lex, testsem,
193 json.data, n_read,
194 false);
195 if (result != JSON_INCOMPLETE)
196 {
197 fprintf(stderr, "%s\n", json_errdetail(result, lex));
198 ret = 1;
199 goto cleanup;
200 }
201 resetStringInfo(&json);
202 }
203 else
204 {
205 result = pg_parse_json_incremental(lex, testsem,
206 json.data, n_read,
207 true);
208 if (result != JSON_SUCCESS)
209 {
210 fprintf(stderr, "%s\n", json_errdetail(result, lex));
211 ret = 1;
212 goto cleanup;
213 }
214 if (!need_strings)
215 printf("SUCCESS!\n");
216 break;
217 }
218 }
219
220cleanup:
222
223 /*
224 * In -r mode, separate output with nulls so that the calling test can
225 * split it up, decrement the chunk size, and loop back to the top.
226 * All other modes immediately fall out of the loop and exit.
227 */
228 if (run_chunk_ranges)
229 {
230 fputc('\0', stdout);
231 fputc('\0', stderr);
232 }
233 } while (run_chunk_ranges && (--chunk_size > 0));
234
235 fclose(json_file);
236 free(json.data);
237 free(lex);
238
239 return ret;
240}
static void cleanup(void)
Definition: bootstrap.c:715
#define PG_BINARY_R
Definition: c.h:1274
#define fprintf(file, fmt, msg)
Definition: cubescan.l:21
#define calloc(a, b)
Definition: header.h:55
JsonParseErrorType pg_parse_json_incremental(JsonLexContext *lex, const JsonSemAction *sem, const char *json, size_t len, bool is_last)
Definition: jsonapi.c:868
JsonLexContext * makeJsonLexContextIncremental(JsonLexContext *lex, int encoding, bool need_escapes)
Definition: jsonapi.c:497
void setJsonLexContextOwnsTokens(JsonLexContext *lex, bool owned_by_context)
Definition: jsonapi.c:542
const JsonSemAction nullSemAction
Definition: jsonapi.c:287
char * json_errdetail(JsonParseErrorType error, JsonLexContext *lex)
Definition: jsonapi.c:2404
void freeJsonLexContext(JsonLexContext *lex)
Definition: jsonapi.c:687
JsonParseErrorType
Definition: jsonapi.h:35
@ JSON_INCOMPLETE
Definition: jsonapi.h:37
void pg_logging_init(const char *argv0)
Definition: logging.c:83
void * palloc(Size size)
Definition: mcxt.c:1365
#define pg_fatal(...)
PGDLLIMPORT int optind
Definition: getopt.c:51
int getopt(int nargc, char *const *nargv, const char *ostr)
Definition: getopt.c:72
PGDLLIMPORT char * optarg
Definition: getopt.c:53
@ PG_UTF8
Definition: pg_wchar.h:232
char * c
StringInfo makeStringInfo(void)
Definition: stringinfo.c:72
void appendBinaryStringInfo(StringInfo str, const void *data, int datalen)
Definition: stringinfo.c:281
void initStringInfo(StringInfo str)
Definition: stringinfo.c:97
JsonLexContext * lex
void * semstate
Definition: jsonapi.h:153
static void usage(const char *progname)
static JsonSemAction sem
#define DEFAULT_CHUNK_SIZE
#define BUFSIZE
#define fstat
Definition: win32_port.h:273

References appendBinaryStringInfo(), appendStringInfoString(), BUFSIZE, calloc, cleanup(), StringInfoData::data, DEFAULT_CHUNK_SIZE, fprintf, free, freeJsonLexContext(), fstat, getopt(), initStringInfo(), json_errdetail(), JSON_INCOMPLETE, JSON_SUCCESS, DoState::lex, lex_owns_tokens, makeJsonLexContextIncremental(), makeStringInfo(), nullSemAction, optarg, optind, palloc(), PG_BINARY_R, pg_fatal, pg_logging_init(), pg_parse_json_incremental(), PG_UTF8, printf, resetStringInfo(), sem, JsonSemAction::semstate, setJsonLexContextOwnsTokens(), stat::st_size, generate_unaccent_rules::stdout, and usage().

◆ usage()

static void usage ( const char *  progname)
static

Definition at line 402 of file test_json_parser_incremental.c.

403{
404 fprintf(stderr, "Usage: %s [OPTION ...] testfile\n", progname);
405 fprintf(stderr, "Options:\n");
406 fprintf(stderr, " -c chunksize size of piece fed to parser (default 64)\n");
407 fprintf(stderr, " -o set JSONLEX_CTX_OWNS_TOKENS for leak checking\n");
408 fprintf(stderr, " -s do semantic processing\n");
409
410}
const char * progname
Definition: main.c:44

References fprintf, and progname.

Referenced by main().

Variable Documentation

◆ lex_owns_tokens

bool lex_owns_tokens = false
static

Definition at line 84 of file test_json_parser_incremental.c.

Referenced by do_object_field_end(), do_scalar(), and main().

◆ sem

JsonSemAction sem
static
Initial value:
= {
.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 JsonParseErrorType do_object_field_start(void *state, char *fname, bool isnull)
static JsonParseErrorType do_array_element_end(void *state, bool isnull)
static JsonParseErrorType do_array_element_start(void *state, bool isnull)
static JsonParseErrorType do_object_end(void *state)
static JsonParseErrorType do_scalar(void *state, char *token, JsonTokenType tokentype)
static JsonParseErrorType do_array_start(void *state)
static JsonParseErrorType do_object_start(void *state)
static JsonParseErrorType do_array_end(void *state)
static JsonParseErrorType do_object_field_end(void *state, char *fname, bool isnull)

Definition at line 72 of file test_json_parser_incremental.c.

Referenced by datum_to_jsonb_internal(), each_worker(), elements_worker(), get_json_object_as_hash(), get_worker(), handle_oauth_sasl_error(), iterate_json_values(), json_array_length(), json_object_keys(), json_parse_manifest(), json_strip_nulls(), jsonb_from_cstring(), main(), parse_array(), parse_array_element(), parse_oauth_json(), parse_object(), parse_object_field(), parse_scalar(), pg_parse_json(), pg_parse_json_incremental(), pg_parse_json_or_errsave(), populate_array_json(), populate_recordset_worker(), PosixSemaphoreCreate(), PosixSemaphoreKill(), test_gb18030_json(), and transform_json_string_values().