PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
jsonfuncs.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * jsonfuncs.h
4  * Functions to process JSON data types.
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/utils/jsonfuncs.h
10  *
11  *-------------------------------------------------------------------------
12  */
13 
14 #ifndef JSONFUNCS_H
15 #define JSONFUNCS_H
16 
17 #include "common/jsonapi.h"
18 #include "nodes/nodes.h"
19 #include "utils/jsonb.h"
20 
21 /*
22  * Flag types for iterate_json(b)_values to specify what elements from a
23  * json(b) document we want to iterate.
24  */
25 typedef enum JsonToIndex
26 {
27  jtiKey = 0x01,
28  jtiString = 0x02,
29  jtiNumeric = 0x04,
30  jtiBool = 0x08,
33 
34 /* an action that will be applied to each value in iterate_json(b)_values functions */
35 typedef void (*JsonIterateStringValuesAction) (void *state, char *elem_value, int elem_len);
36 
37 /* an action that will be applied to each value in transform_json(b)_values functions */
38 typedef text *(*JsonTransformStringValuesAction) (void *state, char *elem_value, int elem_len);
39 
40 /* build a JsonLexContext from a text datum; see also freeJsonLexContext */
41 extern JsonLexContext *makeJsonLexContext(JsonLexContext *lex, text *json, bool need_escapes);
42 
43 /* try to parse json, and errsave(escontext) on failure */
45  struct Node *escontext);
46 
47 #define pg_parse_json_or_ereport(lex, sem) \
48  (void) pg_parse_json_or_errsave(lex, sem, NULL)
49 
50 /* save an error during json lexing or parsing */
52  struct Node *escontext);
53 
54 /* get first JSON token */
55 extern JsonTokenType json_get_first_token(text *json, bool throw_error);
56 
58 extern void iterate_jsonb_values(Jsonb *jb, uint32 flags, void *state,
60 extern void iterate_json_values(text *json, uint32 flags, void *action_state,
62 extern Jsonb *transform_jsonb_string_values(Jsonb *jsonb, void *action_state,
63  JsonTransformStringValuesAction transform_action);
64 extern text *transform_json_string_values(text *json, void *action_state,
65  JsonTransformStringValuesAction transform_action);
66 
67 /* Type categories returned by json_categorize_type */
68 typedef enum
69 {
70  JSONTYPE_NULL, /* null, so we didn't bother to identify */
71  JSONTYPE_BOOL, /* boolean (built-in types only) */
72  JSONTYPE_NUMERIC, /* numeric (ditto) */
73  JSONTYPE_DATE, /* we use special formatting for datetimes */
76  JSONTYPE_JSON, /* JSON (and JSONB, if not is_jsonb) */
77  JSONTYPE_JSONB, /* JSONB (if is_jsonb) */
78  JSONTYPE_ARRAY, /* array */
79  JSONTYPE_COMPOSITE, /* composite */
80  JSONTYPE_CAST, /* something with an explicit cast to JSON */
81  JSONTYPE_OTHER, /* all else */
83 
84 extern void json_categorize_type(Oid typoid, bool is_jsonb,
85  JsonTypeCategory *tcategory, Oid *outfuncoid);
86 extern Datum datum_to_json(Datum val, JsonTypeCategory tcategory,
87  Oid outfuncoid);
89  Oid outfuncoid);
90 extern Datum jsonb_from_text(text *js, bool unique_keys);
91 
92 extern Datum json_populate_type(Datum json_val, Oid json_type,
93  Oid typid, int32 typmod,
94  void **cache, MemoryContext mcxt,
95  bool *isnull,
96  bool omit_quotes,
97  Node *escontext);
98 
99 #endif
unsigned int uint32
Definition: c.h:518
signed int int32
Definition: c.h:508
long val
Definition: informix.c:689
JsonParseErrorType
Definition: jsonapi.h:35
JsonTokenType
Definition: jsonapi.h:18
Datum jsonb_from_text(text *js, bool unique_keys)
Definition: jsonb.c:147
text * transform_json_string_values(text *json, void *action_state, JsonTransformStringValuesAction transform_action)
Definition: jsonfuncs.c:5830
Jsonb * transform_jsonb_string_values(Jsonb *jsonb, void *action_state, JsonTransformStringValuesAction transform_action)
Definition: jsonfuncs.c:5783
Datum json_populate_type(Datum json_val, Oid json_type, Oid typid, int32 typmod, void **cache, MemoryContext mcxt, bool *isnull, bool omit_quotes, Node *escontext)
Definition: jsonfuncs.c:3344
JsonLexContext * makeJsonLexContext(JsonLexContext *lex, text *json, bool need_escapes)
Definition: jsonfuncs.c:539
JsonToIndex
Definition: jsonfuncs.h:26
@ jtiKey
Definition: jsonfuncs.h:27
@ jtiAll
Definition: jsonfuncs.h:31
@ jtiNumeric
Definition: jsonfuncs.h:29
@ jtiBool
Definition: jsonfuncs.h:30
@ jtiString
Definition: jsonfuncs.h:28
uint32 parse_jsonb_index_flags(Jsonb *jb)
Definition: jsonfuncs.c:5573
void(* JsonIterateStringValuesAction)(void *state, char *elem_value, int elem_len)
Definition: jsonfuncs.h:35
JsonTokenType json_get_first_token(text *json, bool throw_error)
Definition: jsonfuncs.c:5949
JsonTypeCategory
Definition: jsonfuncs.h:69
@ JSONTYPE_JSON
Definition: jsonfuncs.h:76
@ JSONTYPE_NULL
Definition: jsonfuncs.h:70
@ JSONTYPE_TIMESTAMP
Definition: jsonfuncs.h:74
@ JSONTYPE_NUMERIC
Definition: jsonfuncs.h:72
@ JSONTYPE_DATE
Definition: jsonfuncs.h:73
@ JSONTYPE_BOOL
Definition: jsonfuncs.h:71
@ JSONTYPE_OTHER
Definition: jsonfuncs.h:81
@ JSONTYPE_CAST
Definition: jsonfuncs.h:80
@ JSONTYPE_COMPOSITE
Definition: jsonfuncs.h:79
@ JSONTYPE_ARRAY
Definition: jsonfuncs.h:78
@ JSONTYPE_TIMESTAMPTZ
Definition: jsonfuncs.h:75
@ JSONTYPE_JSONB
Definition: jsonfuncs.h:77
void iterate_jsonb_values(Jsonb *jb, uint32 flags, void *state, JsonIterateStringValuesAction action)
Definition: jsonfuncs.c:5641
void json_categorize_type(Oid typoid, bool is_jsonb, JsonTypeCategory *tcategory, Oid *outfuncoid)
Definition: jsonfuncs.c:5976
void iterate_json_values(text *json, uint32 flags, void *action_state, JsonIterateStringValuesAction action)
Definition: jsonfuncs.c:5709
void json_errsave_error(JsonParseErrorType error, JsonLexContext *lex, struct Node *escontext)
Definition: jsonfuncs.c:640
bool pg_parse_json_or_errsave(JsonLexContext *lex, const JsonSemAction *sem, struct Node *escontext)
Definition: jsonfuncs.c:518
Datum datum_to_json(Datum val, JsonTypeCategory tcategory, Oid outfuncoid)
Definition: json.c:763
Datum datum_to_jsonb(Datum val, JsonTypeCategory tcategory, Oid outfuncoid)
Definition: jsonb.c:1112
text *(* JsonTransformStringValuesAction)(void *state, char *elem_value, int elem_len)
Definition: jsonfuncs.h:38
uintptr_t Datum
Definition: postgres.h:64
unsigned int Oid
Definition: postgres_ext.h:31
static void error(void)
Definition: sql-dyntest.c:147
Definition: jsonb.h:213
Definition: nodes.h:129
Definition: regguts.h:323
Definition: c.h:692
static JsonSemAction sem