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

1114{
1115 JsonbInState result;
1116
1117 memset(&result, 0, sizeof(JsonbInState));
1118
1119 datum_to_jsonb_internal(val, false, &result, tcategory, outfuncoid,
1120 false);
1121
1122 return JsonbPGetDatum(JsonbValueToJsonb(result.res));
1123}
static void datum_to_jsonb_internal(Datum val, bool is_null, JsonbInState *result, JsonTypeCategory tcategory, Oid outfuncoid, bool key_scalar)
Definition: jsonb.c:639
static Datum JsonbPGetDatum(const Jsonb *p)
Definition: jsonb.h:386
Jsonb * JsonbValueToJsonb(JsonbValue *val)
Definition: jsonb_util.c:92
JsonbValue * res
Definition: jsonb.c:31

References datum_to_jsonb_internal(), JsonbPGetDatum(), JsonbValueToJsonb(), JsonbInState::res, 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 5735 of file jsonfuncs.c.

5737{
5738 JsonLexContext lex;
5741
5742 state->lex = makeJsonLexContext(&lex, json, true);
5743 state->action = action;
5744 state->action_state = action_state;
5745 state->flags = flags;
5746
5747 sem->semstate = state;
5750
5752 freeJsonLexContext(&lex);
5753}
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:5788
static JsonParseErrorType iterate_values_scalar(void *state, char *token, JsonTokenType tokentype)
Definition: jsonfuncs.c:5760
#define pg_parse_json_or_ereport(lex, sem)
Definition: jsonfuncs.h:47
void * palloc0(Size size)
Definition: mcxt.c:1395
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(), 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 5667 of file jsonfuncs.c.

5669{
5670 JsonbIterator *it;
5671 JsonbValue v;
5673
5674 it = JsonbIteratorInit(&jb->root);
5675
5676 /*
5677 * Just recursively iterating over jsonb and call callback on all
5678 * corresponding elements
5679 */
5680 while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
5681 {
5682 if (type == WJB_KEY)
5683 {
5684 if (flags & jtiKey)
5685 action(state, v.val.string.val, v.val.string.len);
5686
5687 continue;
5688 }
5689 else if (!(type == WJB_VALUE || type == WJB_ELEM))
5690 {
5691 /* do not call callback for composite JsonbValue */
5692 continue;
5693 }
5694
5695 /* JsonbValue is a value of object or element of array */
5696 switch (v.type)
5697 {
5698 case jbvString:
5699 if (flags & jtiString)
5700 action(state, v.val.string.val, v.val.string.len);
5701 break;
5702 case jbvNumeric:
5703 if (flags & jtiNumeric)
5704 {
5705 char *val;
5706
5708 NumericGetDatum(v.val.numeric)));
5709
5710 action(state, val, strlen(val));
5711 pfree(val);
5712 }
5713 break;
5714 case jbvBool:
5715 if (flags & jtiBool)
5716 {
5717 if (v.val.boolean)
5718 action(state, "true", 4);
5719 else
5720 action(state, "false", 5);
5721 }
5722 break;
5723 default:
5724 /* do not call callback for composite JsonbValue */
5725 break;
5726 }
5727 }
5728}
Datum numeric_out(PG_FUNCTION_ARGS)
Definition: numeric.c:799
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:682
@ jbvNumeric
Definition: jsonb.h:230
@ jbvBool
Definition: jsonb.h:231
@ jbvString
Definition: jsonb.h:229
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:818
JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
Definition: jsonb_util.c:856
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:255
char * val
Definition: jsonb.h:264
JsonbContainer root
Definition: jsonb.h:215
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 6005 of file jsonfuncs.c.

