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, 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,
32 } JsonToIndex;
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 754 of file json.c.

755 {
756  StringInfo result = makeStringInfo();
757 
758  datum_to_json_internal(val, false, result, tcategory, outfuncoid,
759  false);
760 
761  return PointerGetDatum(cstring_to_text_with_len(result->data, result->len));
762 }
long val
Definition: informix.c:670
static void datum_to_json_internal(Datum val, bool is_null, StringInfo result, JsonTypeCategory tcategory, Oid outfuncoid, bool key_scalar)
Definition: json.c:177
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
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(), StringInfoData::len, makeStringInfo(), 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 1112 of file jsonb.c.

1113 {
1114  JsonbInState result;
1115 
1116  memset(&result, 0, sizeof(JsonbInState));
1117 
1118  datum_to_jsonb_internal(val, false, &result, tcategory, outfuncoid,
1119  false);
1120 
1121  return JsonbPGetDatum(JsonbValueToJsonb(result.res));
1122 }
static void datum_to_jsonb_internal(Datum val, bool is_null, JsonbInState *result, JsonTypeCategory tcategory, Oid outfuncoid, bool key_scalar)
Definition: jsonb.c:638
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 5708 of file jsonfuncs.c.

5710 {
5711  JsonLexContext lex;
5712  JsonSemAction *sem = palloc0(sizeof(JsonSemAction));
5714 
5715  state->lex = makeJsonLexContext(&lex, json, true);
5716  state->action = action;
5717  state->action_state = action_state;
5718  state->flags = flags;
5719 
5720  sem->semstate = (void *) state;
5723 
5725  freeJsonLexContext(&lex);
5726 }
void freeJsonLexContext(JsonLexContext *lex)
Definition: jsonapi.c:482
static JsonParseErrorType iterate_values_object_field_start(void *state, char *fname, bool isnull)
Definition: jsonfuncs.c:5761
JsonLexContext * makeJsonLexContext(JsonLexContext *lex, text *json, bool need_escapes)
Definition: jsonfuncs.c:538
static JsonParseErrorType iterate_values_scalar(void *state, char *token, JsonTokenType tokentype)
Definition: jsonfuncs.c:5733
#define pg_parse_json_or_ereport(lex, sem)
Definition: jsonfuncs.h:47
void * palloc0(Size size)
Definition: mcxt.c:1347
json_ofield_action object_field_start
Definition: jsonapi.h:139
json_scalar_action scalar
Definition: jsonapi.h:143
void * semstate
Definition: jsonapi.h:134
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 5640 of file jsonfuncs.c.

5642 {
5643  JsonbIterator *it;
5644  JsonbValue v;
5646 
5647  it = JsonbIteratorInit(&jb->root);
5648 
5649  /*
5650  * Just recursively iterating over jsonb and call callback on all
5651  * corresponding elements
5652  */
5653  while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
5654  {
5655  if (type == WJB_KEY)
5656  {
5657  if (flags & jtiKey)
5658  action(state, v.val.string.val, v.val.string.len);
5659 
5660  continue;
5661  }
5662  else if (!(type == WJB_VALUE || type == WJB_ELEM))
5663  {
5664  /* do not call callback for composite JsonbValue */
5665  continue;
5666  }
5667 
5668  /* JsonbValue is a value of object or element of array */
5669  switch (v.type)
5670  {
5671  case jbvString:
5672  if (flags & jtiString)
5673  action(state, v.val.string.val, v.val.string.len);
5674  break;
5675  case jbvNumeric:
5676  if (flags & jtiNumeric)
5677  {
5678  char *val;
5679 
5681  NumericGetDatum(v.val.numeric)));
5682 
5683  action(state, val, strlen(val));
5684  pfree(val);
5685  }
5686  break;
5687  case jbvBool:
5688  if (flags & jtiBool)
5689  {
5690  if (v.val.boolean)
5691  action(state, "true", 4);
5692  else
5693  action(state, "false", 5);
5694  }
5695  break;
5696  default:
5697  /* do not call callback for composite JsonbValue */
5698  break;
5699  }
5700  }
5701 }
Datum numeric_out(PG_FUNCTION_ARGS)
Definition: numeric.c:816
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:642
@ 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:817
JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
Definition: jsonb_util.c:853
void pfree(void *pointer)
Definition: mcxt.c:1521
static Datum NumericGetDatum(Numeric X)
Definition: numeric.h:73
static char * DatumGetCString(Datum X)
Definition: postgres.h:335
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 5975 of file jsonfuncs.c.

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

