PostgreSQL Source Code git master
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-2025, 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 */
25typedef 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 */
35typedef 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 */
38typedef text *(*JsonTransformStringValuesAction) (void *state, char *elem_value, int elem_len);
39
40/* build a JsonLexContext from a text datum; see also freeJsonLexContext */
41extern 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 */
55extern JsonTokenType json_get_first_token(text *json, bool throw_error);
56
58extern void iterate_jsonb_values(Jsonb *jb, uint32 flags, void *state,
60extern void iterate_json_values(text *json, uint32 flags, void *action_state,
62extern Jsonb *transform_jsonb_string_values(Jsonb *jsonb, void *action_state,
63 JsonTransformStringValuesAction transform_action);
64extern text *transform_json_string_values(text *json, void *action_state,
65 JsonTransformStringValuesAction transform_action);
66
67/* Type categories returned by json_categorize_type */
68typedef 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
84extern void json_categorize_type(Oid typoid, bool is_jsonb,
85 JsonTypeCategory *tcategory, Oid *outfuncoid);
87 Oid outfuncoid);
89 Oid outfuncoid);
90extern Datum jsonb_from_text(text *js, bool unique_keys);
91
92extern 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
int32_t int32
Definition: c.h:484
uint32_t uint32
Definition: c.h:488
long val
Definition: informix.c:689
JsonParseErrorType
Definition: jsonapi.h:35
JsonTokenType
Definition: jsonapi.h:18
JsonLexContext * makeJsonLexContext(JsonLexContext *lex, text *json, bool need_escapes)
Definition: jsonfuncs.c:539
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
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
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
text *(* JsonTransformStringValuesAction)(void *state, char *elem_value, int elem_len)
Definition: jsonfuncs.h:38
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
Jsonb * transform_jsonb_string_values(Jsonb *jsonb, void *action_state, JsonTransformStringValuesAction transform_action)
Definition: jsonfuncs.c:5783
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
uintptr_t Datum
Definition: postgres.h:69
unsigned int Oid
Definition: postgres_ext.h:32
static void error(void)
Definition: sql-dyntest.c:147
Definition: jsonb.h:213
Definition: nodes.h:129
Definition: regguts.h:323
Definition: c.h:644
static JsonSemAction sem