PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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-2017, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * src/include/utils/jsonapi.h
10  *
11  *-------------------------------------------------------------------------
12  */
13 
14 #ifndef JSONAPI_H
15 #define JSONAPI_H
16 
17 #include "jsonb.h"
18 #include "lib/stringinfo.h"
19 
20 typedef enum
21 {
36 
37 
38 /*
39  * All the fields in this structure should be treated as read-only.
40  *
41  * If strval is not null, then it should contain the de-escaped value
42  * of the lexeme if it's a string. Otherwise most of these field names
43  * should be self-explanatory.
44  *
45  * line_number and line_start are principally for use by the parser's
46  * error reporting routines.
47  * token_terminator and prev_token_terminator point to the character
48  * AFTER the end of the token, i.e. where there would be a nul byte
49  * if we were using nul-terminated strings.
50  */
51 typedef struct JsonLexContext
52 {
53  char *input;
55  char *token_start;
59  int lex_level;
61  char *line_start;
64 
65 typedef void (*json_struct_action) (void *state);
66 typedef void (*json_ofield_action) (void *state, char *fname, bool isnull);
67 typedef void (*json_aelem_action) (void *state, bool isnull);
68 typedef void (*json_scalar_action) (void *state, char *token, JsonTokenType tokentype);
69 
70 
71 /*
72  * Semantic Action structure for use in parsing json.
73  * Any of these actions can be NULL, in which case nothing is done at that
74  * point, Likewise, semstate can be NULL. Using an all-NULL structure amounts
75  * to doing a pure parse with no side-effects, and is therefore exactly
76  * what the json input routines do.
77  *
78  * The 'fname' and 'token' strings passed to these actions are palloc'd.
79  * They are not free'd or used further by the parser, so the action function
80  * is free to do what it wishes with them.
81  */
82 typedef struct JsonSemAction
83 {
84  void *semstate;
95 
96 /*
97  * parse_json will parse the string in the lex calling the
98  * action functions in sem at the appropriate points. It is
99  * up to them to keep what state they need in semstate. If they
100  * need access to the state of the lexer, then its pointer
101  * should be passed to them as a member of whatever semstate
102  * points to. If the action pointers are NULL the parser
103  * does nothing and just continues.
104  */
105 extern void pg_parse_json(JsonLexContext *lex, JsonSemAction *sem);
106 
107 /*
108  * json_count_array_elements performs a fast secondary parse to determine the
109  * number of elements in passed array lex context. It should be called from an
110  * array_start action.
111  */
113 
114 /*
115  * constructors for JsonLexContext, with or without strval element.
116  * If supplied, the strval element will contain a de-escaped version of
117  * the lexeme. However, doing this imposes a performance penalty, so
118  * it should be avoided if the de-escaped lexeme is not required.
119  *
120  * If you already have the json as a text* value, use the first of these
121  * functions, otherwise use makeJsonLexContextCstringLen().
122  */
123 extern JsonLexContext *makeJsonLexContext(text *json, bool need_escapes);
125  int len,
126  bool need_escapes);
127 
128 /*
129  * Utility function to check if a string is a valid JSON number.
130  *
131  * str argument does not need to be nul-terminated.
132  */
133 extern bool IsValidJsonNumber(const char *str, int len);
134 
135 /* an action that will be applied to each value in iterate_json(b)_string_vaues functions */
136 typedef void (*JsonIterateStringValuesAction) (void *state, char *elem_value, int elem_len);
137 
138 /* an action that will be applied to each value in transform_json(b)_string_values functions */
139 typedef text *(*JsonTransformStringValuesAction) (void *state, char *elem_value, int elem_len);
140 
141 extern void iterate_jsonb_string_values(Jsonb *jb, void *state,
143 extern void iterate_json_string_values(text *json, void *action_state,
145 extern Jsonb *transform_jsonb_string_values(Jsonb *jsonb, void *action_state,
146  JsonTransformStringValuesAction transform_action);
147 extern text *transform_json_string_values(text *json, void *action_state,
148  JsonTransformStringValuesAction transform_action);
149 
150 #endif /* JSONAPI_H */
json_struct_action array_end
Definition: jsonapi.h:88
int line_number
Definition: jsonapi.h:60
Definition: jsonb.h:215
json_struct_action object_end
Definition: jsonapi.h:86
JsonTokenType token_type
Definition: jsonapi.h:58
json_struct_action object_start
Definition: jsonapi.h:85
char * prev_token_terminator
Definition: jsonapi.h:57
void iterate_jsonb_string_values(Jsonb *jb, void *state, JsonIterateStringValuesAction action)
Definition: jsonfuncs.c:4815
void(* json_scalar_action)(void *state, char *token, JsonTokenType tokentype)
Definition: jsonapi.h:68
json_scalar_action scalar
Definition: jsonapi.h:93
int json_count_array_elements(JsonLexContext *lex)
Definition: json.c:366
int lex_level
Definition: jsonapi.h:59
char * line_start
Definition: jsonapi.h:61
int input_length
Definition: jsonapi.h:54
void(* json_struct_action)(void *state)
Definition: jsonapi.h:65
text * transform_json_string_values(text *json, void *action_state, JsonTransformStringValuesAction transform_action)
Definition: jsonfuncs.c:4918
void(* json_ofield_action)(void *state, char *fname, bool isnull)
Definition: jsonapi.h:66
JsonLexContext * makeJsonLexContext(text *json, bool need_escapes)
Definition: json.c:300
StringInfo strval
Definition: jsonapi.h:62
void(* JsonIterateStringValuesAction)(void *state, char *elem_value, int elem_len)
Definition: jsonapi.h:136
JsonLexContext * makeJsonLexContextCstringLen(char *json, int len, bool need_escapes)
Definition: json.c:308
json_ofield_action object_field_end
Definition: jsonapi.h:90
char * token_start
Definition: jsonapi.h:55
char * token_terminator
Definition: jsonapi.h:56
void pg_parse_json(JsonLexContext *lex, JsonSemAction *sem)
Definition: json.c:331
json_aelem_action array_element_start
Definition: jsonapi.h:91
char * input
Definition: jsonapi.h:53
json_struct_action array_start
Definition: jsonapi.h:87
Definition: regguts.h:298
text *(* JsonTransformStringValuesAction)(void *state, char *elem_value, int elem_len)
Definition: jsonapi.h:139
struct JsonSemAction JsonSemAction
Jsonb * transform_jsonb_string_values(Jsonb *jsonb, void *action_state, JsonTransformStringValuesAction transform_action)
Definition: jsonfuncs.c:4873
struct JsonLexContext JsonLexContext
Definition: c.h:433
json_ofield_action object_field_start
Definition: jsonapi.h:89
void iterate_json_string_values(text *json, void *action_state, JsonIterateStringValuesAction action)
Definition: jsonfuncs.c:4837
void(* json_aelem_action)(void *state, bool isnull)
Definition: jsonapi.h:67
void * semstate
Definition: jsonapi.h:84
bool IsValidJsonNumber(const char *str, int len)
Definition: json.c:192
json_aelem_action array_element_end
Definition: jsonapi.h:92
JsonTokenType
Definition: jsonapi.h:20