641 {
645  errsave(escontext,
646  (errcode(ERRCODE_UNTRANSLATABLE_CHARACTER),
647  errmsg("unsupported Unicode escape sequence"),
649  report_json_context(lex)));
650  else if (error == JSON_SEM_ACTION_FAILED)
651  {
652  /* semantic action function had better have reported something */
653  if (!SOFT_ERROR_OCCURRED(escontext))
654  elog(ERROR, "JSON semantic action function did not provide error information");
655  }
656  else
657  errsave(escontext,
658  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
659  errmsg("invalid input syntax for type %s", "json"),
661  report_json_context(lex)));
662 }
int errdetail_internal(const char *fmt,...)
Definition: elog.c:1230
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define errsave(context,...)
Definition: elog.h:260
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
char * json_errdetail(JsonParseErrorType error, JsonLexContext *lex)
Definition: jsonapi.c:2096
@ JSON_SEM_ACTION_FAILED
Definition: jsonapi.h:60
@ JSON_UNICODE_CODE_POINT_ZERO
Definition: jsonapi.h:54
@ JSON_UNICODE_UNTRANSLATABLE
Definition: jsonapi.h:57
@ JSON_UNICODE_HIGH_ESCAPE
Definition: jsonapi.h:56
static int report_json_context(JsonLexContext *lex)
Definition: jsonfuncs.c:675
#define SOFT_ERROR_OCCURRED(escontext)
Definition: miscnodes.h:52
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 5948 of file jsonfuncs.c.

5949 {
5950  JsonLexContext lex;
5951  JsonParseErrorType result;
5952 
5953  makeJsonLexContext(&lex, json, false);
5954 
5955  /* Lex exactly one token from the input and check its type. */
5956  result = json_lex(&lex);
5957 
5958  if (result == JSON_SUCCESS)
5959  return lex.token_type;
5960 
5961  if (throw_error)
5962  json_errsave_error(result, &lex, NULL);
5963 
5964  return JSON_TOKEN_INVALID; /* invalid json */
5965 }
JsonParseErrorType json_lex(JsonLexContext *lex)
Definition: jsonapi.c:1308
JsonParseErrorType
Definition: jsonapi.h:37
@ JSON_SUCCESS
Definition: jsonapi.h:38
@ JSON_TOKEN_INVALID
Definition: jsonapi.h:21
void json_errsave_error(JsonParseErrorType error, JsonLexContext *lex, Node *escontext)
Definition: jsonfuncs.c:639
JsonTokenType token_type
Definition: jsonapi.h:98

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

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

References DatumGetJsonbP(), DatumGetTextPP, JsValue::is_json, jbvBinary, jbvString, JsValue::json, JSON_TOKEN_INVALID, JsValue::jsonb, JsonbUnquote(), MemoryContextAllocZero(), PointerGetDatum(), populate_record_field(), Jsonb::root, str, 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 147 of file jsonb.c.

148 {
149  return jsonb_from_cstring(VARDATA_ANY(js),
150  VARSIZE_ANY_EXHDR(js),
151  unique_keys,
152  NULL);
153 }
static Datum jsonb_from_cstring(char *json, int len, bool unique_keys, Node *escontext)
Definition: jsonb.c:248

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

