PostgreSQL Source Code git master
Loading...
Searching...
No Matches
jsonfuncs.h File Reference
#include "common/jsonapi.h"
#include "nodes/nodes.h"
#include "utils/jsonb.h"
Include dependency graph for jsonfuncs.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define pg_parse_json_or_ereport(lex, sem)    (void) pg_parse_json_or_errsave(lex, sem, NULL)
 

Typedefs

typedef enum JsonToIndex JsonToIndex
 
typedef void(* JsonIterateStringValuesAction) (void *state, char *elem_value, int elem_len)
 
typedef text *(* JsonTransformStringValuesAction) (void *state, char *elem_value, int elem_len)
 

Enumerations

enum  JsonToIndex {
  jtiKey = 0x01 , jtiString = 0x02 , jtiNumeric = 0x04 , jtiBool = 0x08 ,
  jtiAll = jtiKey | jtiString | jtiNumeric | jtiBool
}
 
enum  JsonTypeCategory {
  JSONTYPE_NULL , JSONTYPE_BOOL , JSONTYPE_NUMERIC , JSONTYPE_DATE ,
  JSONTYPE_TIMESTAMP , JSONTYPE_TIMESTAMPTZ , JSONTYPE_JSON , JSONTYPE_JSONB ,
  JSONTYPE_ARRAY , JSONTYPE_COMPOSITE , JSONTYPE_CAST , JSONTYPE_OTHER
}
 

Functions

JsonLexContextmakeJsonLexContext (JsonLexContext *lex, text *json, bool need_escapes)
 
bool pg_parse_json_or_errsave (JsonLexContext *lex, const JsonSemAction *sem, struct Node *escontext)
 
void json_errsave_error (JsonParseErrorType error, JsonLexContext *lex, struct Node *escontext)
 
JsonTokenType json_get_first_token (text *json, bool throw_error)
 
uint32 parse_jsonb_index_flags (Jsonb *jb)
 
void iterate_jsonb_values (Jsonb *jb, uint32 flags, void *state, JsonIterateStringValuesAction action)
 
void iterate_json_values (text *json, uint32 flags, void *action_state, JsonIterateStringValuesAction action)
 
Jsonbtransform_jsonb_string_values (Jsonb *jsonb, void *action_state, JsonTransformStringValuesAction transform_action)
 
texttransform_json_string_values (text *json, void *action_state, JsonTransformStringValuesAction transform_action)
 
void json_categorize_type (Oid typoid, bool is_jsonb, JsonTypeCategory *tcategory, Oid *outfuncoid)
 
void json_check_mutability (Oid typoid, bool is_jsonb, bool *has_mutable)
 
Datum datum_to_json (Datum val, JsonTypeCategory tcategory, Oid outfuncoid)
 
Datum datum_to_jsonb (Datum val, JsonTypeCategory tcategory, Oid outfuncoid)
 
Datum jsonb_from_text (text *js, bool unique_keys)
 
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)
 

Macro Definition Documentation

◆ pg_parse_json_or_ereport

#define pg_parse_json_or_ereport (   lex,
  sem 
)     (void) pg_parse_json_or_errsave(lex, sem, NULL)

Definition at line 47 of file jsonfuncs.h.

