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)
 
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,
89extern Datum jsonb_from_text(text *js, bool unique_keys);
90
92 Oid typid, int32 typmod,
93 void **cache, MemoryContext mcxt,
94 bool *isnull,
95 bool omit_quotes,
96 Node *escontext);
97
98#endif
int32_t int32
Definition c.h:542
long val
Definition informix.c:689
Datum jsonb_from_text(text *js, bool unique_keys)
Definition jsonb.c:140
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:3342
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:5964
Datum datum_to_json(Datum val, JsonTypeCategory tcategory, Oid outfuncoid)
Definition json.c:764
Datum datum_to_jsonb(Datum val, JsonTypeCategory tcategory, Oid outfuncoid)
Definition jsonb.c:1149
uint64_t Datum
Definition postgres.h:70
unsigned int Oid
static int fb(int x)
Definition nodes.h:135
Definition c.h:706

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 764 of file json.c.

765{
766 StringInfoData result;
767
768 initStringInfo(&result);
770 false);
771
772 return PointerGetDatum(cstring_to_text_with_len(result.data, result.len));
773}
static void datum_to_json_internal(Datum val, bool is_null, StringInfo result, JsonTypeCategory tcategory, Oid outfuncoid, bool key_scalar)
Definition json.c:180
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352
void initStringInfo(StringInfo str)
Definition stringinfo.c:97
text * cstring_to_text_with_len(const char *s, int len)
Definition varlena.c:193

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 1149 of file jsonb.c.

1150{
1151 JsonbInState result;
1152
1153 memset(&result, 0, sizeof(JsonbInState));
1154
1156 false);
1157
1158 return JsonbPGetDatum(JsonbValueToJsonb(result.result));
1159}
static void datum_to_jsonb_internal(Datum val, bool is_null, JsonbInState *result, JsonTypeCategory tcategory, Oid outfuncoid, bool key_scalar)
Definition jsonb.c:631
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 5695 of file jsonfuncs.c.

5697{
5698 JsonLexContext lex;
5701
5702 state->lex = makeJsonLexContext(&lex, json, true);
5703 state->action = action;
5704 state->action_state = action_state;
5705 state->flags = flags;
5706
5707 sem->semstate = state;
5710
5712 freeJsonLexContext(&lex);
5713}
#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:540
static JsonParseErrorType iterate_values_object_field_start(void *state, char *fname, bool isnull)
Definition jsonfuncs.c:5748
static JsonParseErrorType iterate_values_scalar(void *state, char *token, JsonTokenType tokentype)
Definition jsonfuncs.c:5720
#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 5627 of file jsonfuncs.c.

5629{
5631 JsonbValue v;
5633
5634 it = JsonbIteratorInit(&jb->root);
5635
5636 /*
5637 * Just recursively iterating over jsonb and call callback on all
5638 * corresponding elements
5639 */
5640 while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
5641 {
5642 if (type == WJB_KEY)
5643 {
5644 if (flags & jtiKey)
5645 action(state, v.val.string.val, v.val.string.len);
5646
5647 continue;
5648 }
5649 else if (!(type == WJB_VALUE || type == WJB_ELEM))
5650 {
5651 /* do not call callback for composite JsonbValue */
5652 continue;
5653 }
5654
5655 /* JsonbValue is a value of object or element of array */
5656 switch (v.type)
5657 {
5658 case jbvString:
5659 if (flags & jtiString)
5660 action(state, v.val.string.val, v.val.string.len);
5661 break;
5662 case jbvNumeric:
5663 if (flags & jtiNumeric)
5664 {
5665 char *val;
5666
5668 NumericGetDatum(v.val.numeric)));
5669
5671 pfree(val);
5672 }
5673 break;
5674 case jbvBool:
5675 if (flags & jtiBool)
5676 {
5677 if (v.val.boolean)
5678 action(state, "true", 4);
5679 else
5680 action(state, "false", 5);
5681 }
5682 break;
5683 default:
5684 /* do not call callback for composite JsonbValue */
5685 break;
5686 }
5687 }
5688}
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:365
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 5964 of file jsonfuncs.c.

