1 /*-------------------------------------------------------------------------
4 * Declarations for JSON API support.
6 * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
9 * src/include/utils/jsonapi.h
11 *-------------------------------------------------------------------------
17 #include "lib/stringinfo.h"
24 JSON_TOKEN_OBJECT_START,
25 JSON_TOKEN_OBJECT_END,
26 JSON_TOKEN_ARRAY_START,
38 * All the fields in this structure should be treated as read-only.
40 * If strval is not null, then it should contain the de-escaped value
41 * of the lexeme if it's a string. Otherwise most of these field names
42 * should be self-explanatory.
44 * line_number and line_start are principally for use by the parser's
45 * error reporting routines.
46 * token_terminator and prev_token_terminator point to the character
47 * AFTER the end of the token, i.e. where there would be a nul byte
48 * if we were using nul-terminated strings.
50 typedef struct JsonLexContext
55 char *token_terminator;
56 char *prev_token_terminator;
57 JsonTokenType token_type;
64 typedef void (*json_struct_action) (void *state);
65 typedef void (*json_ofield_action) (void *state, char *fname, bool isnull);
66 typedef void (*json_aelem_action) (void *state, bool isnull);
67 typedef void (*json_scalar_action) (void *state, char *token, JsonTokenType tokentype);
71 * Semantic Action structure for use in parsing json.
72 * Any of these actions can be NULL, in which case nothing is done at that
73 * point, Likewise, semstate can be NULL. Using an all-NULL structure amounts
74 * to doing a pure parse with no side-effects, and is therefore exactly
75 * what the json input routines do.
77 typedef struct JsonSemAction
80 json_struct_action object_start;
81 json_struct_action object_end;
82 json_struct_action array_start;
83 json_struct_action array_end;
84 json_ofield_action object_field_start;
85 json_ofield_action object_field_end;
86 json_aelem_action array_element_start;
87 json_aelem_action array_element_end;
88 json_scalar_action scalar;
92 * parse_json will parse the string in the lex calling the
93 * action functions in sem at the appropriate points. It is
94 * up to them to keep what state they need in semstate. If they
95 * need access to the state of the lexer, then its pointer
96 * should be passed to them as a member of whatever semstate
97 * points to. If the action pointers are NULL the parser
98 * does nothing and just continues.
100 extern void pg_parse_json(JsonLexContext *lex, JsonSemAction *sem);
103 * constructor for JsonLexContext, with or without strval element.
104 * If supplied, the strval element will contain a de-escaped version of
105 * the lexeme. However, doing this imposes a performance penalty, so
106 * it should be avoided if the de-escaped lexeme is not required.
108 extern JsonLexContext *makeJsonLexContext(text *json, bool need_escapes);
110 #endif /* JSONAPI_H */