68{
69 JSONTYPE_NULL, /* null, so we didn't bother to identify */
70 JSONTYPE_BOOL, /* boolean (built-in types only) */
71 JSONTYPE_NUMERIC, /* numeric (ditto) */
72 JSONTYPE_DATE, /* we use special formatting for datetimes */
75 JSONTYPE_JSON, /* JSON (and JSONB, if not is_jsonb) */
76 JSONTYPE_JSONB, /* JSONB (if is_jsonb) */
77 JSONTYPE_ARRAY, /* array */
78 JSONTYPE_COMPOSITE, /* composite */
79 JSONTYPE_CAST, /* something with an explicit cast to JSON */
80 JSONTYPE_OTHER, /* all else */
82
83extern void json_categorize_type(Oid typoid, bool is_jsonb,
85extern void json_check_mutability(Oid typoid, bool is_jsonb,
86 bool *has_mutable);
91extern Datum jsonb_from_text(text *js, bool unique_keys);
92
94 Oid typid, int32 typmod,
95 void **cache, MemoryContext mcxt,
96 bool *isnull,
97 bool omit_quotes,
98 Node *escontext);
99
100#endif
int32_t int32
Definition c.h:614
long val
Definition informix.c:689
Datum jsonb_from_text(text *js, bool unique_keys)
Definition jsonb.c:139
void json_check_mutability(Oid typoid, bool is_jsonb, bool *has_mutable)
Definition jsonfuncs.c:6078
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:3345
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 json_categorize_type(Oid typoid, bool is_jsonb, JsonTypeCategory *tcategory, Oid *outfuncoid)
Definition jsonfuncs.c:5967
Datum datum_to_json(Datum val, JsonTypeCategory tcategory, Oid outfuncoid)
Definition json.c:732
Datum datum_to_jsonb(Datum val, JsonTypeCategory tcategory, Oid outfuncoid)
Definition jsonb.c:1117
uint64_t Datum
Definition postgres.h:70
unsigned int Oid
static int fb(int x)
Definition nodes.h:135
Definition c.h:778

Typedef Documentation

◆ JsonIterateStringValuesAction

typedef void(* JsonIterateStringValuesAction) (void *state, char *elem_value, int elem_len)

Definition at line 35 of file jsonfuncs.h.

◆ JsonToIndex

◆ JsonTransformStringValuesAction

typedef text *(* JsonTransformStringValuesAction) (void *state, char *elem_value, int elem_len)

Definition at line 38 of file jsonfuncs.h.

Enumeration Type Documentation

◆ JsonToIndex

Enumerator
jtiKey 
jtiString 
jtiNumeric 
jtiBool 
jtiAll 

Definition at line 25 of file jsonfuncs.h.

26{
27 jtiKey = 0x01,
28 jtiString = 0x02,
29 jtiNumeric = 0x04,
30 jtiBool = 0x08,
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

◆ JsonTypeCategory

Enumerator
JSONTYPE_NULL 
JSONTYPE_BOOL 
JSONTYPE_NUMERIC 
JSONTYPE_DATE 
JSONTYPE_TIMESTAMP 
JSONTYPE_TIMESTAMPTZ 
JSONTYPE_JSON 
JSONTYPE_JSONB 
JSONTYPE_ARRAY 
JSONTYPE_COMPOSITE 
JSONTYPE_CAST 
JSONTYPE_OTHER 

Definition at line 68 of file jsonfuncs.h.

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 */

Function Documentation

◆ datum_to_json()

Datum datum_to_json ( Datum  val,
JsonTypeCategory  tcategory,
Oid  outfuncoid 
)
extern

Definition at line 732 of file json.c.

733{
734 StringInfoData result;
735
736 initStringInfo(&result);
738 false);
739
740 return PointerGetDatum(cstring_to_text_with_len(result.data, result.len));
741}
static void datum_to_json_internal(Datum val, bool is_null, StringInfo result, JsonTypeCategory tcategory, Oid outfuncoid, bool key_scalar)
Definition json.c:178
static Datum PointerGetDatum(const void *X)
Definition postgres.h:342
void initStringInfo(StringInfo str)
Definition stringinfo.c:97
text * cstring_to_text_with_len(const char *s, int len)
Definition varlena.c:196

References cstring_to_text_with_len(), StringInfoData::data, datum_to_json_internal(), fb(), initStringInfo(), StringInfoData::len, PointerGetDatum(), and val.

Referenced by ExecEvalJsonConstructor(), and to_json().

◆ datum_to_jsonb()

Datum datum_to_jsonb ( Datum  val,
JsonTypeCategory  tcategory,
Oid  outfuncoid 
)
extern

Definition at line 1117 of file jsonb.c.

1118{
1119 JsonbInState result;
1120
1121 memset(&result, 0, sizeof(JsonbInState));
1122
1124 false);
1125
1126 return JsonbPGetDatum(JsonbValueToJsonb(result.result));
1127}
static void datum_to_jsonb_internal(Datum val, bool is_null, JsonbInState *result, JsonTypeCategory tcategory, Oid outfuncoid, bool key_scalar)
Definition jsonb.c:630
static Datum JsonbPGetDatum(const Jsonb *p)
Definition jsonb.h:413
Jsonb * JsonbValueToJsonb(JsonbValue *val)
Definition jsonb_util.c:96
JsonbValue * result
Definition jsonb.h:333

References datum_to_jsonb_internal(), fb(), JsonbPGetDatum(), JsonbValueToJsonb(), JsonbInState::result, and val.

Referenced by ExecEvalJsonConstructor(), and to_jsonb().

◆ iterate_json_values()

void iterate_json_values ( text json,
uint32  flags,
void action_state,
JsonIterateStringValuesAction  action 
)
extern

Definition at line 5698 of file jsonfuncs.c.

5700{
5701 JsonLexContext lex;
5704
5705 state->lex = makeJsonLexContext(&lex, json, true);
5706 state->action = action;
5707 state->action_state = action_state;
5708 state->flags = flags;
5709
5710 sem->semstate = state;
5713
5715 freeJsonLexContext(&lex);
5716}
#define palloc0_object(type)
Definition fe_memutils.h:75
void freeJsonLexContext(JsonLexContext *lex)
Definition jsonapi.c:687
JsonLexContext * makeJsonLexContext(JsonLexContext *lex, text *json, bool need_escapes)
Definition jsonfuncs.c:543
static JsonParseErrorType iterate_values_object_field_start(void *state, char *fname, bool isnull)
Definition jsonfuncs.c:5751
static JsonParseErrorType iterate_values_scalar(void *state, char *token, JsonTokenType tokentype)
Definition jsonfuncs.c:5723
#define pg_parse_json_or_ereport(lex, sem)
Definition jsonfuncs.h:47
json_ofield_action object_field_start
Definition jsonapi.h:158
json_scalar_action scalar
Definition jsonapi.h:162
void * semstate
Definition jsonapi.h:153
static JsonSemAction sem