5966{
5967 bool typisvarlena;
5968
5969 /* Look through any domain */
5970 typoid = getBaseType(typoid);
5971
5973
5974 switch (typoid)
5975 {
5976 case BOOLOID:
5979 break;
5980
5981 case INT2OID:
5982 case INT4OID:
5983 case INT8OID:
5984 case FLOAT4OID:
5985 case FLOAT8OID:
5986 case NUMERICOID:
5987 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
5989 break;
5990
5991 case DATEOID:
5994 break;
5995
5996 case TIMESTAMPOID:
5999 break;
6000
6001 case TIMESTAMPTZOID:
6004 break;
6005
6006 case JSONOID:
6007 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
6009 break;
6010
6011 case JSONBOID:
6012 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
6014 break;
6015
6016 default:
6017 /* Check for arrays and composites */
6018 if (OidIsValid(get_element_type(typoid)) || typoid == ANYARRAYOID
6019 || typoid == ANYCOMPATIBLEARRAYOID || typoid == RECORDARRAYOID)
6020 {
6023 }
6024 else if (type_is_rowtype(typoid)) /* includes RECORDOID */
6025 {
6028 }
6029 else
6030 {
6031 /*
6032 * It's probably the general case. But let's look for a cast
6033 * to json (note: not to jsonb even if is_jsonb is true), if
6034 * it's not built-in.
6035 */
6037 if (typoid >= FirstNormalObjectId)
6038 {
6039 Oid castfunc;
6040 CoercionPathType ctype;
6041
6042 ctype = find_coercion_pathway(JSONOID, typoid,
6044 &castfunc);
6045 if (ctype == COERCION_PATH_FUNC && OidIsValid(castfunc))
6046 {
6047 *outfuncoid = castfunc;
6049 }
6050 else
6051 {
6052 /* non builtin type with no cast */
6053 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
6054 }
6055 }
6056 else
6057 {
6058 /* any other builtin type */
6059 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
6060 }
6061 }
6062 break;
6063 }
6064}
#define OidIsValid(objectId)
Definition c.h:788
Oid get_element_type(Oid typid)
Definition lsyscache.c:2909
bool type_is_rowtype(Oid typid)
Definition lsyscache.c:2805
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition lsyscache.c:3057
Oid getBaseType(Oid typid)
Definition lsyscache.c:2671
CoercionPathType find_coercion_pathway(Oid targetTypeId, Oid sourceTypeId, CoercionContext ccontext, Oid *funcid)
CoercionPathType
@ COERCION_PATH_FUNC
#define InvalidOid
@ COERCION_EXPLICIT
Definition primnodes.h:749
#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_object_agg_transfn_worker(), jsonb_agg_transfn_worker(), jsonb_object_agg_transfn_worker(), to_json(), to_json_is_immutable(), to_jsonb(), and to_jsonb_is_immutable().

◆ json_errsave_error()

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

Definition at line 641 of file jsonfuncs.c.

643{
647 errsave(escontext,
649 errmsg("unsupported Unicode escape sequence"),
651 report_json_context(lex)));
652 else if (error == JSON_SEM_ACTION_FAILED)
653 {
654 /* semantic action function had better have reported something */
655 if (!SOFT_ERROR_OCCURRED(escontext))
656 elog(ERROR, "JSON semantic action function did not provide error information");
657 }
658 else
659 errsave(escontext,
661 errmsg("invalid input syntax for type %s", "json"),
663 report_json_context(lex)));
664}
int errdetail_internal(const char *fmt,...)
Definition elog.c:1243
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define errsave(context,...)
Definition elog.h:262
#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:677
#define SOFT_ERROR_OCCURRED(escontext)
Definition miscnodes.h:53
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 5937 of file jsonfuncs.c.

5938{
5939 JsonLexContext lex;
5940 JsonParseErrorType result;
5941
5942 makeJsonLexContext(&lex, json, false);
5943
5944 /* Lex exactly one token from the input and check its type. */
5945 result = json_lex(&lex);
5946
5947 if (result == JSON_SUCCESS)
5948 return lex.token_type;
5949
5950 if (throw_error)
5951 json_errsave_error(result, &lex, NULL);
5952
5953 return JSON_TOKEN_INVALID; /* invalid json */
5954}
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:641
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 3342 of file jsonfuncs.c.

