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.

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
}
 

Functions

JsonLexContextmakeJsonLexContext (text *json, bool need_escapes)
 
void pg_parse_json_or_ereport (JsonLexContext *lex, JsonSemAction *sem)
 
void json_ereport_error (JsonParseErrorType error, JsonLexContext *lex)
 
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)
 

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

Function Documentation

◆ iterate_json_values()

void iterate_json_values ( text json,
uint32  flags,
void *  action_state,
JsonIterateStringValuesAction  action 
)

Definition at line 5216 of file jsonfuncs.c.

References IterateJsonStringValuesState::action, generate_unaccent_rules::action, IterateJsonStringValuesState::action_state, IterateJsonStringValuesState::flags, iterate_values_object_field_start(), iterate_values_scalar(), OkeysState::lex, IterateJsonStringValuesState::lex, makeJsonLexContext(), JsonSemAction::object_field_start, palloc0(), pg_parse_json_or_ereport(), JsonSemAction::scalar, and JsonSemAction::semstate.

Referenced by json_to_tsvector_worker().

5218 {
5219  JsonLexContext *lex = makeJsonLexContext(json, true);
5220  JsonSemAction *sem = palloc0(sizeof(JsonSemAction));
5222 
5223  state->lex = lex;
5224  state->action = action;
5225  state->action_state = action_state;
5226  state->flags = flags;
5227 
5228  sem->semstate = (void *) state;
5231 
5232  pg_parse_json_or_ereport(lex, sem);
5233 }
JsonIterateStringValuesAction action
Definition: jsonfuncs.c:61
static void iterate_values_scalar(void *state, char *token, JsonTokenType tokentype)
Definition: jsonfuncs.c:5240
JsonLexContext * makeJsonLexContext(text *json, bool need_escapes)
Definition: jsonfuncs.c:513
json_scalar_action scalar
Definition: jsonapi.h:115
void pg_parse_json_or_ereport(JsonLexContext *lex, JsonSemAction *sem)
Definition: jsonfuncs.c:497
void * palloc0(Size size)
Definition: mcxt.c:980
Definition: regguts.h:298
JsonLexContext * lex
Definition: jsonfuncs.c:60
json_ofield_action object_field_start
Definition: jsonapi.h:111
static void iterate_values_object_field_start(void *state, char *fname, bool isnull)
Definition: jsonfuncs.c:5266
void * semstate
Definition: jsonapi.h:106

◆ iterate_jsonb_values()

void iterate_jsonb_values ( Jsonb jb,
uint32  flags,
void *  state,
JsonIterateStringValuesAction  action 
)

Definition at line 5148 of file jsonfuncs.c.

References generate_unaccent_rules::action, DatumGetCString, DirectFunctionCall1, jbvBool, jbvNumeric, jbvString, JsonbIteratorInit(), JsonbIteratorNext(), jtiBool, jtiKey, jtiNumeric, jtiString, numeric_out(), NumericGetDatum, pfree(), Jsonb::root, JsonbValue::type, generate_unaccent_rules::type, JsonbValue::val, val, WJB_DONE, WJB_ELEM, WJB_KEY, and WJB_VALUE.

Referenced by jsonb_to_tsvector_worker().

5150 {
5151  JsonbIterator *it;
5152  JsonbValue v;
5154 
5155  it = JsonbIteratorInit(&jb->root);
5156 
5157  /*
5158  * Just recursively iterating over jsonb and call callback on all
5159  * corresponding elements
5160  */
5161  while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
5162  {
5163  if (type == WJB_KEY)
5164  {
5165  if (flags & jtiKey)
5166  action(state, v.val.string.val, v.val.string.len);
5167 
5168  continue;
5169  }
5170  else if (!(type == WJB_VALUE || type == WJB_ELEM))
5171  {
5172  /* do not call callback for composite JsonbValue */
5173  continue;
5174  }
5175 
5176  /* JsonbValue is a value of object or element of array */
5177  switch (v.type)
5178  {
5179  case jbvString:
5180  if (flags & jtiString)
5181  action(state, v.val.string.val, v.val.string.len);
5182  break;
5183  case jbvNumeric:
5184  if (flags & jtiNumeric)
5185  {
5186  char *val;
5187 
5189  NumericGetDatum(v.val.numeric)));
5190 
5191  action(state, val, strlen(val));
5192  pfree(val);
5193  }
5194  break;
5195  case jbvBool:
5196  if (flags & jtiBool)
5197  {
5198  if (v.val.boolean)
5199  action(state, "true", 4);
5200  else
5201  action(state, "false", 5);
5202  }
5203  break;
5204  default:
5205  /* do not call callback for composite JsonbValue */
5206  break;
5207  }
5208  }
5209 }
char * val
Definition: jsonb.h:272
#define NumericGetDatum(X)
Definition: numeric.h:51
Definition: jsonb.h:239
Datum numeric_out(PG_FUNCTION_ARGS)
Definition: numeric.c:649
Definition: jsonb.h:22
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:624
void pfree(void *pointer)
Definition: mcxt.c:1056
#define DatumGetCString(X)
Definition: postgres.h:566
Definition: jsonb.h:23
JsonbIteratorToken
Definition: jsonb.h:20
JsonbContainer root
Definition: jsonb.h:223
JsonbIterator * JsonbIteratorInit(JsonbContainer *container)
Definition: jsonb_util.c:760
Definition: regguts.h:298
enum jbvType type
Definition: jsonb.h:263
long val
Definition: informix.c:664
JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
Definition: jsonb_util.c:796
Definition: jsonb.h:25

