PostgreSQL Source Code  git master
jsonfuncs.h File Reference
#include "common/jsonapi.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)
 

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 46 of file jsonfuncs.h.

Typedef Documentation

◆ JsonIterateStringValuesAction

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

Definition at line 34 of file jsonfuncs.h.

◆ JsonToIndex

typedef enum JsonToIndex JsonToIndex

◆ JsonTransformStringValuesAction

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

Definition at line 37 of file jsonfuncs.h.

Enumeration Type Documentation

◆ JsonToIndex

Enumerator
jtiKey 
jtiString 
jtiNumeric 
jtiBool 
jtiAll 

Definition at line 24 of file jsonfuncs.h.

25 {
26  jtiKey = 0x01,
27  jtiString = 0x02,
28  jtiNumeric = 0x04,
29  jtiBool = 0x08,
31 } JsonToIndex;
JsonToIndex
Definition: jsonfuncs.h:25
@ jtiKey
Definition: jsonfuncs.h:26
@ jtiAll
Definition: jsonfuncs.h:30
@ jtiNumeric
Definition: jsonfuncs.h:28
@ jtiBool
Definition: jsonfuncs.h:29
@ jtiString
Definition: jsonfuncs.h:27

◆ 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 67 of file jsonfuncs.h.

68 {
69  JSONTYPE_NULL, /* null, so we didn't bother to identify */
70  JSONTYPE_BOOL, /* boolean (built-in types only) */
71  JSONTYPE_NUMERIC, /* numeric (ditto) */
72  JSONTYPE_DATE, /* we use special formatting for datetimes */
75  JSONTYPE_JSON, /* JSON (and JSONB, if not is_jsonb) */
76  JSONTYPE_JSONB, /* JSONB (if is_jsonb) */
77  JSONTYPE_ARRAY, /* array */
78  JSONTYPE_COMPOSITE, /* composite */
79  JSONTYPE_CAST, /* something with an explicit cast to JSON */
80  JSONTYPE_OTHER, /* all else */
JsonTypeCategory
Definition: jsonfuncs.h:68
@ JSONTYPE_JSON
Definition: jsonfuncs.h:75
@ JSONTYPE_NULL
Definition: jsonfuncs.h:69
@ JSONTYPE_TIMESTAMP
Definition: jsonfuncs.h:73
@ JSONTYPE_NUMERIC
Definition: jsonfuncs.h:71
@ JSONTYPE_DATE
Definition: jsonfuncs.h:72
@ JSONTYPE_BOOL
Definition: jsonfuncs.h:70
@ JSONTYPE_OTHER
Definition: jsonfuncs.h:80
@ JSONTYPE_CAST
Definition: jsonfuncs.h:79
@ JSONTYPE_COMPOSITE
Definition: jsonfuncs.h:78
@ JSONTYPE_ARRAY
Definition: jsonfuncs.h:77
@ JSONTYPE_TIMESTAMPTZ
Definition: jsonfuncs.h:74
@ JSONTYPE_JSONB
Definition: jsonfuncs.h:76

Function Documentation

◆ datum_to_json()

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

Definition at line 752 of file json.c.

753 {
754  StringInfo result = makeStringInfo();
755 
756  datum_to_json_internal(val, false, result, tcategory, outfuncoid,
757  false);
758 
759  return PointerGetDatum(cstring_to_text_with_len(result->data, result->len));
760 }
long val
Definition: informix.c:664
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 1117 of file jsonb.c.

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

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

5440 {
5441  JsonLexContext lex;
5442  JsonSemAction *sem = palloc0(sizeof(JsonSemAction));
5444 
5445  state->lex = makeJsonLexContext(&lex, json, true);
5446  state->action = action;
5447  state->action_state = action_state;
5448  state->flags = flags;
5449 
5450  sem->semstate = (void *) state;
5453 
5454  pg_parse_json_or_ereport(&lex, sem);
5455  freeJsonLexContext(&lex);
5456 }
void freeJsonLexContext(JsonLexContext *lex)
Definition: jsonapi.c:183
static JsonParseErrorType iterate_values_object_field_start(void *state, char *fname, bool isnull)
Definition: jsonfuncs.c:5491
JsonLexContext * makeJsonLexContext(JsonLexContext *lex, text *json, bool need_escapes)
Definition: jsonfuncs.c:529
static JsonParseErrorType iterate_values_scalar(void *state, char *token, JsonTokenType tokentype)
Definition: jsonfuncs.c:5463
#define pg_parse_json_or_ereport(lex, sem)
Definition: jsonfuncs.h:46
void * palloc0(Size size)
Definition: mcxt.c:1257
json_ofield_action object_field_start
Definition: jsonapi.h:124
json_scalar_action scalar
Definition: jsonapi.h:128
void * semstate
Definition: jsonapi.h:119
Definition: regguts.h:323

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

5372 {
5373  JsonbIterator *it;
5374  JsonbValue v;
5376 
5377  it = JsonbIteratorInit(&jb->root);
5378 
5379  /*
5380  * Just recursively iterating over jsonb and call callback on all
5381  * corresponding elements
5382  */
5383  while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
5384  {
5385  if (type == WJB_KEY)
5386  {
5387  if (flags & jtiKey)
5388  action(state, v.val.string.val, v.val.string.len);
5389 
5390  continue;
5391  }
5392  else if (!(type == WJB_VALUE || type == WJB_ELEM))
5393  {
5394  /* do not call callback for composite JsonbValue */
5395  continue;
5396  }
5397 
5398  /* JsonbValue is a value of object or element of array */
5399  switch (v.type)
5400  {
5401  case jbvString:
5402  if (flags & jtiString)
5403  action(state, v.val.string.val, v.val.string.len);
5404  break;
5405  case jbvNumeric:
5406  if (flags & jtiNumeric)
5407  {
5408  char *val;
5409 
5411  NumericGetDatum(v.val.numeric)));
5412 
5413  action(state, val, strlen(val));
5414  pfree(val);
5415  }
5416  break;
5417  case jbvBool:
5418  if (flags & jtiBool)
5419  {
5420  if (v.val.boolean)
5421  action(state, "true", 4);
5422  else
5423  action(state, "false", 5);
5424  }
5425  break;
5426  default:
5427  /* do not call callback for composite JsonbValue */
5428  break;
5429  }
5430  }
5431 }
Datum numeric_out(PG_FUNCTION_ARGS)
Definition: numeric.c:806
#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:819
JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
Definition: jsonb_util.c:855
void pfree(void *pointer)
Definition: mcxt.c:1456
static Datum NumericGetDatum(Numeric X)
Definition: numeric.h:72
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 5705 of file jsonfuncs.c.

