PostgreSQL Source Code git master
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.

Typedef Documentation

◆ JsonIterateStringValuesAction

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

Definition at line 35 of file jsonfuncs.h.

◆ JsonToIndex

typedef enum JsonToIndex 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 */
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

Function Documentation

◆ datum_to_json()

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

Definition at line 764 of file json.c.

765{
766 StringInfoData result;
767
768 initStringInfo(&result);
769 datum_to_json_internal(val, false, &result, tcategory, outfuncoid,
770 false);
771
772 return PointerGetDatum(cstring_to_text_with_len(result.data, result.len));
773}
long val
Definition: informix.c:689
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:332
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(), 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 
)

Definition at line 1149 of file jsonb.c.

1150{
1151 JsonbInState result;
1152
1153 memset(&result, 0, sizeof(JsonbInState));
1154
1155 datum_to_jsonb_internal(val, false, &result, tcategory, outfuncoid,
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(), 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 
)

Definition at line 5699 of file jsonfuncs.c.

5701{
5702 JsonLexContext lex;
5705
5706 state->lex = makeJsonLexContext(&lex, json, true);
5707 state->action = action;
5708 state->action_state = action_state;
5709 state->flags = flags;
5710
5711 sem->semstate = state;
5714
5716 freeJsonLexContext(&lex);
5717}
#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:5752
static JsonParseErrorType iterate_values_scalar(void *state, char *token, JsonTokenType tokentype)
Definition: jsonfuncs.c:5724
#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
Definition: regguts.h:323
static JsonSemAction sem

References generate_unaccent_rules::action, 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 
)

Definition at line 5631 of file jsonfuncs.c.

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

References generate_unaccent_rules::action, DatumGetCString(), DirectFunctionCall1, jbvBool, jbvNumeric, jbvString, JsonbIteratorInit(), JsonbIteratorNext(), jtiBool, jtiKey, jtiNumeric, jtiString, numeric_out(), NumericGetDatum(), pfree(), Jsonb::root, 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 
)

Definition at line 5968 of file jsonfuncs.c.

5970{
5971 bool typisvarlena;
5972
5973 /* Look through any domain */
5974 typoid = getBaseType(typoid);
5975
5976 *outfuncoid = InvalidOid;
5977
5978 switch (typoid)
5979 {
5980 case BOOLOID:
5981 *outfuncoid = F_BOOLOUT;
5982 *tcategory = JSONTYPE_BOOL;
5983 break;
5984
5985 case INT2OID:
5986 case INT4OID:
5987 case INT8OID:
5988 case FLOAT4OID:
5989 case FLOAT8OID:
5990 case NUMERICOID:
5991 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
5992 *tcategory = JSONTYPE_NUMERIC;
5993 break;
5994
5995 case DATEOID:
5996 *outfuncoid = F_DATE_OUT;
5997 *tcategory = JSONTYPE_DATE;
5998 break;
5999
6000 case TIMESTAMPOID:
6001 *outfuncoid = F_TIMESTAMP_OUT;
6002 *tcategory = JSONTYPE_TIMESTAMP;
6003 break;
6004
6005 case TIMESTAMPTZOID:
6006 *outfuncoid = F_TIMESTAMPTZ_OUT;
6007 *tcategory = JSONTYPE_TIMESTAMPTZ;
6008 break;
6009
6010 case JSONOID:
6011 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
6012 *tcategory = JSONTYPE_JSON;
6013 break;
6014
6015 case JSONBOID:
6016 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
6017 *tcategory = is_jsonb ? JSONTYPE_JSONB : JSONTYPE_JSON;
6018 break;
6019
6020 default:
6021 /* Check for arrays and composites */
6022 if (OidIsValid(get_element_type(typoid)) || typoid == ANYARRAYOID
6023 || typoid == ANYCOMPATIBLEARRAYOID || typoid == RECORDARRAYOID)
6024 {
6025 *outfuncoid = F_ARRAY_OUT;
6026 *tcategory = JSONTYPE_ARRAY;
6027 }
6028 else if (type_is_rowtype(typoid)) /* includes RECORDOID */
6029 {
6030 *outfuncoid = F_RECORD_OUT;
6031 *tcategory = JSONTYPE_COMPOSITE;
6032 }
6033 else
6034 {
6035 /*
6036 * It's probably the general case. But let's look for a cast
6037 * to json (note: not to jsonb even if is_jsonb is true), if
6038 * it's not built-in.
6039 */
6040 *tcategory = JSONTYPE_OTHER;
6041 if (typoid >= FirstNormalObjectId)
6042 {
6043 Oid castfunc;
6044 CoercionPathType ctype;
6045
6046 ctype = find_coercion_pathway(JSONOID, typoid,
6048 &castfunc);
6049 if (ctype == COERCION_PATH_FUNC && OidIsValid(castfunc))
6050 {
6051 *outfuncoid = castfunc;
6052 *tcategory = JSONTYPE_CAST;
6053 }
6054 else
6055 {
6056 /* non builtin type with no cast */
6057 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
6058 }
6059 }
6060 else
6061 {
6062 /* any other builtin type */
6063 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
6064 }
6065 }
6066 break;
6067 }
6068}
#define OidIsValid(objectId)
Definition: c.h:788
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2924
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2820
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:3072
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2686
CoercionPathType find_coercion_pathway(Oid targetTypeId, Oid sourceTypeId, CoercionContext ccontext, Oid *funcid)
CoercionPathType
Definition: parse_coerce.h:25
@ COERCION_PATH_FUNC
Definition: parse_coerce.h:27
#define InvalidOid
Definition: postgres_ext.h:37
unsigned int Oid
Definition: postgres_ext.h:32
@ COERCION_EXPLICIT
Definition: primnodes.h:749
#define FirstNormalObjectId
Definition: transam.h:197