3347{
3348 JsValue jsv = {0};
3350
3351 jsv.is_json = json_type == JSONOID;
3352
3353 if (*isnull)
3354 {
3355 if (jsv.is_json)
3356 jsv.val.json.str = NULL;
3357 else
3358 jsv.val.jsonb = NULL;
3359 }
3360 else if (jsv.is_json)
3361 {
3362 text *json = DatumGetTextPP(json_val);
3363
3364 jsv.val.json.str = VARDATA_ANY(json);
3365 jsv.val.json.len = VARSIZE_ANY_EXHDR(json);
3366 jsv.val.json.type = JSON_TOKEN_INVALID; /* not used in
3367 * populate_composite() */
3368 }
3369 else
3370 {
3371 Jsonb *jsonb = DatumGetJsonbP(json_val);
3372
3373 jsv.val.jsonb = &jbv;
3374
3375 if (omit_quotes)
3376 {
3378
3379 /* fill the quote-stripped string */
3380 jbv.type = jbvString;
3381 jbv.val.string.len = strlen(str);
3382 jbv.val.string.val = str;
3383 }
3384 else
3385 {
3386 /* fill binary jsonb value pointing to jb */
3387 jbv.type = jbvBinary;
3388 jbv.val.binary.data = &jsonb->root;
3389 jbv.val.binary.len = VARSIZE(jsonb) - VARHDRSZ;
3390 }
3391 }
3392
3393 if (*cache == NULL)
3394 *cache = MemoryContextAllocZero(mcxt, sizeof(ColumnIOData));
3395
3396 return populate_record_field(*cache, typid, typmod, NULL, mcxt,
3397 PointerGetDatum(NULL), &jsv, isnull,
3398 escontext, omit_quotes);
3399}
#define VARHDRSZ
Definition c.h:711
#define DatumGetTextPP(X)
Definition fmgr.h:293
const char * str
char * JsonbUnquote(Jsonb *jb)
Definition jsonb.c:2042
@ 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:3403
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 140 of file jsonb.c.

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

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 540 of file jsonfuncs.c.

541{
542 /*
543 * Most callers pass a detoasted datum, but it's not clear that they all
544 * do. pg_detoast_datum_packed() is cheap insurance.
545 */
546 json = pg_detoast_datum_packed(json);
547
549 VARDATA_ANY(json),
550 VARSIZE_ANY_EXHDR(json),
552 need_escapes);
553}
struct varlena * pg_detoast_datum_packed(struct varlena *datum)
Definition fmgr.c:1829
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:1264

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 5559 of file jsonfuncs.c.

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

521{
522 JsonParseErrorType result;
523
524 result = pg_parse_json(lex, sem);
525 if (result != JSON_SUCCESS)
526 {
527 json_errsave_error(result, lex, escontext);
528 return false;
529 }
530 return true;
531}
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 5815 of file jsonfuncs.c.

5817{
5818 JsonLexContext lex;
5822
5824
5825 state->lex = makeJsonLexContext(&lex, json, true);
5826 state->strval = &strbuf;
5827 state->action = transform_action;
5828 state->action_state = action_state;
5829
5830 sem->semstate = state;
5838
5840 freeJsonLexContext(&lex);
5841
5842 return cstring_to_text_with_len(state->strval->data, state->strval->len);
5843}
static JsonParseErrorType transform_string_values_array_start(void *state)
Definition jsonfuncs.c:5871
static JsonParseErrorType transform_string_values_object_field_start(void *state, char *fname, bool isnull)
Definition jsonfuncs.c:5891
static JsonParseErrorType transform_string_values_array_end(void *state)
Definition jsonfuncs.c:5881
static JsonParseErrorType transform_string_values_object_end(void *state)
Definition jsonfuncs.c:5861
static JsonParseErrorType transform_string_values_object_start(void *state)
Definition jsonfuncs.c:5851
static JsonParseErrorType transform_string_values_array_element_start(void *state, bool isnull)
Definition jsonfuncs.c:5909
static JsonParseErrorType transform_string_values_scalar(void *state, char *token, JsonTokenType tokentype)
Definition jsonfuncs.c:5920
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 5769 of file jsonfuncs.c.

5771{
5773 JsonbValue v;
5775 JsonbInState st = {0};
5776 text *out;
5777 bool is_scalar = false;
5778
5779 it = JsonbIteratorInit(&jsonb->root);
5780 is_scalar = it->isScalar;
5781
5782 while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
5783 {
5784 if ((type == WJB_VALUE || type == WJB_ELEM) && v.type == jbvString)
5785 {
5786 out = transform_action(action_state, v.val.string.val, v.val.string.len);
5787 /* out is probably not toasted, but let's be sure */
5788 out = pg_detoast_datum_packed(out);
5789 v.val.string.val = VARDATA_ANY(out);
5790 v.val.string.len = VARSIZE_ANY_EXHDR(out);
5792 }
5793 else
5794 {
5795 pushJsonbValue(&st, type, (type == WJB_KEY ||
5796 type == WJB_VALUE ||
5797 type == WJB_ELEM) ? &v : NULL);
5798 }
5799 }
5800
5801 if (st.result->type == jbvArray)
5802 st.result->val.array.rawScalar = is_scalar;
5803
5804 return JsonbValueToJsonb(st.result);
5805}
@ 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().