PostgreSQL Source Code  git master
jsonapi.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * jsonapi.h
4  * Declarations for JSON API support.
5  *
6  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * src/include/common/jsonapi.h
10  *
11  *-------------------------------------------------------------------------
12  */
13 
14 #ifndef JSONAPI_H
15 #define JSONAPI_H
16 
17 #include "lib/stringinfo.h"
18 
19 typedef enum JsonTokenType
20 {
35 
36 typedef enum JsonParseErrorType
37 {
60  JSON_SEM_ACTION_FAILED, /* error should already be reported */
62 
63 /* Parser state private to jsonapi.c */
64 typedef struct JsonParserStack JsonParserStack;
66 
67 /*
68  * All the fields in this structure should be treated as read-only.
69  *
70  * If strval is not null, then it should contain the de-escaped value
71  * of the lexeme if it's a string. Otherwise most of these field names
72  * should be self-explanatory.
73  *
74  * line_number and line_start are principally for use by the parser's
75  * error reporting routines.
76  * token_terminator and prev_token_terminator point to the character
77  * AFTER the end of the token, i.e. where there would be a nul byte
78  * if we were using nul-terminated strings.
79  *
80  * The prev_token_terminator field should not be used when incremental is
81  * true, as the previous token might have started in a previous piece of input,
82  * and thus it can't be used in any pointer arithmetic or other operations in
83  * conjunction with token_start.
84  *
85  * JSONLEX_FREE_STRUCT/STRVAL are used to drive freeJsonLexContext.
86  */
87 #define JSONLEX_FREE_STRUCT (1 << 0)
88 #define JSONLEX_FREE_STRVAL (1 << 1)
89 typedef struct JsonLexContext
90 {
91  char *input;
94  char *token_start;
99  int lex_level;
101  int line_number; /* line number, starting from 1 */
102  char *line_start; /* where that line starts within input */
108 
110 typedef JsonParseErrorType (*json_ofield_action) (void *state, char *fname, bool isnull);
111 typedef JsonParseErrorType (*json_aelem_action) (void *state, bool isnull);
112 typedef JsonParseErrorType (*json_scalar_action) (void *state, char *token, JsonTokenType tokentype);
113 
114 
115 /*
116  * Semantic Action structure for use in parsing json.
117  *
118  * Any of these actions can be NULL, in which case nothing is done at that
119  * point, Likewise, semstate can be NULL. Using an all-NULL structure amounts
120  * to doing a pure parse with no side-effects, and is therefore exactly
121  * what the json input routines do.
122  *
123  * The 'fname' and 'token' strings passed to these actions are palloc'd.
124  * They are not free'd or used further by the parser, so the action function
125  * is free to do what it wishes with them.
126  *
127  * All action functions return JsonParseErrorType. If the result isn't
128  * JSON_SUCCESS, the parse is abandoned and that error code is returned.
129  * If it is JSON_SEM_ACTION_FAILED, the action function is responsible
130  * for having reported the error in some appropriate way.
131  */
132 typedef struct JsonSemAction
133 {
134  void *semstate;
145 
146 /*
147  * pg_parse_json will parse the string in the lex calling the
148  * action functions in sem at the appropriate points. It is
149  * up to them to keep what state they need in semstate. If they
150  * need access to the state of the lexer, then its pointer
151  * should be passed to them as a member of whatever semstate
152  * points to. If the action pointers are NULL the parser
153  * does nothing and just continues.
154  */
156  JsonSemAction *sem);
157 
160  char *json,
161  int len,
162  bool is_last);
163 
164 /* the null action object used for pure validation */
166 
167 /*
168  * json_count_array_elements performs a fast secondary parse to determine the
169  * number of elements in passed array lex context. It should be called from an
170  * array_start action.
171  *
172  * The return value indicates whether any error occurred, while the number
173  * of elements is stored into *elements (but only if the return value is
174  * JSON_SUCCESS).
175  */
177  int *elements);
178 
179 /*
180  * initializer for JsonLexContext.
181  *
182  * If a valid 'lex' pointer is given, it is initialized. This can be used
183  * for stack-allocated structs, saving overhead. If NULL is given, a new
184  * struct is allocated.
185  *
186  * If need_escapes is true, ->strval stores the unescaped lexemes.
187  * Unescaping is expensive, so only request it when necessary.
188  *
189  * If need_escapes is true or lex was given as NULL, then the caller is
190  * responsible for freeing the returned struct, either by calling
191  * freeJsonLexContext() or (in backend environment) via memory context
192  * cleanup.
193  */
195  char *json,
196  int len,
197  int encoding,
198  bool need_escapes);
199 
200 /*
201  * make a JsonLexContext suitable for incremental parsing.
202  * the string chunks will be handed to pg_parse_json_incremental,
203  * so there's no need for them here.
204  */
206  int encoding,
207  bool need_escapes);
208 
209 extern void freeJsonLexContext(JsonLexContext *lex);
210 
211 /* lex one token */
213 
214 /* construct an error detail string for a json error */
216 
217 /*
218  * Utility function to check if a string is a valid JSON number.
219  *
220  * str argument does not need to be nul-terminated.
221  */
222 extern bool IsValidJsonNumber(const char *str, int len);
223 
224 #endif /* JSONAPI_H */
#define PGDLLIMPORT
Definition: c.h:1316
uint32 bits32
Definition: c.h:515
const char * str
#define token
Definition: indent_globs.h:126
PGDLLIMPORT JsonSemAction nullSemAction
Definition: jsonapi.c:224
struct JsonLexContext JsonLexContext
JsonParseErrorType(* json_struct_action)(void *state)
Definition: jsonapi.h:109
JsonParseErrorType(* json_aelem_action)(void *state, bool isnull)
Definition: jsonapi.h:111
JsonParseErrorType pg_parse_json_incremental(JsonLexContext *lex, JsonSemAction *sem, char *json, int len, bool is_last)
Definition: jsonapi.c:649
JsonParseErrorType
Definition: jsonapi.h:37
@ JSON_SEM_ACTION_FAILED
Definition: jsonapi.h:60
@ JSON_EXPECTED_ARRAY_FIRST
Definition: jsonapi.h:44
@ JSON_EXPECTED_MORE
Definition: jsonapi.h:49
@ JSON_UNICODE_HIGH_SURROGATE
Definition: jsonapi.h:58
@ JSON_EXPECTED_COLON
Definition: jsonapi.h:46
@ JSON_EXPECTED_OBJECT_FIRST
Definition: jsonapi.h:50
@ JSON_UNICODE_CODE_POINT_ZERO
Definition: jsonapi.h:54
@ JSON_INVALID_LEXER_TYPE
Definition: jsonapi.h:40
@ JSON_EXPECTED_STRING
Definition: jsonapi.h:52
@ JSON_UNICODE_ESCAPE_FORMAT
Definition: jsonapi.h:55
@ JSON_SUCCESS
Definition: jsonapi.h:38
@ JSON_UNICODE_UNTRANSLATABLE
Definition: jsonapi.h:57
@ JSON_EXPECTED_OBJECT_NEXT
Definition: jsonapi.h:51
@ JSON_ESCAPING_REQUIRED
Definition: jsonapi.h:43
@ JSON_EXPECTED_JSON
Definition: jsonapi.h:48
@ JSON_INVALID_TOKEN
Definition: jsonapi.h:53
@ JSON_ESCAPING_INVALID
Definition: jsonapi.h:42
@ JSON_INCOMPLETE
Definition: jsonapi.h:39
@ JSON_EXPECTED_END
Definition: jsonapi.h:47
@ JSON_EXPECTED_ARRAY_NEXT
Definition: jsonapi.h:45
@ JSON_UNICODE_HIGH_ESCAPE
Definition: jsonapi.h:56
@ JSON_NESTING_TOO_DEEP
Definition: jsonapi.h:41
@ JSON_UNICODE_LOW_SURROGATE
Definition: jsonapi.h:59
JsonParseErrorType(* json_ofield_action)(void *state, char *fname, bool isnull)
Definition: jsonapi.h:110
char * json_errdetail(JsonParseErrorType error, JsonLexContext *lex)
Definition: jsonapi.c:2096
bool IsValidJsonNumber(const char *str, int len)
Definition: jsonapi.c:272
JsonParseErrorType pg_parse_json(JsonLexContext *lex, JsonSemAction *sem)
Definition: jsonapi.c:521
JsonTokenType
Definition: jsonapi.h:20
@ JSON_TOKEN_INVALID
Definition: jsonapi.h:21
@ JSON_TOKEN_COMMA
Definition: jsonapi.h:28
@ JSON_TOKEN_FALSE
Definition: jsonapi.h:31
@ JSON_TOKEN_END
Definition: jsonapi.h:33
@ JSON_TOKEN_TRUE
Definition: jsonapi.h:30
@ JSON_TOKEN_OBJECT_END
Definition: jsonapi.h:25
@ JSON_TOKEN_NULL
Definition: jsonapi.h:32
@ JSON_TOKEN_ARRAY_END
Definition: jsonapi.h:27
@ JSON_TOKEN_OBJECT_START
Definition: jsonapi.h:24
@ JSON_TOKEN_NUMBER
Definition: jsonapi.h:23
@ JSON_TOKEN_STRING
Definition: jsonapi.h:22
@ JSON_TOKEN_COLON
Definition: jsonapi.h:29
@ JSON_TOKEN_ARRAY_START
Definition: jsonapi.h:26
JsonParseErrorType json_lex(JsonLexContext *lex)
Definition: jsonapi.c:1308
JsonParseErrorType(* json_scalar_action)(void *state, char *token, JsonTokenType tokentype)
Definition: jsonapi.h:112
JsonLexContext * makeJsonLexContextCstringLen(JsonLexContext *lex, char *json, int len, int encoding, bool need_escapes)
Definition: jsonapi.c:326
JsonParseErrorType json_count_array_elements(JsonLexContext *lex, int *elements)
Definition: jsonapi.c:587
void freeJsonLexContext(JsonLexContext *lex)
Definition: jsonapi.c:482
JsonLexContext * makeJsonLexContextIncremental(JsonLexContext *lex, int encoding, bool need_escapes)
Definition: jsonapi.c:366
struct JsonSemAction JsonSemAction
const void size_t len
int32 encoding
Definition: pg_database.h:41
static void error(void)
Definition: sql-dyntest.c:147
char * input
Definition: jsonapi.h:91
bits32 flags
Definition: jsonapi.h:100
char * token_start
Definition: jsonapi.h:94
int input_encoding
Definition: jsonapi.h:93
StringInfo strval
Definition: jsonapi.h:105
char * token_terminator
Definition: jsonapi.h:95
char * prev_token_terminator
Definition: jsonapi.h:96
char * line_start
Definition: jsonapi.h:102
int lex_level
Definition: jsonapi.h:99
JsonParserStack * pstack
Definition: jsonapi.h:103
StringInfo errormsg
Definition: jsonapi.h:106
JsonIncrementalState * inc_state
Definition: jsonapi.h:104
int input_length
Definition: jsonapi.h:92
bool incremental
Definition: jsonapi.h:97
int line_number
Definition: jsonapi.h:101
JsonTokenType token_type
Definition: jsonapi.h:98
json_struct_action array_end
Definition: jsonapi.h:138
json_struct_action object_start
Definition: jsonapi.h:135
json_ofield_action object_field_start
Definition: jsonapi.h:139
json_aelem_action array_element_start
Definition: jsonapi.h:141
json_scalar_action scalar
Definition: jsonapi.h:143
void * semstate
Definition: jsonapi.h:134
json_aelem_action array_element_end
Definition: jsonapi.h:142
json_struct_action array_start
Definition: jsonapi.h:137
json_struct_action object_end
Definition: jsonapi.h:136
json_ofield_action object_field_end
Definition: jsonapi.h:140
Definition: regguts.h:323
JsonSemAction sem