5707 {
5708  bool typisvarlena;
5709 
5710  /* Look through any domain */
5711  typoid = getBaseType(typoid);
5712 
5713  *outfuncoid = InvalidOid;
5714 
5715  switch (typoid)
5716  {
5717  case BOOLOID:
5718  *outfuncoid = F_BOOLOUT;
5719  *tcategory = JSONTYPE_BOOL;
5720  break;
5721 
5722  case INT2OID:
5723  case INT4OID:
5724  case INT8OID:
5725  case FLOAT4OID:
5726  case FLOAT8OID:
5727  case NUMERICOID:
5728  getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
5729  *tcategory = JSONTYPE_NUMERIC;
5730  break;
5731 
5732  case DATEOID:
5733  *outfuncoid = F_DATE_OUT;
5734  *tcategory = JSONTYPE_DATE;
5735  break;
5736 
5737  case TIMESTAMPOID:
5738  *outfuncoid = F_TIMESTAMP_OUT;
5739  *tcategory = JSONTYPE_TIMESTAMP;
5740  break;
5741 
5742  case TIMESTAMPTZOID:
5743  *outfuncoid = F_TIMESTAMPTZ_OUT;
5744  *tcategory = JSONTYPE_TIMESTAMPTZ;
5745  break;
5746 
5747  case JSONOID:
5748  getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
5749  *tcategory = JSONTYPE_JSON;
5750  break;
5751 
5752  case JSONBOID:
5753  getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
5754  *tcategory = is_jsonb ? JSONTYPE_JSONB : JSONTYPE_JSON;
5755  break;
5756 
5757  default:
5758  /* Check for arrays and composites */
5759  if (OidIsValid(get_element_type(typoid)) || typoid == ANYARRAYOID
5760  || typoid == ANYCOMPATIBLEARRAYOID || typoid == RECORDARRAYOID)
5761  {
5762  *outfuncoid = F_ARRAY_OUT;
5763  *tcategory = JSONTYPE_ARRAY;
5764  }
5765  else if (type_is_rowtype(typoid)) /* includes RECORDOID */
5766  {
5767  *outfuncoid = F_RECORD_OUT;
5768  *tcategory = JSONTYPE_COMPOSITE;
5769  }
5770  else
5771  {
5772  /*
5773  * It's probably the general case. But let's look for a cast
5774  * to json (note: not to jsonb even if is_jsonb is true), if
5775  * it's not built-in.
5776  */
5777  *tcategory = JSONTYPE_OTHER;
5778  if (typoid >= FirstNormalObjectId)
5779  {
5780  Oid castfunc;
5781  CoercionPathType ctype;
5782 
5783  ctype = find_coercion_pathway(JSONOID, typoid,
5785  &castfunc);
5786  if (ctype == COERCION_PATH_FUNC && OidIsValid(castfunc))
5787  {
5788  *outfuncoid = castfunc;
5789  *tcategory = JSONTYPE_CAST;
5790  }
5791  else
5792  {
5793  /* non builtin type with no cast */
5794  getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
5795  }
5796  }
5797  else
5798  {
5799  /* any other builtin type */
5800  getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
5801  }
5802  }
5803  break;
5804  }
5805 }
#define OidIsValid(objectId)
Definition: c.h:764
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2741
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2637
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2889
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2503
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:652
#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 630 of file jsonfuncs.c.