◆ json_ereport_error()

void json_ereport_error ( JsonParseErrorType  error,
JsonLexContext lex 
)

Definition at line 607 of file jsonfuncs.c.

References ereport, errcode(), errdetail(), errmsg(), ERROR, json_errdetail(), JSON_UNICODE_CODE_POINT_ZERO, JSON_UNICODE_HIGH_ESCAPE, and report_json_context().

Referenced by get_array_start(), json_typeof(), and pg_parse_json_or_ereport().

608 {
611  ereport(ERROR,
612  (errcode(ERRCODE_UNTRANSLATABLE_CHARACTER),
613  errmsg("unsupported Unicode escape sequence"),
614  errdetail("%s", json_errdetail(error, lex)),
615  report_json_context(lex)));
616  else
617  ereport(ERROR,
618  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
619  errmsg("invalid input syntax for type %s", "json"),
620  errdetail("%s", json_errdetail(error, lex)),
621  report_json_context(lex)));
622 }
static void error(void)
Definition: sql-dyntest.c:147
char * json_errdetail(JsonParseErrorType error, JsonLexContext *lex)
Definition: jsonapi.c:1056
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:957
static int report_json_context(JsonLexContext *lex)
Definition: jsonfuncs.c:635
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824

◆ makeJsonLexContext()

JsonLexContext* makeJsonLexContext ( text json,
bool  need_escapes 
)

Definition at line 513 of file jsonfuncs.c.