539 {
540  /*
541  * Most callers pass a detoasted datum, but it's not clear that they all
542  * do. pg_detoast_datum_packed() is cheap insurance.
543  */
544  json = pg_detoast_datum_packed(json);
545 
546  return makeJsonLexContextCstringLen(lex,
547  VARDATA_ANY(json),
548  VARSIZE_ANY_EXHDR(json),
550  need_escapes);
551 }
struct varlena * pg_detoast_datum_packed(struct varlena *datum)
Definition: fmgr.c:1864
JsonLexContext * makeJsonLexContextCstringLen(JsonLexContext *lex, const char *json, size_t len, int encoding, bool need_escapes)
Definition: jsonapi.c:326
int GetDatabaseEncoding(void)
Definition: mbutils.c:1261

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

5573 {
5574  JsonbIterator *it;
5575  JsonbValue v;
5577  uint32 flags = 0;
5578 
5579  it = JsonbIteratorInit(&jb->root);
5580 
5581  type = JsonbIteratorNext(&it, &v, false);
5582 
5583  /*
5584  * We iterate over array (scalar internally is represented as array, so,
5585  * we will accept it too) to check all its elements. Flag names are
5586  * chosen the same as jsonb_typeof uses.
5587  */
5588  if (type != WJB_BEGIN_ARRAY)
5589  ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5590  errmsg("wrong flag type, only arrays and scalars are allowed")));
5591 
5592  while ((type = JsonbIteratorNext(&it, &v, false)) == WJB_ELEM)
5593  {
5594  if (v.type != jbvString)
5595  ereport(ERROR,
5596  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5597  errmsg("flag array element is not a string"),
5598  errhint("Possible values are: \"string\", \"numeric\", \"boolean\", \"key\", and \"all\".")));
5599 
5600  if (v.val.string.len == 3 &&
5601  pg_strncasecmp(v.val.string.val, "all", 3) == 0)
5602  flags |= jtiAll;
5603  else if (v.val.string.len == 3 &&
5604  pg_strncasecmp(v.val.string.val, "key", 3) == 0)
5605  flags |= jtiKey;
5606  else if (v.val.string.len == 6 &&
5607  pg_strncasecmp(v.val.string.val, "string", 6) == 0)
5608  flags |= jtiString;
5609  else if (v.val.string.len == 7 &&
5610  pg_strncasecmp(v.val.string.val, "numeric", 7) == 0)
5611  flags |= jtiNumeric;
5612  else if (v.val.string.len == 7 &&
5613  pg_strncasecmp(v.val.string.val, "boolean", 7) == 0)
5614  flags |= jtiBool;
5615  else
5616  ereport(ERROR,
5617  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5618  errmsg("wrong flag in flag array: \"%s\"",
5619  pnstrdup(v.val.string.val, v.val.string.len)),
5620  errhint("Possible values are: \"string\", \"numeric\", \"boolean\", \"key\", and \"all\".")));
5621  }
5622 
5623  /* expect end of array now */
5624  if (type != WJB_END_ARRAY)
5625  elog(ERROR, "unexpected end of flag array");
5626 
5627  /* get final WJB_DONE and free iterator */
5628  type = JsonbIteratorNext(&it, &v, false);
5629  if (type != WJB_DONE)
5630  elog(ERROR, "unexpected end of flag array");
5631 
5632  return flags;
5633 }
unsigned int uint32
Definition: c.h:506
int errhint(const char *fmt,...)
Definition: elog.c:1317
#define ereport(elevel,...)
Definition: elog.h:149
@ WJB_END_ARRAY
Definition: jsonb.h:27
@ WJB_BEGIN_ARRAY
Definition: jsonb.h:26
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1707
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,
JsonSemAction sem,
struct Node escontext 
)

Definition at line 517 of file jsonfuncs.c.