References freeJsonLexContext(), iterate_values_object_field_start(), iterate_values_scalar(), makeJsonLexContext(), JsonSemAction::object_field_start, palloc0_object, pg_parse_json_or_ereport, JsonSemAction::scalar, sem, and JsonSemAction::semstate.

Referenced by json_to_tsvector_worker().

◆ iterate_jsonb_values()

void iterate_jsonb_values ( Jsonb jb,
uint32  flags,
void state,
JsonIterateStringValuesAction  action 
)
extern

Definition at line 5630 of file jsonfuncs.c.

5632{
5634 JsonbValue v;
5636
5637 it = JsonbIteratorInit(&jb->root);
5638
5639 /*
5640 * Just recursively iterating over jsonb and call callback on all
5641 * corresponding elements
5642 */
5643 while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
5644 {
5645 if (type == WJB_KEY)
5646 {
5647 if (flags & jtiKey)
5648 action(state, v.val.string.val, v.val.string.len);
5649
5650 continue;
5651 }
5652 else if (!(type == WJB_VALUE || type == WJB_ELEM))
5653 {
5654 /* do not call callback for composite JsonbValue */
5655 continue;
5656 }
5657
5658 /* JsonbValue is a value of object or element of array */
5659 switch (v.type)
5660 {
5661 case jbvString:
5662 if (flags & jtiString)
5663 action(state, v.val.string.val, v.val.string.len);
5664 break;
5665 case jbvNumeric:
5666 if (flags & jtiNumeric)
5667 {
5668 char *val;
5669
5671 NumericGetDatum(v.val.numeric)));
5672
5674 pfree(val);
5675 }
5676 break;
5677 case jbvBool:
5678 if (flags & jtiBool)
5679 {
5680 if (v.val.boolean)
5681 action(state, "true", 4);
5682 else
5683 action(state, "false", 5);
5684 }
5685 break;
5686 default:
5687 /* do not call callback for composite JsonbValue */
5688 break;
5689 }
5690 }
5691}
Datum numeric_out(PG_FUNCTION_ARGS)
Definition numeric.c:799
#define DirectFunctionCall1(func, arg1)
Definition fmgr.h:684
@ jbvNumeric
Definition jsonb.h:232
@ jbvBool
Definition jsonb.h:233
@ jbvString
Definition jsonb.h:231
JsonbIteratorToken
Definition jsonb.h:21
@ WJB_KEY
Definition jsonb.h:23
@ WJB_DONE
Definition jsonb.h:22
@ WJB_VALUE
Definition jsonb.h:24
@ WJB_ELEM
Definition jsonb.h:25
JsonbIterator * JsonbIteratorInit(JsonbContainer *container)
Definition jsonb_util.c:935
JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
Definition jsonb_util.c:973
void pfree(void *pointer)
Definition mcxt.c:1616
static Datum NumericGetDatum(Numeric X)
Definition numeric.h:76
static char * DatumGetCString(Datum X)
Definition postgres.h:355
enum jbvType type
Definition jsonb.h:257
char * val
Definition jsonb.h:266
const char * type

References DatumGetCString(), DirectFunctionCall1, fb(), jbvBool, jbvNumeric, jbvString, JsonbIteratorInit(), JsonbIteratorNext(), jtiBool, jtiKey, jtiNumeric, jtiString, numeric_out(), NumericGetDatum(), pfree(), type, JsonbValue::type, JsonbValue::val, val, WJB_DONE, WJB_ELEM, WJB_KEY, and WJB_VALUE.

Referenced by jsonb_to_tsvector_worker().

◆ json_categorize_type()

void json_categorize_type ( Oid  typoid,
bool  is_jsonb,
JsonTypeCategory tcategory,
Oid outfuncoid 
)
extern

Definition at line 5967 of file jsonfuncs.c.