632 {
636  errsave(escontext,
637  (errcode(ERRCODE_UNTRANSLATABLE_CHARACTER),
638  errmsg("unsupported Unicode escape sequence"),
640  report_json_context(lex)));
641  else if (error == JSON_SEM_ACTION_FAILED)
642  {
643  /* semantic action function had better have reported something */
644  if (!SOFT_ERROR_OCCURRED(escontext))
645  elog(ERROR, "JSON semantic action function did not provide error information");
646  }
647  else
648  errsave(escontext,
649  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
650  errmsg("invalid input syntax for type %s", "json"),
652  report_json_context(lex)));
653 }
int errdetail_internal(const char *fmt,...)
Definition: elog.c:1229
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define errsave(context,...)
Definition: elog.h:260
#define ERROR
Definition: elog.h:39
char * json_errdetail(JsonParseErrorType error, JsonLexContext *lex)
Definition: jsonapi.c:1172
@ JSON_SEM_ACTION_FAILED
Definition: jsonapi.h:57
@ JSON_UNICODE_CODE_POINT_ZERO
Definition: jsonapi.h:51
@ JSON_UNICODE_UNTRANSLATABLE
Definition: jsonapi.h:54
@ JSON_UNICODE_HIGH_ESCAPE
Definition: jsonapi.h:53
static int report_json_context(JsonLexContext *lex)
Definition: jsonfuncs.c:666
#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 5678 of file jsonfuncs.c.

5679 {
5680  JsonLexContext lex;
5681  JsonParseErrorType result;
5682 
5683  makeJsonLexContext(&lex, json, false);
5684 
5685  /* Lex exactly one token from the input and check its type. */
5686  result = json_lex(&lex);
5687 
5688  if (result == JSON_SUCCESS)
5689  return lex.token_type;
5690 
5691  if (throw_error)
5692  json_errsave_error(result, &lex, NULL);
5693 
5694  return JSON_TOKEN_INVALID; /* invalid json */
5695 }
JsonParseErrorType json_lex(JsonLexContext *lex)
Definition: jsonapi.c:588
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:630
JsonTokenType token_type
Definition: jsonapi.h:86

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