References GetDatabaseEncoding(), makeJsonLexContextCstringLen(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by datum_to_jsonb(), each_worker(), elements_worker(), get_worker(), iterate_json_values(), json_array_length(), json_in(), json_object_keys(), json_strip_nulls(), json_typeof(), populate_recordset_worker(), and transform_json_string_values().

514 {
516  VARSIZE_ANY_EXHDR(json),
518  need_escapes);
519 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
JsonLexContext * makeJsonLexContextCstringLen(char *json, int len, int encoding, bool need_escapes)
Definition: jsonapi.c:155
int GetDatabaseEncoding(void)
Definition: mbutils.c:1151
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341

◆ parse_jsonb_index_flags()

uint32 parse_jsonb_index_flags ( Jsonb jb)

Definition at line 5080 of file jsonfuncs.c.

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

5081 {
5082  JsonbIterator *it;
5083  JsonbValue v;
5085  uint32 flags = 0;
5086 
5087  it = JsonbIteratorInit(&jb->root);
5088 
5089  type = JsonbIteratorNext(&it, &v, false);
5090 
5091  /*
5092  * We iterate over array (scalar internally is represented as array, so,
5093  * we will accept it too) to check all its elements. Flag names are
5094  * chosen the same as jsonb_typeof uses.
5095  */
5096  if (type != WJB_BEGIN_ARRAY)
5097  ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5098  errmsg("wrong flag type, only arrays and scalars are allowed")));
5099 
5100  while ((type = JsonbIteratorNext(&it, &v, false)) == WJB_ELEM)
5101  {
5102  if (v.type != jbvString)
5103  ereport(ERROR,
5104  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5105  errmsg("flag array element is not a string"),
5106  errhint("Possible values are: \"string\", \"numeric\", \"boolean\", \"key\", and \"all\".")));
5107 
5108  if (v.val.string.len == 3 &&
5109  pg_strncasecmp(v.val.string.val, "all", 3) == 0)
5110  flags |= jtiAll;
5111  else if (v.val.string.len == 3 &&
5112  pg_strncasecmp(v.val.string.val, "key", 3) == 0)
5113  flags |= jtiKey;
5114  else if (v.val.string.len == 6 &&
5115  pg_strncasecmp(v.val.string.val, "string", 6) == 0)
5116  flags |= jtiString;
5117  else if (v.val.string.len == 7 &&
5118  pg_strncasecmp(v.val.string.val, "numeric", 7) == 0)
5119  flags |= jtiNumeric;
5120  else if (v.val.string.len == 7 &&
5121  pg_strncasecmp(v.val.string.val, "boolean", 7) == 0)
5122  flags |= jtiBool;
5123  else
5124  ereport(ERROR,
5125  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5126  errmsg("wrong flag in flag array: \"%s\"",
5127  pnstrdup(v.val.string.val, v.val.string.len)),
5128  errhint("Possible values are: \"string\", \"numeric\", \"boolean\", \"key\", and \"all\".")));
5129  }
5130 
5131  /* expect end of array now */
5132  if (type != WJB_END_ARRAY)
5133  elog(ERROR, "unexpected end of flag array");
5134 
5135  /* get final WJB_DONE and free iterator */
5136  type = JsonbIteratorNext(&it, &v, false);
5137  if (type != WJB_DONE)
5138  elog(ERROR, "unexpected end of flag array");
5139 
5140  return flags;
5141 }
int errhint(const char *fmt,...)
Definition: elog.c:1071
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1197
char * val
Definition: jsonb.h:272
Definition: jsonb.h:22
int errcode(int sqlerrcode)
Definition: elog.c:610
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: pgstrcasecmp.c:69
#define ERROR
Definition: elog.h:43
unsigned int uint32
Definition: c.h:367
JsonbIteratorToken
Definition: jsonb.h:20
JsonbContainer root
Definition: jsonb.h:223
#define ereport(elevel,...)
Definition: elog.h:144
JsonbIterator * JsonbIteratorInit(JsonbContainer *container)
Definition: jsonb_util.c:760
enum jbvType type
Definition: jsonb.h:263
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
Definition: jsonb_util.c:796
Definition: jsonb.h:25

◆ pg_parse_json_or_ereport()

void pg_parse_json_or_ereport ( JsonLexContext lex,
JsonSemAction sem 
)

Definition at line 497 of file jsonfuncs.c.

References json_ereport_error(), JSON_SUCCESS, pg_parse_json(), and OkeysState::result.

Referenced by datum_to_jsonb(), each_worker(), elements_worker(), get_json_object_as_hash(), get_worker(), iterate_json_values(), json_array_length(), json_in(), json_object_keys(), json_recv(), json_strip_nulls(), jsonb_from_cstring(), populate_array_json(), populate_recordset_worker(), and transform_json_string_values().

498 {
499  JsonParseErrorType result;
500 
501  result = pg_parse_json(lex, sem);
502  if (result != JSON_SUCCESS)
503  json_ereport_error(result, lex);
504 }
JsonParseErrorType pg_parse_json(JsonLexContext *lex, JsonSemAction *sem)
Definition: jsonapi.c:179
JsonParseErrorType
Definition: jsonapi.h:36
void json_ereport_error(JsonParseErrorType error, JsonLexContext *lex)
Definition: jsonfuncs.c:607

◆ transform_json_string_values()

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

Definition at line 5330 of file jsonfuncs.c.

References TransformJsonStringValuesState::action, TransformJsonStringValuesState::action_state, JsonSemAction::array_element_start, JsonSemAction::array_end, JsonSemAction::array_start, cstring_to_text_with_len(), StringInfoData::data, StringInfoData::len, OkeysState::lex, TransformJsonStringValuesState::lex, makeJsonLexContext(), makeStringInfo(), JsonSemAction::object_end, JsonSemAction::object_field_start, JsonSemAction::object_start, palloc0(), pg_parse_json_or_ereport(), JsonSemAction::scalar, JsonSemAction::semstate, TransformJsonStringValuesState::strval, 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().