6007{
6008 bool typisvarlena;
6009
6010 /* Look through any domain */
6011 typoid = getBaseType(typoid);
6012
6013 *outfuncoid = InvalidOid;
6014
6015 switch (typoid)
6016 {
6017 case BOOLOID:
6018 *outfuncoid = F_BOOLOUT;
6019 *tcategory = JSONTYPE_BOOL;
6020 break;
6021
6022 case INT2OID:
6023 case INT4OID:
6024 case INT8OID:
6025 case FLOAT4OID:
6026 case FLOAT8OID:
6027 case NUMERICOID:
6028 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
6029 *tcategory = JSONTYPE_NUMERIC;
6030 break;
6031
6032 case DATEOID:
6033 *outfuncoid = F_DATE_OUT;
6034 *tcategory = JSONTYPE_DATE;
6035 break;
6036
6037 case TIMESTAMPOID:
6038 *outfuncoid = F_TIMESTAMP_OUT;
6039 *tcategory = JSONTYPE_TIMESTAMP;
6040 break;
6041
6042 case TIMESTAMPTZOID:
6043 *outfuncoid = F_TIMESTAMPTZ_OUT;
6044 *tcategory = JSONTYPE_TIMESTAMPTZ;
6045 break;
6046
6047 case JSONOID:
6048 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
6049 *tcategory = JSONTYPE_JSON;
6050 break;
6051
6052 case JSONBOID:
6053 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
6054 *tcategory = is_jsonb ? JSONTYPE_JSONB : JSONTYPE_JSON;
6055 break;
6056
6057 default:
6058 /* Check for arrays and composites */
6059 if (OidIsValid(get_element_type(typoid)) || typoid == ANYARRAYOID
6060 || typoid == ANYCOMPATIBLEARRAYOID || typoid == RECORDARRAYOID)
6061 {
6062 *outfuncoid = F_ARRAY_OUT;
6063 *tcategory = JSONTYPE_ARRAY;
6064 }
6065 else if (type_is_rowtype(typoid)) /* includes RECORDOID */
6066 {
6067 *outfuncoid = F_RECORD_OUT;
6068 *tcategory = JSONTYPE_COMPOSITE;
6069 }
6070 else
6071 {
6072 /*
6073 * It's probably the general case. But let's look for a cast
6074 * to json (note: not to jsonb even if is_jsonb is true), if
6075 * it's not built-in.
6076 */
6077 *tcategory = JSONTYPE_OTHER;
6078 if (typoid >= FirstNormalObjectId)
6079 {
6080 Oid castfunc;
6081 CoercionPathType ctype;
6082
6083 ctype = find_coercion_pathway(JSONOID, typoid,
6085 &castfunc);
6086 if (ctype == COERCION_PATH_FUNC && OidIsValid(castfunc))
6087 {
6088 *outfuncoid = castfunc;
6089 *tcategory = JSONTYPE_CAST;
6090 }
6091 else
6092 {
6093 /* non builtin type with no cast */
6094 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
6095 }
6096 }
6097 else
6098 {
6099 /* any other builtin type */
6100 getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
6101 }
6102 }
6103 break;
6104 }
6105}
#define OidIsValid(objectId)
Definition: c.h:779
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2926
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2822
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:3074
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2688
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 5978 of file jsonfuncs.c.

5979{
5980 JsonLexContext lex;
5981 JsonParseErrorType result;
5982
5983 makeJsonLexContext(&lex, json, false);
5984
5985 /* Lex exactly one token from the input and check its type. */
5986 result = json_lex(&lex);
5987
5988 if (result == JSON_SUCCESS)
5989 return lex.token_type;
5990
5991 if (throw_error)
5992 json_errsave_error(result, &lex, NULL);
5993
5994 return JSON_TOKEN_INVALID; /* invalid json */
5995}
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 3345 of file jsonfuncs.c.

3350{
3351 JsValue jsv = {0};
3352 JsonbValue jbv;
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 {
3380 char *str = JsonbUnquote(DatumGetJsonbP(json_val));
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:702
#define DatumGetTextPP(X)
Definition: fmgr.h:292
const char * str
char * JsonbUnquote(Jsonb *jb)
Definition: jsonb.c:2230
@ jbvBinary
Definition: jsonb.h:236
static Jsonb * DatumGetJsonbP(Datum d)
Definition: jsonb.h:374
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: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:213
Definition: c.h:697
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 148 of file jsonb.c.

149{
152 unique_keys,
153 NULL);
154}
static Datum jsonb_from_cstring(char *json, int len, bool unique_keys, Node *escontext)
Definition: jsonb.c:249

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:1262

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

5600{
5601 JsonbIterator *it;
5602 JsonbValue v;
5604 uint32 flags = 0;
5605
5606 it = JsonbIteratorInit(&jb->root);
5607
5608 type = JsonbIteratorNext(&it, &v, false);
5609
5610 /*
5611 * We iterate over array (scalar internally is represented as array, so,
5612 * we will accept it too) to check all its elements. Flag names are
5613 * chosen the same as jsonb_typeof uses.
5614 */
5615 if (type != WJB_BEGIN_ARRAY)
5616 ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5617 errmsg("wrong flag type, only arrays and scalars are allowed")));
5618
5619 while ((type = JsonbIteratorNext(&it, &v, false)) == WJB_ELEM)
5620 {
5621 if (v.type != jbvString)
5622 ereport(ERROR,
5623 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5624 errmsg("flag array element is not a string"),
5625 errhint("Possible values are: \"string\", \"numeric\", \"boolean\", \"key\", and \"all\".")));
5626
5627 if (v.val.string.len == 3 &&
5628 pg_strncasecmp(v.val.string.val, "all", 3) == 0)
5629 flags |= jtiAll;
5630 else if (v.val.string.len == 3 &&
5631 pg_strncasecmp(v.val.string.val, "key", 3) == 0)
5632 flags |= jtiKey;
5633 else if (v.val.string.len == 6 &&
5634 pg_strncasecmp(v.val.string.val, "string", 6) == 0)
5635 flags |= jtiString;
5636 else if (v.val.string.len == 7 &&
5637 pg_strncasecmp(v.val.string.val, "numeric", 7) == 0)
5638 flags |= jtiNumeric;
5639 else if (v.val.string.len == 7 &&
5640 pg_strncasecmp(v.val.string.val, "boolean", 7) == 0)
5641 flags |= jtiBool;
5642 else
5643 ereport(ERROR,
5644 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5645 errmsg("wrong flag in flag array: \"%s\"",
5646 pnstrdup(v.val.string.val, v.val.string.len)),
5647 errhint("Possible values are: \"string\", \"numeric\", \"boolean\", \"key\", and \"all\".")));
5648 }
5649
5650 /* expect end of array now */
5651 if (type != WJB_END_ARRAY)
5652 elog(ERROR, "unexpected end of flag array");
5653
5654 /* get final WJB_DONE and free iterator */
5655 type = JsonbIteratorNext(&it, &v, false);
5656 if (type != WJB_DONE)
5657 elog(ERROR, "unexpected end of flag array");
5658
5659 return flags;
5660}
uint32_t uint32
Definition: c.h:543
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:69

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

