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-2020, 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
20 {
35 
36 typedef enum
37 {
57 
58 
59 /*
60  * All the fields in this structure should be treated as read-only.
61  *
62  * If strval is not null, then it should contain the de-escaped value
63  * of the lexeme if it's a string. Otherwise most of these field names
64  * should be self-explanatory.
65  *
66  * line_number and line_start are principally for use by the parser's
67  * error reporting routines.
68  * token_terminator and prev_token_terminator point to the character
69  * AFTER the end of the token, i.e. where there would be a nul byte
70  * if we were using nul-terminated strings.
71  */
72 typedef struct JsonLexContext
73 {
74  char *input;
77  char *token_start;
81  int lex_level;
83  char *line_start;
86 
87 typedef void (*json_struct_action) (void *state);
88 typedef void (*json_ofield_action) (void *state, char *fname, bool isnull);
89 typedef void (*json_aelem_action) (void *state, bool isnull);
90 typedef void (*json_scalar_action) (void *state, char *token, JsonTokenType tokentype);
91 
92 
93 /*
94  * Semantic Action structure for use in parsing json.
95  * Any of these actions can be NULL, in which case nothing is done at that
96  * point, Likewise, semstate can be NULL. Using an all-NULL structure amounts
97  * to doing a pure parse with no side-effects, and is therefore exactly
98  * what the json input routines do.
99  *
100  * The 'fname' and 'token' strings passed to these actions are palloc'd.
101  * They are not free'd or used further by the parser, so the action function
102  * is free to do what it wishes with them.
103  */
104 typedef struct JsonSemAction
105 {
106  void *semstate;
116 } JsonSemAction;
117 
118 /*
119  * pg_parse_json will parse the string in the lex calling the
120  * action functions in sem at the appropriate points. It is
121  * up to them to keep what state they need in semstate. If they
122  * need access to the state of the lexer, then its pointer
123  * should be passed to them as a member of whatever semstate
124  * points to. If the action pointers are NULL the parser
125  * does nothing and just continues.
126  */
128  JsonSemAction *sem);
129 
130 /* the null action object used for pure validation */
132 
133 /*
134  * json_count_array_elements performs a fast secondary parse to determine the
135  * number of elements in passed array lex context. It should be called from an
136  * array_start action.
137  *
138  * The return value indicates whether any error occurred, while the number
139  * of elements is stored into *elements (but only if the return value is
140  * JSON_SUCCESS).
141  */
143  int *elements);
144 
145 /*
146  * constructor for JsonLexContext, with or without strval element.
147  * If supplied, the strval element will contain a de-escaped version of
148  * the lexeme. However, doing this imposes a performance penalty, so
149  * it should be avoided if the de-escaped lexeme is not required.
150  */
152  int len,
153  int encoding,
154  bool need_escapes);
155 
156 /* lex one token */
158 
159 /* construct an error detail string for a json error */
161 
162 /*
163  * Utility function to check if a string is a valid JSON number.
164  *
165  * str argument does not need to be nul-terminated.
166  */
167 extern bool IsValidJsonNumber(const char *str, int len);
168 
169 #endif /* JSONAPI_H */
json_struct_action array_end
Definition: jsonapi.h:110
int line_number
Definition: jsonapi.h:82
void(* json_scalar_action)(void *state, char *token, JsonTokenType tokentype)
Definition: jsonapi.h:90
static void error(void)
Definition: sql-dyntest.c:147
json_struct_action object_end
Definition: jsonapi.h:108
JsonParseErrorType json_count_array_elements(JsonLexContext *lex, int *elements)
Definition: jsonapi.c:219
JsonTokenType token_type
Definition: jsonapi.h:80
json_struct_action object_start
Definition: jsonapi.h:107
char * prev_token_terminator
Definition: jsonapi.h:79
json_scalar_action scalar
Definition: jsonapi.h:115
char * json_errdetail(JsonParseErrorType error, JsonLexContext *lex)
Definition: jsonapi.c:1056
int lex_level
Definition: jsonapi.h:81
void(* json_ofield_action)(void *state, char *fname, bool isnull)
Definition: jsonapi.h:88
char * line_start
Definition: jsonapi.h:83
JsonSemAction nullSemAction
Definition: jsonapi.c:67
int input_length
Definition: jsonapi.h:75
int input_encoding
Definition: jsonapi.h:76
StringInfo strval
Definition: jsonapi.h:84
json_ofield_action object_field_end
Definition: jsonapi.h:112
char * token_start
Definition: jsonapi.h:77
JsonLexContext * makeJsonLexContextCstringLen(char *json, int len, int encoding, bool need_escapes)
Definition: jsonapi.c:155
char * token_terminator
Definition: jsonapi.h:78
JsonParseErrorType
Definition: jsonapi.h:36
json_aelem_action array_element_start
Definition: jsonapi.h:113
JsonParseErrorType json_lex(JsonLexContext *lex)
Definition: jsonapi.c:526
char * input
Definition: jsonapi.h:74
json_struct_action array_start
Definition: jsonapi.h:109
Definition: regguts.h:298
void(* json_struct_action)(void *state)
Definition: jsonapi.h:87
int32 encoding
Definition: pg_database.h:41
JsonParseErrorType pg_parse_json(JsonLexContext *lex, JsonSemAction *sem)
Definition: jsonapi.c:179
void(* json_aelem_action)(void *state, bool isnull)
Definition: jsonapi.h:89
struct JsonSemAction JsonSemAction
struct JsonLexContext JsonLexContext
json_ofield_action object_field_start
Definition: jsonapi.h:111
void * semstate
Definition: jsonapi.h:106
bool IsValidJsonNumber(const char *str, int len)
Definition: jsonapi.c:115
json_aelem_action array_element_end
Definition: jsonapi.h:114
JsonTokenType
Definition: jsonapi.h:19