5969{
5970 bool typisvarlena;
5971
5972 /* Look through any domain */
5973 typoid = getBaseType(typoid);
5974
5976
5977 switch (typoid)
5978 {
5979 case BOOLOID:
5982 break;
5983
5984 case INT2OID:
5985 case INT4OID:
5986 case INT8OID:
5987 case FLOAT4OID:
5988 case FLOAT8OID:
5989 case NUMERICOID:
5990 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
5992 break;
5993
5994 case DATEOID:
5997 break;
5998
5999 case TIMESTAMPOID:
6002 break;
6003
6004 case TIMESTAMPTZOID:
6007 break;
6008
6009 case JSONOID:
6010 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
6012 break;
6013
6014 case JSONBOID:
6015 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
6017 break;
6018
6019 default:
6020 /* Check for arrays and composites */
6021 if (OidIsValid(get_element_type(typoid)) || typoid == ANYARRAYOID
6022 || typoid == ANYCOMPATIBLEARRAYOID || typoid == RECORDARRAYOID)
6023 {
6026 }
6027 else if (type_is_rowtype(typoid)) /* includes RECORDOID */
6028 {
6031 }
6032 else
6033 {
6034 /*
6035 * It's probably the general case. But let's look for a cast
6036 * to json (note: not to jsonb even if is_jsonb is true), if
6037 * it's not built-in.
6038 */
6040 if (typoid >= FirstNormalObjectId)
6041 {
6042 Oid castfunc;
6043 CoercionPathType ctype;
6044
6045 ctype = find_coercion_pathway(JSONOID, typoid,
6047 &castfunc);
6048 if (ctype == COERCION_PATH_FUNC && OidIsValid(castfunc))
6049 {
6050 *outfuncoid = castfunc;
6052 }
6053 else
6054 {
6055 /* non builtin type with no cast */
6056 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
6057 }
6058 }
6059 else
6060 {
6061 /* any other builtin type */
6062 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
6063 }
6064 }
6065 break;
6066 }
6067}
#define OidIsValid(objectId)
Definition c.h:860
Oid get_element_type(Oid typid)
Definition lsyscache.c:2981
bool type_is_rowtype(Oid typid)
Definition lsyscache.c:2877
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition lsyscache.c:3129
Oid getBaseType(Oid typid)
Definition lsyscache.c:2743
CoercionPathType find_coercion_pathway(Oid targetTypeId, Oid sourceTypeId, CoercionContext ccontext, Oid *funcid)
CoercionPathType
@ COERCION_PATH_FUNC
#define InvalidOid
@ COERCION_EXPLICIT
Definition primnodes.h:750
#define FirstNormalObjectId
Definition transam.h:197

References COERCION_EXPLICIT, COERCION_PATH_FUNC, fb(), find_coercion_pathway(), FirstNormalObjectId, get_element_type(), getBaseType(), getTypeOutputInfo(), InvalidOid, JSONTYPE_ARRAY, JSONTYPE_BOOL, JSONTYPE_CAST, JSONTYPE_COMPOSITE, JSONTYPE_DATE, JSONTYPE_JSON, JSONTYPE_JSONB, JSONTYPE_NUMERIC, JSONTYPE_OTHER, JSONTYPE_TIMESTAMP, JSONTYPE_TIMESTAMPTZ, OidIsValid, and type_is_rowtype().

Referenced by add_json(), add_jsonb(), array_to_json_internal(), array_to_jsonb_internal(), composite_to_json(), composite_to_jsonb(), ExecInitExprRec(), json_agg_transfn_worker(), json_check_mutability(), json_object_agg_transfn_worker(), jsonb_agg_transfn_worker(), jsonb_object_agg_transfn_worker(), to_json(), and to_jsonb().

◆ json_check_mutability()

void json_check_mutability ( Oid  typoid,
bool  is_jsonb,
bool has_mutable 
)
extern

Definition at line 6078 of file jsonfuncs.c.

6079{
6080 char att_typtype = get_typtype(typoid);
6083
6084 /* since this function recurses, it could be driven to stack overflow */
6086
6088
6089 if (*has_mutable)
6090 return;
6091
6093 {
6095 return;
6096 }
6097 else if (att_typtype == TYPTYPE_COMPOSITE)
6098 {
6099 /*
6100 * For a composite type, recurse into its attributes. Use the
6101 * typcache to avoid opening the relation directly.
6102 */
6103 TupleDesc tupdesc = lookup_rowtype_tupdesc(typoid, -1);
6104
6105 for (int i = 0; i < tupdesc->natts; i++)
6106 {
6107 Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
6108
6109 if (attr->attisdropped)
6110 continue;
6111
6112 json_check_mutability(attr->atttypid, is_jsonb, has_mutable);
6113 if (*has_mutable)
6114 break;
6115 }
6116 ReleaseTupleDesc(tupdesc);
6117 return;
6118 }
6119 else if (att_typtype == TYPTYPE_RANGE)
6120 {
6122 has_mutable);
6123 return;
6124 }
6125 else if (att_typtype == TYPTYPE_MULTIRANGE)
6126 {
6128 has_mutable);
6129 return;
6130 }
6131 else
6132 {
6134
6136 {
6137 /* recurse into array element type */
6139 return;
6140 }
6141 }
6142
6144
6145 switch (tcategory)
6146 {
6147 case JSONTYPE_NULL:
6148 case JSONTYPE_BOOL:
6149 case JSONTYPE_NUMERIC:
6150 break;
6151
6152 case JSONTYPE_DATE:
6153 case JSONTYPE_TIMESTAMP:
6155 *has_mutable = true;
6156 break;
6157
6158 case JSONTYPE_JSON:
6159 case JSONTYPE_JSONB:
6160 case JSONTYPE_ARRAY:
6161 case JSONTYPE_COMPOSITE:
6162 break;
6163
6164 case JSONTYPE_CAST:
6165 case JSONTYPE_OTHER:
6167 *has_mutable = true;
6168 break;
6169 }
6170}
#define Assert(condition)
Definition c.h:945
int i
Definition isn.c:77
void json_check_mutability(Oid typoid, bool is_jsonb, bool *has_mutable)
Definition jsonfuncs.c:6078
void json_categorize_type(Oid typoid, bool is_jsonb, JsonTypeCategory *tcategory, Oid *outfuncoid)
Definition jsonfuncs.c:5967
Oid get_range_subtype(Oid rangeOid)
Definition lsyscache.c:3629
Oid get_multirange_range(Oid multirangeOid)
Definition lsyscache.c:3705
char func_volatile(Oid funcid)
Definition lsyscache.c:2000
char get_typtype(Oid typid)
Definition lsyscache.c:2851
FormData_pg_attribute * Form_pg_attribute
void check_stack_depth(void)
Definition stack_depth.c:95
#define ReleaseTupleDesc(tupdesc)
Definition tupdesc.h:238
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:178
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition typcache.c:1947