Referenced by ExecEvalJsonIsPredicate().

◆ jsonb_from_text()

Datum jsonb_from_text ( text js,
bool  unique_keys 
)

Definition at line 152 of file jsonb.c.

153 {
154  return jsonb_from_cstring(VARDATA_ANY(js),
155  VARSIZE_ANY_EXHDR(js),
156  unique_keys,
157  NULL);
158 }
static Datum jsonb_from_cstring(char *json, int len, bool unique_keys, Node *escontext)
Definition: jsonb.c:253
#define VARDATA_ANY(PTR)
Definition: varatt.h:324
#define VARSIZE_ANY_EXHDR(PTR)
Definition: varatt.h:317

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

530 {
531  /*
532  * Most callers pass a detoasted datum, but it's not clear that they all
533  * do. pg_detoast_datum_packed() is cheap insurance.
534  */
535  json = pg_detoast_datum_packed(json);
536 
537  return makeJsonLexContextCstringLen(lex,
538  VARDATA_ANY(json),
539  VARSIZE_ANY_EXHDR(json),
541  need_escapes);
542 }
struct varlena * pg_detoast_datum_packed(struct varlena *datum)
Definition: fmgr.c:1864
JsonLexContext * makeJsonLexContextCstringLen(JsonLexContext *lex, char *json, int len, int encoding, bool need_escapes)
Definition: jsonapi.c:153
int GetDatabaseEncoding(void)
Definition: mbutils.c:1268

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

5303 {
5304  JsonbIterator *it;
5305  JsonbValue v;
5307  uint32 flags = 0;
5308 
5309  it = JsonbIteratorInit(&jb->root);
5310 
5311  type = JsonbIteratorNext(&it, &v, false);
5312 
5313  /*
5314  * We iterate over array (scalar internally is represented as array, so,
5315  * we will accept it too) to check all its elements. Flag names are
5316  * chosen the same as jsonb_typeof uses.
5317  */
5318  if (type != WJB_BEGIN_ARRAY)
5319  ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5320  errmsg("wrong flag type, only arrays and scalars are allowed")));
5321 
5322  while ((type = JsonbIteratorNext(&it, &v, false)) == WJB_ELEM)
5323  {
5324  if (v.type != jbvString)
5325  ereport(ERROR,
5326  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5327  errmsg("flag array element is not a string"),
5328  errhint("Possible values are: \"string\", \"numeric\", \"boolean\", \"key\", and \"all\".")));
5329 
5330  if (v.val.string.len == 3 &&
5331  pg_strncasecmp(v.val.string.val, "all", 3) == 0)
5332  flags |= jtiAll;
5333  else if (v.val.string.len == 3 &&
5334  pg_strncasecmp(v.val.string.val, "key", 3) == 0)
5335  flags |= jtiKey;
5336  else if (v.val.string.len == 6 &&
5337  pg_strncasecmp(v.val.string.val, "string", 6) == 0)
5338  flags |= jtiString;
5339  else if (v.val.string.len == 7 &&
5340  pg_strncasecmp(v.val.string.val, "numeric", 7) == 0)
5341  flags |= jtiNumeric;
5342  else if (v.val.string.len == 7 &&
5343  pg_strncasecmp(v.val.string.val, "boolean", 7) == 0)
5344  flags |= jtiBool;
5345  else
5346  ereport(ERROR,
5347  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5348  errmsg("wrong flag in flag array: \"%s\"",
5349  pnstrdup(v.val.string.val, v.val.string.len)),
5350  errhint("Possible values are: \"string\", \"numeric\", \"boolean\", \"key\", and \"all\".")));
5351  }
5352 
5353  /* expect end of array now */
5354  if (type != WJB_END_ARRAY)
5355  elog(ERROR, "unexpected end of flag array");
5356 
5357  /* get final WJB_DONE and free iterator */
5358  type = JsonbIteratorNext(&it, &v, false);
5359  if (type != WJB_DONE)
5360  elog(ERROR, "unexpected end of flag array");
5361 
5362  return flags;
5363 }
unsigned int uint32
Definition: c.h:495
int errhint(const char *fmt,...)
Definition: elog.c:1316
#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:1655
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 508 of file jsonfuncs.c.

