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

5216 {
5217  JsonLexContext *lex = makeJsonLexContext(json, true);
5218  JsonSemAction *sem = palloc0(sizeof(JsonSemAction));
5220 
5221  state->lex = lex;
5222  state->action = action;
5223  state->action_state = action_state;
5224  state->flags = flags;
5225 
5226  sem->semstate = (void *) state;
5229 
5230  pg_parse_json_or_ereport(lex, sem);
5231 }
JsonIterateStringValuesAction action
Definition: jsonfuncs.c:61
static void iterate_values_scalar(void *state, char *token, JsonTokenType tokentype)
Definition: jsonfuncs.c:5238
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:981
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:5264
void * semstate
Definition: jsonapi.h:106

◆ iterate_jsonb_values()

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

Definition at line 5146 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().

5148 {
5149  JsonbIterator *it;
5150  JsonbValue v;
5152 
5153  it = JsonbIteratorInit(&jb->root);
5154 
5155  /*
5156  * Just recursively iterating over jsonb and call callback on all
5157  * corresponding elements
5158  */
5159  while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
5160  {
5161  if (type == WJB_KEY)
5162  {
5163  if (flags & jtiKey)
5164  action(state, v.val.string.val, v.val.string.len);
5165 
5166  continue;
5167  }
5168  else if (!(type == WJB_VALUE || type == WJB_ELEM))
5169  {
5170  /* do not call callback for composite JsonbValue */
5171  continue;
5172  }
5173 
5174  /* JsonbValue is a value of object or element of array */
5175  switch (v.type)
5176  {
5177  case jbvString:
5178  if (flags & jtiString)
5179  action(state, v.val.string.val, v.val.string.len);
5180  break;
5181  case jbvNumeric:
5182  if (flags & jtiNumeric)
5183  {
5184  char *val;
5185 
5187  NumericGetDatum(v.val.numeric)));
5188 
5189  action(state, val, strlen(val));
5190  pfree(val);
5191  }
5192  break;
5193  case jbvBool:
5194  if (flags & jtiBool)
5195  {
5196  if (v.val.boolean)
5197  action(state, "true", 4);
5198  else
5199  action(state, "false", 5);
5200  }
5201  break;
5202  default:
5203  /* do not call callback for composite JsonbValue */
5204  break;
5205  }
5206  }
5207 }
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:738
Definition: jsonb.h:22
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:624
void pfree(void *pointer)
Definition: mcxt.c:1057
#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 5078 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().

5079 {
5080  JsonbIterator *it;
5081  JsonbValue v;
5083  uint32 flags = 0;
5084 
5085  it = JsonbIteratorInit(&jb->root);
5086 
5087  type = JsonbIteratorNext(&it, &v, false);
5088 
5089  /*
5090  * We iterate over array (scalar internally is represented as array, so,
5091  * we will accept it too) to check all its elements. Flag names are
5092  * chosen the same as jsonb_typeof uses.
5093  */
5094  if (type != WJB_BEGIN_ARRAY)
5095  ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5096  errmsg("wrong flag type, only arrays and scalars are allowed")));
5097 
5098  while ((type = JsonbIteratorNext(&it, &v, false)) == WJB_ELEM)
5099  {
5100  if (v.type != jbvString)
5101  ereport(ERROR,
5102  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5103  errmsg("flag array element is not a string"),
5104  errhint("Possible values are: \"string\", \"numeric\", \"boolean\", \"key\", and \"all\".")));
5105 
5106  if (v.val.string.len == 3 &&
5107  pg_strncasecmp(v.val.string.val, "all", 3) == 0)
5108  flags |= jtiAll;
5109  else if (v.val.string.len == 3 &&
5110  pg_strncasecmp(v.val.string.val, "key", 3) == 0)
5111  flags |= jtiKey;
5112  else if (v.val.string.len == 6 &&
5113  pg_strncasecmp(v.val.string.val, "string", 6) == 0)
5114  flags |= jtiString;
5115  else if (v.val.string.len == 7 &&
5116  pg_strncasecmp(v.val.string.val, "numeric", 7) == 0)
5117  flags |= jtiNumeric;
5118  else if (v.val.string.len == 7 &&
5119  pg_strncasecmp(v.val.string.val, "boolean", 7) == 0)
5120  flags |= jtiBool;
5121  else
5122  ereport(ERROR,
5123  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5124  errmsg("wrong flag in flag array: \"%s\"",
5125  pnstrdup(v.val.string.val, v.val.string.len)),
5126  errhint("Possible values are: \"string\", \"numeric\", \"boolean\", \"key\", and \"all\".")));
5127  }
5128 
5129  /* expect end of array now */
5130  if (type != WJB_END_ARRAY)
5131  elog(ERROR, "unexpected end of flag array");
5132 
5133  /* get final WJB_DONE and free iterator */
5134  type = JsonbIteratorNext(&it, &v, false);
5135  if (type != WJB_DONE)
5136  elog(ERROR, "unexpected end of flag array");
5137 
5138  return flags;
5139 }
int errhint(const char *fmt,...)
Definition: elog.c:1071
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1198
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:374
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 5328 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().