References Assert, check_stack_depth(), fb(), func_volatile(), get_element_type(), get_multirange_range(), get_range_subtype(), get_typtype(), getBaseType(), i, json_categorize_type(), json_check_mutability(), JSONTYPE_ARRAY, JSONTYPE_BOOL, JSONTYPE_CAST, JSONTYPE_COMPOSITE, JSONTYPE_DATE, JSONTYPE_JSON, JSONTYPE_JSONB, JSONTYPE_NULL, JSONTYPE_NUMERIC, JSONTYPE_OTHER, JSONTYPE_TIMESTAMP, JSONTYPE_TIMESTAMPTZ, lookup_rowtype_tupdesc(), TupleDescData::natts, OidIsValid, ReleaseTupleDesc, and TupleDescAttr().

Referenced by json_check_mutability(), to_json_is_immutable(), and to_jsonb_is_immutable().

◆ json_errsave_error()

void json_errsave_error ( JsonParseErrorType  error,
JsonLexContext lex,
struct Node escontext 
)
extern

Definition at line 644 of file jsonfuncs.c.

646{
650 errsave(escontext,
652 errmsg("unsupported Unicode escape sequence"),
654 report_json_context(lex)));
655 else if (error == JSON_SEM_ACTION_FAILED)
656 {
657 /* semantic action function had better have reported something */
658 if (!SOFT_ERROR_OCCURRED(escontext))
659 elog(ERROR, "JSON semantic action function did not provide error information");
660 }
661 else
662 errsave(escontext,
664 errmsg("invalid input syntax for type %s", "json"),
666 report_json_context(lex)));
667}
int errcode(int sqlerrcode)
Definition elog.c:874
#define errsave(context,...)
Definition elog.h:262
int int errdetail_internal(const char *fmt,...) pg_attribute_printf(1
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
char * json_errdetail(JsonParseErrorType error, JsonLexContext *lex)
Definition jsonapi.c:2404
@ JSON_SEM_ACTION_FAILED
Definition jsonapi.h:59
@ JSON_UNICODE_CODE_POINT_ZERO
Definition jsonapi.h:53
@ JSON_UNICODE_UNTRANSLATABLE
Definition jsonapi.h:56
@ JSON_UNICODE_HIGH_ESCAPE
Definition jsonapi.h:55
static int report_json_context(JsonLexContext *lex)
Definition jsonfuncs.c:680
#define SOFT_ERROR_OCCURRED(escontext)
Definition miscnodes.h:53
static char * errmsg
static void error(void)

References elog, errcode(), errdetail_internal(), errmsg, ERROR, error(), errsave, fb(), json_errdetail(), JSON_SEM_ACTION_FAILED, JSON_UNICODE_CODE_POINT_ZERO, JSON_UNICODE_HIGH_ESCAPE, JSON_UNICODE_UNTRANSLATABLE, report_json_context(), and SOFT_ERROR_OCCURRED.

Referenced by get_array_start(), json_get_first_token(), json_typeof(), json_validate(), and pg_parse_json_or_errsave().

◆ json_get_first_token()

JsonTokenType json_get_first_token ( text json,
bool  throw_error 
)
extern

Definition at line 5940 of file jsonfuncs.c.

5941{
5942 JsonLexContext lex;
5943 JsonParseErrorType result;
5944
5945 makeJsonLexContext(&lex, json, false);
5946
5947 /* Lex exactly one token from the input and check its type. */
5948 result = json_lex(&lex);
5949
5950 if (result == JSON_SUCCESS)
5951 return lex.token_type;
5952
5953 if (throw_error)
5954 json_errsave_error(result, &lex, NULL);
5955
5956 return JSON_TOKEN_INVALID; /* invalid json */
5957}
JsonParseErrorType json_lex(JsonLexContext *lex)
Definition jsonapi.c:1588
JsonParseErrorType
Definition jsonapi.h:35
@ JSON_SUCCESS
Definition jsonapi.h:36
@ JSON_TOKEN_INVALID
Definition jsonapi.h:19
void json_errsave_error(JsonParseErrorType error, JsonLexContext *lex, Node *escontext)
Definition jsonfuncs.c:644
JsonTokenType token_type
Definition jsonapi.h:109

References fb(), json_errsave_error(), json_lex(), JSON_SUCCESS, JSON_TOKEN_INVALID, makeJsonLexContext(), and JsonLexContext::token_type.

Referenced by ExecEvalJsonIsPredicate().

◆ json_populate_type()

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 
)
extern