510 {
511  JsonParseErrorType result;
512 
513  result = pg_parse_json(lex, sem);
514  if (result != JSON_SUCCESS)
515  {
516  json_errsave_error(result, lex, escontext);
517  return false;
518  }
519  return true;
520 }
JsonParseErrorType pg_parse_json(JsonLexContext *lex, JsonSemAction *sem)
Definition: jsonapi.c:205

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

Referenced by json_in(), and jsonb_from_cstring().

◆ transform_json_string_values()

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

Definition at line 5559 of file jsonfuncs.c.

5561 {
5562  JsonLexContext lex;
5563  JsonSemAction *sem = palloc0(sizeof(JsonSemAction));
5565 
5566  state->lex = makeJsonLexContext(&lex, json, true);
5567  state->strval = makeStringInfo();
5568  state->action = transform_action;
5569  state->action_state = action_state;
5570 
5571  sem->semstate = (void *) state;
5579 
5580  pg_parse_json_or_ereport(&lex, sem);
5581  freeJsonLexContext(&lex);
5582 
5583  return cstring_to_text_with_len(state->strval->data, state->strval->len);
5584 }
static JsonParseErrorType transform_string_values_array_start(void *state)
Definition: jsonfuncs.c:5612
static JsonParseErrorType transform_string_values_object_field_start(void *state, char *fname, bool isnull)
Definition: jsonfuncs.c:5632
static JsonParseErrorType transform_string_values_array_end(void *state)
Definition: jsonfuncs.c:5622
static JsonParseErrorType transform_string_values_object_end(void *state)
Definition: jsonfuncs.c:5602
static JsonParseErrorType transform_string_values_object_start(void *state)
Definition: jsonfuncs.c:5592
static JsonParseErrorType transform_string_values_array_element_start(void *state, bool isnull)
Definition: jsonfuncs.c:5650
static JsonParseErrorType transform_string_values_scalar(void *state, char *token, JsonTokenType tokentype)
Definition: jsonfuncs.c:5661
json_struct_action array_end
Definition: jsonapi.h:123
json_struct_action object_start
Definition: jsonapi.h:120
json_aelem_action array_element_start
Definition: jsonapi.h:126
json_struct_action array_start
Definition: jsonapi.h:122
json_struct_action object_end
Definition: jsonapi.h:121

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

5514 {
5515  JsonbIterator *it;
5516  JsonbValue v,
5517  *res = NULL;
5519  JsonbParseState *st = NULL;
5520  text *out;
5521  bool is_scalar = false;
5522 
5523  it = JsonbIteratorInit(&jsonb->root);
5524  is_scalar = it->isScalar;
5525 
5526  while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
5527  {
5528  if ((type == WJB_VALUE || type == WJB_ELEM) && v.type == jbvString)
5529  {
5530  out = transform_action(action_state, v.val.string.val, v.val.string.len);
5531  /* out is probably not toasted, but let's be sure */
5532  out = pg_detoast_datum_packed(out);
5533  v.val.string.val = VARDATA_ANY(out);
5534  v.val.string.len = VARSIZE_ANY_EXHDR(out);
5535  res = pushJsonbValue(&st, type, type < WJB_BEGIN_ARRAY ? &v : NULL);
5536  }
5537  else
5538  {
5539  res = pushJsonbValue(&st, type, (type == WJB_KEY ||
5540  type == WJB_VALUE ||
5541  type == WJB_ELEM) ? &v : NULL);
5542  }
5543  }
5544 
5545  if (res->type == jbvArray)
5546  res->val.array.rawScalar = is_scalar;
5547 
5548  return JsonbValueToJsonb(res);
5549 }
@ jbvArray
Definition: jsonb.h:233
JsonbValue * pushJsonbValue(JsonbParseState **pstate, JsonbIteratorToken seq, JsonbValue *jbval)
Definition: jsonb_util.c:568
bool isScalar
Definition: jsonb.h:347
Definition: c.h:676

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().