]> granicus.if.org Git - postgresql/blob - src/include/utils/jsonapi.h
Improve performance of numeric sum(), avg(), stddev(), variance(), etc.
[postgresql] / src / include / utils / jsonapi.h
1 /*-------------------------------------------------------------------------
2  *
3  * jsonapi.h
4  *        Declarations for JSON API support.
5  *
6  * Portions Copyright (c) 1996-2013, 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 "lib/stringinfo.h"
18
19 typedef enum
20 {
21         JSON_TOKEN_INVALID,
22         JSON_TOKEN_STRING,
23         JSON_TOKEN_NUMBER,
24         JSON_TOKEN_OBJECT_START,
25         JSON_TOKEN_OBJECT_END,
26         JSON_TOKEN_ARRAY_START,
27         JSON_TOKEN_ARRAY_END,
28         JSON_TOKEN_COMMA,
29         JSON_TOKEN_COLON,
30         JSON_TOKEN_TRUE,
31         JSON_TOKEN_FALSE,
32         JSON_TOKEN_NULL,
33         JSON_TOKEN_END,
34 } JsonTokenType;
35
36
37 /*
38  * All the fields in this structure should be treated as read-only.
39  *
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.
43  *
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.
49  */
50 typedef struct JsonLexContext
51 {
52         char       *input;
53         int                     input_length;
54         char       *token_start;
55         char       *token_terminator;
56         char       *prev_token_terminator;
57         JsonTokenType token_type;
58         int                     lex_level;
59         int                     line_number;
60         char       *line_start;
61         StringInfo      strval;
62 } JsonLexContext;
63
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);
68
69
70 /*
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.
76  */
77 typedef struct JsonSemAction
78 {
79         void       *semstate;
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;
89 } JsonSemAction;
90
91 /*
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.
99  */
100 extern void pg_parse_json(JsonLexContext *lex, JsonSemAction *sem);
101
102 /*
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.
107  */
108 extern JsonLexContext *makeJsonLexContext(text *json, bool need_escapes);
109
110 #endif   /* JSONAPI_H */