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

5424 {
5425  JsonLexContext *lex = makeJsonLexContext(json, true);
5426  JsonSemAction *sem = palloc0(sizeof(JsonSemAction));
5428 
5429  state->lex = lex;
5430  state->action = action;
5431  state->action_state = action_state;
5432  state->flags = flags;
5433 
5434  sem->semstate = (void *) state;
5437 
5438  pg_parse_json_or_ereport(lex, sem);
5439 }
JsonIterateStringValuesAction action
Definition: jsonfuncs.c:65
static void iterate_values_scalar(void *state, char *token, JsonTokenType tokentype)
Definition: jsonfuncs.c:5446
JsonLexContext * makeJsonLexContext(text *json, bool need_escapes)
Definition: jsonfuncs.c:517
json_scalar_action scalar
Definition: jsonapi.h:115
void pg_parse_json_or_ereport(JsonLexContext *lex, JsonSemAction *sem)
Definition: jsonfuncs.c:501
void * palloc0(Size size)
Definition: mcxt.c:981
Definition: regguts.h:317
JsonLexContext * lex
Definition: jsonfuncs.c:64
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:5472
void * semstate
Definition: jsonapi.h:106

◆ iterate_jsonb_values()

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

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

5356 {
5357  JsonbIterator *it;
5358  JsonbValue v;
5360 
5361  it = JsonbIteratorInit(&jb->root);
5362 
5363  /*
5364  * Just recursively iterating over jsonb and call callback on all
5365  * corresponding elements
5366  */
5367  while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
5368  {
5369  if (type == WJB_KEY)
5370  {
5371  if (flags & jtiKey)
5372  action(state, v.val.string.val, v.val.string.len);
5373 
5374  continue;
5375  }
5376  else if (!(type == WJB_VALUE || type == WJB_ELEM))
5377  {
5378  /* do not call callback for composite JsonbValue */
5379  continue;
5380  }
5381 
5382  /* JsonbValue is a value of object or element of array */
5383  switch (v.type)
5384  {
5385  case jbvString:
5386  if (flags & jtiString)
5387  action(state, v.val.string.val, v.val.string.len);
5388  break;
5389  case jbvNumeric:
5390  if (flags & jtiNumeric)
5391  {
5392  char *val;
5393 
5395  NumericGetDatum(v.val.numeric)));
5396 
5397  action(state, val, strlen(val));
5398  pfree(val);
5399  }
5400  break;
5401  case jbvBool:
5402  if (flags & jtiBool)
5403  {
5404  if (v.val.boolean)
5405  action(state, "true", 4);
5406  else
5407  action(state, "false", 5);
5408  }
5409  break;
5410  default:
5411  /* do not call callback for composite JsonbValue */
5412  break;
5413  }
5414  }
5415 }
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:739
Definition: jsonb.h:22
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:626
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:812
Definition: regguts.h:317
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:848
Definition: jsonb.h:25

◆ json_ereport_error()

void json_ereport_error ( JsonParseErrorType  error,
JsonLexContext lex 
)

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

612 {
615  ereport(ERROR,
616  (errcode(ERRCODE_UNTRANSLATABLE_CHARACTER),
617  errmsg("unsupported Unicode escape sequence"),
618  errdetail("%s", json_errdetail(error, lex)),
619  report_json_context(lex)));
620  else
621  ereport(ERROR,
622  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
623  errmsg("invalid input syntax for type %s", "json"),
624  errdetail("%s", json_errdetail(error, lex)),
625  report_json_context(lex)));
626 }
static void error(void)
Definition: sql-dyntest.c:147
char * json_errdetail(JsonParseErrorType error, JsonLexContext *lex)
Definition: jsonapi.c:1058
int errcode(int sqlerrcode)
Definition: elog.c:694
#define ERROR
Definition: elog.h:45
int errdetail(const char *fmt,...)
Definition: elog.c:1038
static int report_json_context(JsonLexContext *lex)
Definition: jsonfuncs.c:639
#define ereport(elevel,...)
Definition: elog.h:155
int errmsg(const char *fmt,...)
Definition: elog.c:905

◆ makeJsonLexContext()

JsonLexContext* makeJsonLexContext ( text json,
bool  need_escapes 
)

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

518 {
520  VARSIZE_ANY_EXHDR(json),
522  need_escapes);
523 }
#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 5286 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().