References COERCION_EXPLICIT, COERCION_PATH_FUNC, 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 
)

Definition at line 641 of file jsonfuncs.c.

643{
647 errsave(escontext,
648 (errcode(ERRCODE_UNTRANSLATABLE_CHARACTER),
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,
660 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
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)
Definition: sql-dyntest.c:147

References elog, errcode(), errdetail_internal(), errmsg(), ERROR, error(), errsave, 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 
)

Definition at line 5941 of file jsonfuncs.c.

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

Definition at line 3342 of file jsonfuncs.c.

3347{
3348 JsValue jsv = {0};
3349 JsonbValue jbv;
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 {
3377 char *str = JsonbUnquote(DatumGetJsonbP(json_val));
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:292
const char * str
char * JsonbUnquote(Jsonb *jb)
Definition: jsonb.c:2039
@ 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:1263
const char * str
Definition: jsonfuncs.c:300
union JsValue::@27 val
JsonbValue * jsonb
Definition: jsonfuncs.c:305
int len
Definition: jsonfuncs.c:301
JsonTokenType type
Definition: jsonfuncs.c:302
struct JsValue::@27::@28 json
bool is_json
Definition: jsonfuncs.c:295
Definition: jsonb.h:215
Definition: c.h:706
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, JsValue::is_json, jbvBinary, jbvString, JsValue::json, JSON_TOKEN_INVALID, JsValue::jsonb, JsonbUnquote(), JsValue::len, MemoryContextAllocZero(), PointerGetDatum(), populate_record_field(), Jsonb::root, JsValue::str, str, JsValue::type, JsonbValue::type, JsValue::val, 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 
)

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 jsonb_from_cstring(), VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by ExecEvalJsonConstructor().

◆ makeJsonLexContext()

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

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)

Definition at line 5563 of file jsonfuncs.c.

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

References elog, ereport, errcode(), errhint(), errmsg(), ERROR, jbvString, JsonbIteratorInit(), JsonbIteratorNext(), jtiAll, jtiBool, jtiKey, jtiNumeric, jtiString, pg_strncasecmp(), pnstrdup(), Jsonb::root, 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 
)

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 
)

Definition at line 5819 of file jsonfuncs.c.

5821{
5822 JsonLexContext lex;
5825 StringInfoData strbuf;
5826
5827 initStringInfo(&strbuf);
5828
5829 state->lex = makeJsonLexContext(&lex, json, true);
5830 state->strval = &strbuf;
5831 state->action = transform_action;
5832 state->action_state = action_state;
5833
5834 sem->semstate = state;
5842
5844 freeJsonLexContext(&lex);
5845
5846 return cstring_to_text_with_len(state->strval->data, state->strval->len);
5847}
static JsonParseErrorType transform_string_values_array_start(void *state)
Definition: jsonfuncs.c:5875
static JsonParseErrorType transform_string_values_object_field_start(void *state, char *fname, bool isnull)
Definition: jsonfuncs.c:5895
static JsonParseErrorType transform_string_values_array_end(void *state)
Definition: jsonfuncs.c:5885
static JsonParseErrorType transform_string_values_object_end(void *state)
Definition: jsonfuncs.c:5865
static JsonParseErrorType transform_string_values_object_start(void *state)
Definition: jsonfuncs.c:5855
static JsonParseErrorType transform_string_values_array_element_start(void *state, bool isnull)
Definition: jsonfuncs.c:5913
static JsonParseErrorType transform_string_values_scalar(void *state, char *token, JsonTokenType tokentype)
Definition: jsonfuncs.c:5924
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(), 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 
)

Definition at line 5773 of file jsonfuncs.c.

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

References JsonbIterator::isScalar, 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().