5332 {
5333  JsonLexContext *lex = makeJsonLexContext(json, true);
5334  JsonSemAction *sem = palloc0(sizeof(JsonSemAction));
5336 
5337  state->lex = lex;
5338  state->strval = makeStringInfo();
5339  state->action = transform_action;
5340  state->action_state = action_state;
5341 
5342  sem->semstate = (void *) state;
5351 
5352  pg_parse_json_or_ereport(lex, sem);
5353 
5354  return cstring_to_text_with_len(state->strval->data, state->strval->len);
5355 }
json_struct_action array_end
Definition: jsonapi.h:110
json_struct_action object_end
Definition: jsonapi.h:108
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
json_struct_action object_start
Definition: jsonapi.h:107
JsonLexContext * makeJsonLexContext(text *json, bool need_escapes)
Definition: jsonfuncs.c:513
static void transform_string_values_object_field_start(void *state, char *fname, bool isnull)
Definition: jsonfuncs.c:5395
json_scalar_action scalar
Definition: jsonapi.h:115
static void transform_string_values_scalar(void *state, char *token, JsonTokenType tokentype)
Definition: jsonfuncs.c:5420
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:184
static void transform_string_values_object_end(void *state)
Definition: jsonfuncs.c:5371
static void transform_string_values_array_element_start(void *state, bool isnull)
Definition: jsonfuncs.c:5411
void pg_parse_json_or_ereport(JsonLexContext *lex, JsonSemAction *sem)
Definition: jsonfuncs.c:497
void * palloc0(Size size)
Definition: mcxt.c:980
json_aelem_action array_element_start
Definition: jsonapi.h:113
static void transform_string_values_object_start(void *state)
Definition: jsonfuncs.c:5363
json_struct_action array_start
Definition: jsonapi.h:109
Definition: regguts.h:298
static void transform_string_values_array_end(void *state)
Definition: jsonfuncs.c:5387
JsonTransformStringValuesAction action
Definition: jsonfuncs.c:73
static void transform_string_values_array_start(void *state)
Definition: jsonfuncs.c:5379
json_ofield_action object_field_start
Definition: jsonapi.h:111
void * semstate
Definition: jsonapi.h:106

◆ transform_jsonb_string_values()

Jsonb* transform_jsonb_string_values ( Jsonb jsonb,
void *  action_state,
JsonTransformStringValuesAction  transform_action 
)

Definition at line 5285 of file jsonfuncs.c.

References JsonbIterator::isScalar, jbvArray, jbvString, JsonbIteratorInit(), JsonbIteratorNext(), JsonbValueToJsonb(), pushJsonbValue(), Jsonb::root, JsonbValue::type, generate_unaccent_rules::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().

5287 {
5288  JsonbIterator *it;
5289  JsonbValue v,
5290  *res = NULL;
5292  JsonbParseState *st = NULL;
5293  text *out;
5294  bool is_scalar = false;
5295 
5296  it = JsonbIteratorInit(&jsonb->root);
5297  is_scalar = it->isScalar;
5298 
5299  while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
5300  {
5301  if ((type == WJB_VALUE || type == WJB_ELEM) && v.type == jbvString)
5302  {
5303  out = transform_action(action_state, v.val.string.val, v.val.string.len);
5304  v.val.string.val = VARDATA_ANY(out);
5305  v.val.string.len = VARSIZE_ANY_EXHDR(out);
5306  res = pushJsonbValue(&st, type, type < WJB_BEGIN_ARRAY ? &v : NULL);
5307  }
5308  else
5309  {
5310  res = pushJsonbValue(&st, type, (type == WJB_KEY ||
5311  type == WJB_VALUE ||
5312  type == WJB_ELEM) ? &v : NULL);
5313  }
5314  }
5315 
5316  if (res->type == jbvArray)
5317  res->val.array.rawScalar = is_scalar;
5318 
5319  return JsonbValueToJsonb(res);
5320 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
Jsonb * JsonbValueToJsonb(JsonbValue *val)
Definition: jsonb_util.c:85
char * val
Definition: jsonb.h:272
Definition: jsonb.h:22
JsonbValue * pushJsonbValue(JsonbParseState **pstate, JsonbIteratorToken seq, JsonbValue *jbval)
Definition: jsonb_util.c:559
Definition: jsonb.h:23
JsonbIteratorToken
Definition: jsonb.h:20
JsonbContainer root
Definition: jsonb.h:223
bool isScalar
Definition: jsonb.h:353
JsonbIterator * JsonbIteratorInit(JsonbContainer *container)
Definition: jsonb_util.c:760
enum jbvType type
Definition: jsonb.h:263
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:555
JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
Definition: jsonb_util.c:796
Definition: jsonb.h:25