5287 {
5288  JsonbIterator *it;
5289  JsonbValue v;
5291  uint32 flags = 0;
5292 
5293  it = JsonbIteratorInit(&jb->root);
5294 
5295  type = JsonbIteratorNext(&it, &v, false);
5296 
5297  /*
5298  * We iterate over array (scalar internally is represented as array, so,
5299  * we will accept it too) to check all its elements. Flag names are
5300  * chosen the same as jsonb_typeof uses.
5301  */
5302  if (type != WJB_BEGIN_ARRAY)
5303  ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5304  errmsg("wrong flag type, only arrays and scalars are allowed")));
5305 
5306  while ((type = JsonbIteratorNext(&it, &v, false)) == WJB_ELEM)
5307  {
5308  if (v.type != jbvString)
5309  ereport(ERROR,
5310  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5311  errmsg("flag array element is not a string"),
5312  errhint("Possible values are: \"string\", \"numeric\", \"boolean\", \"key\", and \"all\".")));
5313 
5314  if (v.val.string.len == 3 &&
5315  pg_strncasecmp(v.val.string.val, "all", 3) == 0)
5316  flags |= jtiAll;
5317  else if (v.val.string.len == 3 &&
5318  pg_strncasecmp(v.val.string.val, "key", 3) == 0)
5319  flags |= jtiKey;
5320  else if (v.val.string.len == 6 &&
5321  pg_strncasecmp(v.val.string.val, "string", 6) == 0)
5322  flags |= jtiString;
5323  else if (v.val.string.len == 7 &&
5324  pg_strncasecmp(v.val.string.val, "numeric", 7) == 0)
5325  flags |= jtiNumeric;
5326  else if (v.val.string.len == 7 &&
5327  pg_strncasecmp(v.val.string.val, "boolean", 7) == 0)
5328  flags |= jtiBool;
5329  else
5330  ereport(ERROR,
5331  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5332  errmsg("wrong flag in flag array: \"%s\"",
5333  pnstrdup(v.val.string.val, v.val.string.len)),
5334  errhint("Possible values are: \"string\", \"numeric\", \"boolean\", \"key\", and \"all\".")));
5335  }
5336 
5337  /* expect end of array now */
5338  if (type != WJB_END_ARRAY)
5339  elog(ERROR, "unexpected end of flag array");
5340 
5341  /* get final WJB_DONE and free iterator */
5342  type = JsonbIteratorNext(&it, &v, false);
5343  if (type != WJB_DONE)
5344  elog(ERROR, "unexpected end of flag array");
5345 
5346  return flags;
5347 }
int errhint(const char *fmt,...)
Definition: elog.c:1152
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:694
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: pgstrcasecmp.c:69
#define ERROR
Definition: elog.h:45
unsigned int uint32
Definition: c.h:441
JsonbIteratorToken
Definition: jsonb.h:20
JsonbContainer root
Definition: jsonb.h:223
#define ereport(elevel,...)
Definition: elog.h:155
JsonbIterator * JsonbIteratorInit(JsonbContainer *container)
Definition: jsonb_util.c:812
enum jbvType type
Definition: jsonb.h:263
int errmsg(const char *fmt,...)
Definition: elog.c:905
#define elog(elevel,...)
Definition: elog.h:227
JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
Definition: jsonb_util.c:848
Definition: jsonb.h:25

◆ pg_parse_json_or_ereport()

void pg_parse_json_or_ereport ( JsonLexContext lex,
JsonSemAction sem 
)

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

502 {
503  JsonParseErrorType result;
504 
505  result = pg_parse_json(lex, sem);
506  if (result != JSON_SUCCESS)
507  json_ereport_error(result, lex);
508 }
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:611

◆ transform_json_string_values()

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

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

5538 {
5539  JsonLexContext *lex = makeJsonLexContext(json, true);
5540  JsonSemAction *sem = palloc0(sizeof(JsonSemAction));
5542 
5543  state->lex = lex;
5544  state->strval = makeStringInfo();
5545  state->action = transform_action;
5546  state->action_state = action_state;
5547 
5548  sem->semstate = (void *) state;
5557 
5558  pg_parse_json_or_ereport(lex, sem);
5559 
5560  return cstring_to_text_with_len(state->strval->data, state->strval->len);
5561 }
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:517
static void transform_string_values_object_field_start(void *state, char *fname, bool isnull)
Definition: jsonfuncs.c:5601
json_scalar_action scalar
Definition: jsonapi.h:115
static void transform_string_values_scalar(void *state, char *token, JsonTokenType tokentype)
Definition: jsonfuncs.c:5626
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:201
static void transform_string_values_object_end(void *state)
Definition: jsonfuncs.c:5577
static void transform_string_values_array_element_start(void *state, bool isnull)
Definition: jsonfuncs.c:5617
void pg_parse_json_or_ereport(JsonLexContext *lex, JsonSemAction *sem)
Definition: jsonfuncs.c:501
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:5569
json_struct_action array_start
Definition: jsonapi.h:109
Definition: regguts.h:317
static void transform_string_values_array_end(void *state)
Definition: jsonfuncs.c:5593
JsonTransformStringValuesAction action
Definition: jsonfuncs.c:77
static void transform_string_values_array_start(void *state)
Definition: jsonfuncs.c:5585
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 5491 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().

5493 {
5494  JsonbIterator *it;
5495  JsonbValue v,
5496  *res = NULL;
5498  JsonbParseState *st = NULL;
5499  text *out;
5500  bool is_scalar = false;
5501 
5502  it = JsonbIteratorInit(&jsonb->root);
5503  is_scalar = it->isScalar;
5504 
5505  while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
5506  {
5507  if ((type == WJB_VALUE || type == WJB_ELEM) && v.type == jbvString)
5508  {
5509  out = transform_action(action_state, v.val.string.val, v.val.string.len);
5510  v.val.string.val = VARDATA_ANY(out);
5511  v.val.string.len = VARSIZE_ANY_EXHDR(out);
5512  res = pushJsonbValue(&st, type, type < WJB_BEGIN_ARRAY ? &v : NULL);
5513  }
5514  else
5515  {
5516  res = pushJsonbValue(&st, type, (type == WJB_KEY ||
5517  type == WJB_VALUE ||
5518  type == WJB_ELEM) ? &v : NULL);
5519  }
5520  }
5521 
5522  if (res->type == jbvArray)
5523  res->val.array.rawScalar = is_scalar;
5524 
5525  return JsonbValueToJsonb(res);
5526 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
Jsonb * JsonbValueToJsonb(JsonbValue *val)
Definition: jsonb_util.c:92
char * val
Definition: jsonb.h:272
Definition: jsonb.h:22
JsonbValue * pushJsonbValue(JsonbParseState **pstate, JsonbIteratorToken seq, JsonbValue *jbval)
Definition: jsonb_util.c:566
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:812
enum jbvType type
Definition: jsonb.h:263
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:621
JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
Definition: jsonb_util.c:848
Definition: jsonb.h:25