5858{
5859 JsonLexContext lex;
5862 StringInfoData strbuf;
5863
5864 initStringInfo(&strbuf);
5865
5866 state->lex = makeJsonLexContext(&lex, json, true);
5867 state->strval = &strbuf;
5868 state->action = transform_action;
5869 state->action_state = action_state;
5870
5871 sem->semstate = state;
5879
5881 freeJsonLexContext(&lex);
5882
5883 return cstring_to_text_with_len(state->strval->data, state->strval->len);
5884}
static JsonParseErrorType transform_string_values_array_start(void *state)
Definition: jsonfuncs.c:5912
static JsonParseErrorType transform_string_values_object_field_start(void *state, char *fname, bool isnull)
Definition: jsonfuncs.c:5932
static JsonParseErrorType transform_string_values_array_end(void *state)
Definition: jsonfuncs.c:5922
static JsonParseErrorType transform_string_values_object_end(void *state)
Definition: jsonfuncs.c:5902
static JsonParseErrorType transform_string_values_object_start(void *state)
Definition: jsonfuncs.c:5892
static JsonParseErrorType transform_string_values_array_element_start(void *state, bool isnull)
Definition: jsonfuncs.c:5950
static JsonParseErrorType transform_string_values_scalar(void *state, char *token, JsonTokenType tokentype)
Definition: jsonfuncs.c:5961
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(), 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 5809 of file jsonfuncs.c.

5811{
5812 JsonbIterator *it;
5813 JsonbValue v,
5814 *res = NULL;
5816 JsonbParseState *st = NULL;
5817 text *out;
5818 bool is_scalar = false;
5819
5820 it = JsonbIteratorInit(&jsonb->root);
5821 is_scalar = it->isScalar;
5822
5823 while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
5824 {
5825 if ((type == WJB_VALUE || type == WJB_ELEM) && v.type == jbvString)
5826 {
5827 out = transform_action(action_state, v.val.string.val, v.val.string.len);
5828 /* out is probably not toasted, but let's be sure */
5829 out = pg_detoast_datum_packed(out);
5830 v.val.string.val = VARDATA_ANY(out);
5831 v.val.string.len = VARSIZE_ANY_EXHDR(out);
5832 res = pushJsonbValue(&st, type, type < WJB_BEGIN_ARRAY ? &v : NULL);
5833 }
5834 else
5835 {
5836 res = pushJsonbValue(&st, type, (type == WJB_KEY ||
5837 type == WJB_VALUE ||
5838 type == WJB_ELEM) ? &v : NULL);
5839 }
5840 }
5841
5842 if (res->type == jbvArray)
5843 res->val.array.rawScalar = is_scalar;
5844
5845 return JsonbValueToJsonb(res);
5846}
@ jbvArray
Definition: jsonb.h:233
JsonbValue * pushJsonbValue(JsonbParseState **pstate, JsonbIteratorToken seq, JsonbValue *jbval)
Definition: jsonb_util.c:567
bool isScalar
Definition: jsonb.h:347

References JsonbIterator::isScalar, jbvArray, jbvString, JsonbIteratorInit(), JsonbIteratorNext(), JsonbValueToJsonb(), pg_detoast_datum_packed(), pushJsonbValue(), 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().