Definition at line 3345 of file jsonfuncs.c.

3350{
3351 JsValue jsv = {0};
3353
3354 jsv.is_json = json_type == JSONOID;
3355
3356 if (*isnull)
3357 {
3358 if (jsv.is_json)
3359 jsv.val.json.str = NULL;
3360 else
3361 jsv.val.jsonb = NULL;
3362 }
3363 else if (jsv.is_json)
3364 {
3365 text *json = DatumGetTextPP(json_val);
3366
3367 jsv.val.json.str = VARDATA_ANY(json);
3368 jsv.val.json.len = VARSIZE_ANY_EXHDR(json);
3369 jsv.val.json.type = JSON_TOKEN_INVALID; /* not used in
3370 * populate_composite() */
3371 }
3372 else
3373 {
3374 Jsonb *jsonb = DatumGetJsonbP(json_val);
3375
3376 jsv.val.jsonb = &jbv;
3377
3378 if (omit_quotes)
3379 {
3381
3382 /* fill the quote-stripped string */
3383 jbv.type = jbvString;
3384 jbv.val.string.len = strlen(str);
3385 jbv.val.string.val = str;
3386 }
3387 else
3388 {
3389 /* fill binary jsonb value pointing to jb */
3390 jbv.type = jbvBinary;
3391 jbv.val.binary.data = &jsonb->root;
3392 jbv.val.binary.len = VARSIZE(jsonb) - VARHDRSZ;
3393 }
3394 }
3395
3396 if (*cache == NULL)
3397 *cache = MemoryContextAllocZero(mcxt, sizeof(ColumnIOData));
3398
3399 return populate_record_field(*cache, typid, typmod, NULL, mcxt,
3400 PointerGetDatum(NULL), &jsv, isnull,
3401 escontext, omit_quotes);
3402}
#define VARHDRSZ
Definition c.h:783
#define DatumGetTextPP(X)
Definition fmgr.h:293
const char * str
char * JsonbUnquote(Jsonb *jb)
Definition jsonb.c:2010
@ jbvBinary
Definition jsonb.h:238
static Jsonb * DatumGetJsonbP(Datum d)
Definition jsonb.h:401
static Datum populate_record_field(ColumnIOData *col, Oid typid, int32 typmod, const char *colname, MemoryContext mcxt, Datum defaultval, JsValue *jsv, bool *isnull, Node *escontext, bool omit_scalar_quotes)
Definition jsonfuncs.c:3406
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition mcxt.c:1266
Definition jsonb.h:215
JsonbContainer root
Definition jsonb.h:217
static Size VARSIZE_ANY_EXHDR(const void *PTR)
Definition varatt.h:472
static Size VARSIZE(const void *PTR)
Definition varatt.h:298
static char * VARDATA_ANY(const void *PTR)
Definition varatt.h:486

References DatumGetJsonbP(), DatumGetTextPP, fb(), jbvBinary, jbvString, JSON_TOKEN_INVALID, JsonbUnquote(), MemoryContextAllocZero(), PointerGetDatum(), populate_record_field(), Jsonb::root, str, JsonbValue::val, VARDATA_ANY(), VARHDRSZ, VARSIZE(), and VARSIZE_ANY_EXHDR().

Referenced by ExecEvalJsonCoercion().

◆ jsonb_from_text()

Datum jsonb_from_text ( text js,
bool  unique_keys 
)
extern

Definition at line 139 of file jsonb.c.

140{
143 unique_keys,
144 NULL);
145}
static Datum jsonb_from_cstring(char *json, int len, bool unique_keys, Node *escontext)
Definition jsonb.c:240

References fb(), jsonb_from_cstring(), VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by ExecEvalJsonConstructor().

◆ makeJsonLexContext()

JsonLexContext * makeJsonLexContext ( JsonLexContext lex,
text json,
bool  need_escapes 
)
extern

Definition at line 543 of file jsonfuncs.c.