519 {
520  JsonParseErrorType result;
521 
522  result = pg_parse_json(lex, sem);
523  if (result != JSON_SUCCESS)
524  {
525  json_errsave_error(result, lex, escontext);
526  return false;
527  }
528  return true;
529 }
JsonParseErrorType pg_parse_json(JsonLexContext *lex, JsonSemAction *sem)
Definition: jsonapi.c:521

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

5831 {
5832  JsonLexContext lex;
5833  JsonSemAction *sem = palloc0(sizeof(JsonSemAction));
5835 
5836  state->lex = makeJsonLexContext(&lex, json, true);
5837  state->strval = makeStringInfo();
5838  state->action = transform_action;
5839  state->action_state = action_state;
5840 
5841  sem->semstate = (void *) state;
5849 
5851  freeJsonLexContext(&lex);
5852 
5853  return cstring_to_text_with_len(state->strval->data, state->strval->len);
5854 }
static JsonParseErrorType transform_string_values_array_start(void *state)
Definition: jsonfuncs.c:5882
static JsonParseErrorType transform_string_values_object_field_start(void *state, char *fname, bool isnull)
Definition: jsonfuncs.c:5902
static JsonParseErrorType transform_string_values_array_end(void *state)
Definition: jsonfuncs.c:5892
static JsonParseErrorType transform_string_values_object_end(void *state)
Definition: jsonfuncs.c:5872
static JsonParseErrorType transform_string_values_object_start(void *state)
Definition: jsonfuncs.c:5862
static JsonParseErrorType transform_string_values_array_element_start(void *state, bool isnull)
Definition: jsonfuncs.c:5920
static JsonParseErrorType transform_string_values_scalar(void *state, char *token, JsonTokenType tokentype)
Definition: jsonfuncs.c:5931
json_struct_action array_end
Definition: jsonapi.h:138
json_struct_action object_start
Definition: jsonapi.h:135
json_aelem_action array_element_start
Definition: jsonapi.h:141
json_struct_action array_start
Definition: jsonapi.h:137
json_struct_action object_end
Definition: jsonapi.h:136

References JsonSemAction::array_element_start, JsonSemAction::array_end, JsonSemAction::array_start, cstring_to_text_with_len(), freeJsonLexContext(), makeJsonLexContext(), makeStringInfo(), 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 5782 of file jsonfuncs.c.

5784 {
5785  JsonbIterator *it;
5786  JsonbValue v,
5787  *res = NULL;
5789  JsonbParseState *st = NULL;
5790  text *out;
5791  bool is_scalar = false;
5792 
5793  it = JsonbIteratorInit(&jsonb->root);
5794  is_scalar = it->isScalar;
5795 
5796  while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
5797  {
5798  if ((type == WJB_VALUE || type == WJB_ELEM) && v.type == jbvString)
5799  {
5800  out = transform_action(action_state, v.val.string.val, v.val.string.len);
5801  /* out is probably not toasted, but let's be sure */
5802  out = pg_detoast_datum_packed(out);
5803  v.val.string.val = VARDATA_ANY(out);
5804  v.val.string.len = VARSIZE_ANY_EXHDR(out);
5805  res = pushJsonbValue(&st, type, type < WJB_BEGIN_ARRAY ? &v : NULL);
5806  }
5807  else
5808  {
5809  res = pushJsonbValue(&st, type, (type == WJB_KEY ||
5810  type == WJB_VALUE ||
5811  type == WJB_ELEM) ? &v : NULL);
5812  }
5813  }
5814 
5815  if (res->type == jbvArray)
5816  res->val.array.rawScalar = is_scalar;
5817 
5818  return JsonbValueToJsonb(res);
5819 }
@ jbvArray
Definition: jsonb.h:233
JsonbValue * pushJsonbValue(JsonbParseState **pstate, JsonbIteratorToken seq, JsonbValue *jbval)
Definition: jsonb_util.c:566
bool isScalar
Definition: jsonb.h:347

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