5330 {
5331  JsonLexContext *lex = makeJsonLexContext(json, true);
5332  JsonSemAction *sem = palloc0(sizeof(JsonSemAction));
5334 
5335  state->lex = lex;
5336  state->strval = makeStringInfo();
5337  state->action = transform_action;
5338  state->action_state = action_state;
5339 
5340  sem->semstate = (void *) state;
5349 
5350  pg_parse_json_or_ereport(lex, sem);
5351 
5352  return cstring_to_text_with_len(state->strval->data, state->strval->len);
5353 }
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:5393
json_scalar_action scalar
Definition: jsonapi.h:115
static void transform_string_values_scalar(void *state, char *token, JsonTokenType tokentype)
Definition: jsonfuncs.c:5418
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:200
static void transform_string_values_object_end(void *state)
Definition: jsonfuncs.c:5369
static void transform_string_values_array_element_start(void *state, bool isnull)
Definition: jsonfuncs.c:5409
void pg_parse_json_or_ereport(JsonLexContext *lex, JsonSemAction *sem)
Definition: jsonfuncs.c:497
void * palloc0(Size size)
Definition: mcxt.c:981
json_aelem_action array_element_start
Definition: jsonapi.h:113
static void transform_string_values_object_start(void *state)
Definition: jsonfuncs.c:5361
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:5385
JsonTransformStringValuesAction action
Definition: jsonfuncs.c:73
static void transform_string_values_array_start(void *state)
Definition: jsonfuncs.c:5377
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 5283 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().

5285 {
5286  JsonbIterator *it;
5287  JsonbValue v,
5288  *res = NULL;
5290  JsonbParseState *st = NULL;
5291  text *out;
5292  bool is_scalar = false;
5293 
5294  it = JsonbIteratorInit(&jsonb->root);
5295  is_scalar = it->isScalar;
5296 
5297  while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
5298  {
5299  if ((type == WJB_VALUE || type == WJB_ELEM) && v.type == jbvString)
5300  {
5301  out = transform_action(action_state, v.val.string.val, v.val.string.len);
5302  v.val.string.val = VARDATA_ANY(out);
5303  v.val.string.len = VARSIZE_ANY_EXHDR(out);
5304  res = pushJsonbValue(&st, type, type < WJB_BEGIN_ARRAY ? &v : NULL);
5305  }
5306  else
5307  {
5308  res = pushJsonbValue(&st, type, (type == WJB_KEY ||
5309  type == WJB_VALUE ||
5310  type == WJB_ELEM) ? &v : NULL);
5311  }
5312  }
5313 
5314  if (res->type == jbvArray)
5315  res->val.array.rawScalar = is_scalar;
5316 
5317  return JsonbValueToJsonb(res);
5318 }
#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:562
JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
Definition: jsonb_util.c:796
Definition: jsonb.h:25