544{
545 /*
546 * Most callers pass a detoasted datum, but it's not clear that they all
547 * do. pg_detoast_datum_packed() is cheap insurance.
548 */
549 json = pg_detoast_datum_packed(json);
550
552 VARDATA_ANY(json),
553 VARSIZE_ANY_EXHDR(json),
555 need_escapes);
556}
varlena * pg_detoast_datum_packed(varlena *datum)
Definition fmgr.c:1830
JsonLexContext * makeJsonLexContextCstringLen(JsonLexContext *lex, const char *json, size_t len, int encoding, bool need_escapes)
Definition jsonapi.c:392
int GetDatabaseEncoding(void)
Definition mbutils.c:1389

References GetDatabaseEncoding(), makeJsonLexContextCstringLen(), pg_detoast_datum_packed(), VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by datum_to_jsonb_internal(), each_worker(), elements_worker(), get_worker(), iterate_json_values(), json_array_length(), json_get_first_token(), json_in(), json_object_keys(), json_strip_nulls(), json_typeof(), json_validate(), populate_recordset_worker(), and transform_json_string_values().

◆ parse_jsonb_index_flags()

uint32 parse_jsonb_index_flags ( Jsonb jb)
extern

Definition at line 5562 of file jsonfuncs.c.

5563{
5565 JsonbValue v;
5567 uint32 flags = 0;
5568
5569 it = JsonbIteratorInit(&jb->root);
5570
5571 type = JsonbIteratorNext(&it, &v, false);
5572
5573 /*
5574 * We iterate over array (scalar internally is represented as array, so,
5575 * we will accept it too) to check all its elements. Flag names are
5576 * chosen the same as jsonb_typeof uses.
5577 */
5578 if (type != WJB_BEGIN_ARRAY)
5580 errmsg("wrong flag type, only arrays and scalars are allowed")));
5581
5582 while ((type = JsonbIteratorNext(&it, &v, false)) == WJB_ELEM)
5583 {
5584 if (v.type != jbvString)
5585 ereport(ERROR,
5587 errmsg("flag array element is not a string"),
5588 errhint("Possible values are: \"string\", \"numeric\", \"boolean\", \"key\", and \"all\".")));
5589
5590 if (v.val.string.len == 3 &&
5591 pg_strncasecmp(v.val.string.val, "all", 3) == 0)
5592 flags |= jtiAll;
5593 else if (v.val.string.len == 3 &&
5594 pg_strncasecmp(v.val.string.val, "key", 3) == 0)
5595 flags |= jtiKey;
5596 else if (v.val.string.len == 6 &&
5597 pg_strncasecmp(v.val.string.val, "string", 6) == 0)
5598 flags |= jtiString;
5599 else if (v.val.string.len == 7 &&
5600 pg_strncasecmp(v.val.string.val, "numeric", 7) == 0)
5601 flags |= jtiNumeric;
5602 else if (v.val.string.len == 7 &&
5603 pg_strncasecmp(v.val.string.val, "boolean", 7) == 0)
5604 flags |= jtiBool;
5605 else
5606 ereport(ERROR,
5608 errmsg("wrong flag in flag array: \"%s\"",
5609 pnstrdup(v.val.string.val, v.val.string.len)),
5610 errhint("Possible values are: \"string\", \"numeric\", \"boolean\", \"key\", and \"all\".")));
5611 }
5612
5613 /* expect end of array now */
5614 if (type != WJB_END_ARRAY)
5615 elog(ERROR, "unexpected end of flag array");
5616
5617 /* get final WJB_DONE and free iterator */
5618 type = JsonbIteratorNext(&it, &v, false);
5619 if (type != WJB_DONE)
5620 elog(ERROR, "unexpected end of flag array");
5621
5622 return flags;
5623}
uint32_t uint32
Definition c.h:618
int errhint(const char *fmt,...) pg_attribute_printf(1
#define ereport(elevel,...)
Definition elog.h:150
@ WJB_END_ARRAY
Definition jsonb.h:27
@ WJB_BEGIN_ARRAY
Definition jsonb.h:26
char * pnstrdup(const char *in, Size len)
Definition mcxt.c:1792
int pg_strncasecmp(const char *s1, const char *s2, size_t n)

References elog, ereport, errcode(), errhint(), errmsg, ERROR, fb(), jbvString, JsonbIteratorInit(), JsonbIteratorNext(), jtiAll, jtiBool, jtiKey, jtiNumeric, jtiString, pg_strncasecmp(), pnstrdup(), type, JsonbValue::type, JsonbValue::val, WJB_BEGIN_ARRAY, WJB_DONE, WJB_ELEM, and WJB_END_ARRAY.

Referenced by json_to_tsvector(), json_to_tsvector_byid(), jsonb_to_tsvector(), and jsonb_to_tsvector_byid().

◆ pg_parse_json_or_errsave()

bool pg_parse_json_or_errsave ( JsonLexContext lex,
const JsonSemAction sem,
struct Node escontext 
)
extern

Definition at line 522 of file jsonfuncs.c.

524{
525 JsonParseErrorType result;
526
527 result = pg_parse_json(lex, sem);
528 if (result != JSON_SUCCESS)
529 {
530 json_errsave_error(result, lex, escontext);
531 return false;
532 }
533 return true;
534}
JsonParseErrorType pg_parse_json(JsonLexContext *lex, const JsonSemAction *sem)
Definition jsonapi.c:744

References json_errsave_error(), JSON_SUCCESS, pg_parse_json(), and sem.

Referenced by get_json_object_as_hash(), json_in(), jsonb_from_cstring(), and populate_array_json().

◆ transform_json_string_values()

text * transform_json_string_values ( text json,
void action_state,
JsonTransformStringValuesAction  transform_action 
)
extern

Definition at line 5818 of file jsonfuncs.c.

5820{
5821 JsonLexContext lex;
5825
5827
5828 state->lex = makeJsonLexContext(&lex, json, true);
5829 state->strval = &strbuf;
5830 state->action = transform_action;
5831 state->action_state = action_state;
5832
5833 sem->semstate = state;
5841
5843 freeJsonLexContext(&lex);
5844
5845 return cstring_to_text_with_len(state->strval->data, state->strval->len);
5846}
static JsonParseErrorType transform_string_values_array_start(void *state)
Definition jsonfuncs.c:5874
static JsonParseErrorType transform_string_values_object_field_start(void *state, char *fname, bool isnull)
Definition jsonfuncs.c:5894
static JsonParseErrorType transform_string_values_array_end(void *state)
Definition jsonfuncs.c:5884
static JsonParseErrorType transform_string_values_object_end(void *state)
Definition jsonfuncs.c:5864
static JsonParseErrorType transform_string_values_object_start(void *state)
Definition jsonfuncs.c:5854
static JsonParseErrorType transform_string_values_array_element_start(void *state, bool isnull)
Definition jsonfuncs.c:5912
static JsonParseErrorType transform_string_values_scalar(void *state, char *token, JsonTokenType tokentype)
Definition jsonfuncs.c:5923
json_struct_action array_end
Definition jsonapi.h:157
json_struct_action object_start
Definition jsonapi.h:154
json_aelem_action array_element_start
Definition jsonapi.h:160
json_struct_action array_start
Definition jsonapi.h:156
json_struct_action object_end
Definition jsonapi.h:155

References JsonSemAction::array_element_start, JsonSemAction::array_end, JsonSemAction::array_start, cstring_to_text_with_len(), fb(), freeJsonLexContext(), initStringInfo(), makeJsonLexContext(), JsonSemAction::object_end, JsonSemAction::object_field_start, JsonSemAction::object_start, palloc0_object, pg_parse_json_or_ereport, JsonSemAction::scalar, sem, JsonSemAction::semstate, transform_string_values_array_element_start(), transform_string_values_array_end(), transform_string_values_array_start(), transform_string_values_object_end(), transform_string_values_object_field_start(), transform_string_values_object_start(), and transform_string_values_scalar().

Referenced by ts_headline_json_byid_opt().

◆ transform_jsonb_string_values()

Jsonb * transform_jsonb_string_values ( Jsonb jsonb,
void action_state,
JsonTransformStringValuesAction  transform_action 
)
extern

Definition at line 5772 of file jsonfuncs.c.

5774{
5776 JsonbValue v;
5778 JsonbInState st = {0};
5779 text *out;
5780 bool is_scalar = false;
5781
5782 it = JsonbIteratorInit(&jsonb->root);
5783 is_scalar = it->isScalar;
5784
5785 while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
5786 {
5787 if ((type == WJB_VALUE || type == WJB_ELEM) && v.type == jbvString)
5788 {
5789 out = transform_action(action_state, v.val.string.val, v.val.string.len);
5790 /* out is probably not toasted, but let's be sure */
5791 out = pg_detoast_datum_packed(out);
5792 v.val.string.val = VARDATA_ANY(out);
5793 v.val.string.len = VARSIZE_ANY_EXHDR(out);
5795 }
5796 else
5797 {
5798 pushJsonbValue(&st, type, (type == WJB_KEY ||
5799 type == WJB_VALUE ||
5800 type == WJB_ELEM) ? &v : NULL);
5801 }
5802 }
5803
5804 if (st.result->type == jbvArray)
5805 st.result->val.array.rawScalar = is_scalar;
5806
5807 return JsonbValueToJsonb(st.result);
5808}
@ jbvArray
Definition jsonb.h:235
void pushJsonbValue(JsonbInState *pstate, JsonbIteratorToken seq, JsonbValue *jbval)
Definition jsonb_util.c:583

References fb(), jbvArray, jbvString, JsonbIteratorInit(), JsonbIteratorNext(), JsonbValueToJsonb(), pg_detoast_datum_packed(), pushJsonbValue(), JsonbInState::result, Jsonb::root, type, JsonbValue::type, JsonbValue::val, VARDATA_ANY(), VARSIZE_ANY_EXHDR(), WJB_BEGIN_ARRAY, WJB_DONE, WJB_ELEM, WJB_KEY, and WJB_VALUE.

Referenced by ts_headline_jsonb_byid_opt().