PostgreSQL Source Code  git master
jsonapi.h File Reference
#include "jsonb.h"
#include "lib/stringinfo.h"
Include dependency graph for jsonapi.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  JsonLexContext
 
struct  JsonSemAction
 

Typedefs

typedef struct JsonLexContext JsonLexContext
 
typedef void(* json_struct_action) (void *state)
 
typedef void(* json_ofield_action) (void *state, char *fname, bool isnull)
 
typedef void(* json_aelem_action) (void *state, bool isnull)
 
typedef void(* json_scalar_action) (void *state, char *token, JsonTokenType tokentype)
 
typedef struct JsonSemAction JsonSemAction
 
typedef void(* JsonIterateStringValuesAction) (void *state, char *elem_value, int elem_len)
 
typedef text *(* JsonTransformStringValuesAction) (void *state, char *elem_value, int elem_len)
 

Enumerations

enum  JsonTokenType {
  JSON_TOKEN_INVALID, JSON_TOKEN_STRING, JSON_TOKEN_NUMBER, JSON_TOKEN_OBJECT_START,
  JSON_TOKEN_OBJECT_END, JSON_TOKEN_ARRAY_START, JSON_TOKEN_ARRAY_END, JSON_TOKEN_COMMA,
  JSON_TOKEN_COLON, JSON_TOKEN_TRUE, JSON_TOKEN_FALSE, JSON_TOKEN_NULL,
  JSON_TOKEN_END
}
 

Functions

void pg_parse_json (JsonLexContext *lex, JsonSemAction *sem)
 
int json_count_array_elements (JsonLexContext *lex)
 
JsonLexContextmakeJsonLexContext (text *json, bool need_escapes)
 
JsonLexContextmakeJsonLexContextCstringLen (char *json, int len, bool need_escapes)
 
bool IsValidJsonNumber (const char *str, int len)
 
void iterate_jsonb_string_values (Jsonb *jb, void *state, JsonIterateStringValuesAction action)
 
void iterate_json_string_values (text *json, 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

◆ json_aelem_action

typedef void(* json_aelem_action) (void *state, bool isnull)

Definition at line 67 of file jsonapi.h.

◆ json_ofield_action

typedef void(* json_ofield_action) (void *state, char *fname, bool isnull)

Definition at line 66 of file jsonapi.h.

◆ json_scalar_action

typedef void(* json_scalar_action) (void *state, char *token, JsonTokenType tokentype)

Definition at line 68 of file jsonapi.h.

◆ json_struct_action

typedef void(* json_struct_action) (void *state)

Definition at line 65 of file jsonapi.h.

◆ JsonIterateStringValuesAction

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

Definition at line 136 of file jsonapi.h.

◆ JsonLexContext

◆ JsonSemAction

◆ JsonTransformStringValuesAction

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

Definition at line 139 of file jsonapi.h.

Enumeration Type Documentation

◆ JsonTokenType

Enumerator
JSON_TOKEN_INVALID 
JSON_TOKEN_STRING 
JSON_TOKEN_NUMBER 
JSON_TOKEN_OBJECT_START 
JSON_TOKEN_OBJECT_END 
JSON_TOKEN_ARRAY_START 
JSON_TOKEN_ARRAY_END 
JSON_TOKEN_COMMA 
JSON_TOKEN_COLON 
JSON_TOKEN_TRUE 
JSON_TOKEN_FALSE 
JSON_TOKEN_NULL 
JSON_TOKEN_END 

Definition at line 20 of file jsonapi.h.

Function Documentation

◆ IsValidJsonNumber()

bool IsValidJsonNumber ( const char *  str,
int  len 
)

Definition at line 193 of file json.c.

References JsonLexContext::input, JsonLexContext::input_length, and json_lex_number().

Referenced by datum_to_json(), hstore_to_json_loose(), and hstore_to_jsonb_loose().

194 {
195  bool numeric_error;
196  int total_len;
197  JsonLexContext dummy_lex;
198 
199  if (len <= 0)
200  return false;
201 
202  /*
203  * json_lex_number expects a leading '-' to have been eaten already.
204  *
205  * having to cast away the constness of str is ugly, but there's not much
206  * easy alternative.
207  */
208  if (*str == '-')
209  {
210  dummy_lex.input = (char *) str + 1;
211  dummy_lex.input_length = len - 1;
212  }
213  else
214  {
215  dummy_lex.input = (char *) str;
216  dummy_lex.input_length = len;
217  }
218 
219  json_lex_number(&dummy_lex, dummy_lex.input, &numeric_error, &total_len);
220 
221  return (!numeric_error) && (total_len == dummy_lex.input_length);
222 }
static void json_lex_number(JsonLexContext *lex, char *s, bool *num_err, int *total_len)
Definition: json.c:1028
int input_length
Definition: jsonapi.h:54
char * input
Definition: jsonapi.h:53

◆ iterate_json_string_values()

void iterate_json_string_values ( text json,
void *  action_state,
JsonIterateStringValuesAction  action 
)

Definition at line 4968 of file jsonfuncs.c.

References IterateJsonStringValuesState::action, generate_unaccent_rules::action, IterateJsonStringValuesState::action_state, iterate_string_values_scalar(), OkeysState::lex, IterateJsonStringValuesState::lex, makeJsonLexContext(), palloc0(), pg_parse_json(), JsonSemAction::scalar, and JsonSemAction::semstate.

Referenced by json_to_tsvector_byid().

4969 {
4970  JsonLexContext *lex = makeJsonLexContext(json, true);
4971  JsonSemAction *sem = palloc0(sizeof(JsonSemAction));
4973 
4974  state->lex = lex;
4975  state->action = action;
4976  state->action_state = action_state;
4977 
4978  sem->semstate = (void *) state;
4980 
4981  pg_parse_json(lex, sem);
4982 }
JsonIterateStringValuesAction action
Definition: jsonfuncs.c:60
json_scalar_action scalar
Definition: jsonapi.h:93
JsonLexContext * makeJsonLexContext(text *json, bool need_escapes)
Definition: json.c:301
void pg_parse_json(JsonLexContext *lex, JsonSemAction *sem)
Definition: json.c:332
void * palloc0(Size size)
Definition: mcxt.c:864
Definition: regguts.h:298
static void iterate_string_values_scalar(void *state, char *token, JsonTokenType tokentype)
Definition: jsonfuncs.c:4989
JsonLexContext * lex
Definition: jsonfuncs.c:59
void * semstate
Definition: jsonapi.h:84

◆ iterate_jsonb_string_values()

void iterate_jsonb_string_values ( Jsonb jb,
void *  state,
JsonIterateStringValuesAction  action 
)

Definition at line 4946 of file jsonfuncs.c.

References generate_unaccent_rules::action, jbvString, JsonbIteratorInit(), JsonbIteratorNext(), Jsonb::root, generate_unaccent_rules::type, JsonbValue::type, JsonbValue::val, WJB_DONE, WJB_ELEM, and WJB_VALUE.

Referenced by jsonb_to_tsvector_byid().

4947 {
4948  JsonbIterator *it;
4949  JsonbValue v;
4951 
4952  it = JsonbIteratorInit(&jb->root);
4953 
4954  while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
4955  {
4956  if ((type == WJB_VALUE || type == WJB_ELEM) && v.type == jbvString)
4957  {
4958  action(state, v.val.string.val, v.val.string.len);
4959  }
4960  }
4961 }
char * val
Definition: jsonb.h:259
Definition: jsonb.h:22
JsonbIteratorToken
Definition: jsonb.h:20
JsonbContainer root
Definition: jsonb.h:218
JsonbIterator * JsonbIteratorInit(JsonbContainer *container)
Definition: jsonb_util.c:718
Definition: regguts.h:298
enum jbvType type
Definition: jsonb.h:250
JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
Definition: jsonb_util.c:754
Definition: jsonb.h:25

◆ json_count_array_elements()

int json_count_array_elements ( JsonLexContext lex)

Definition at line 367 of file json.c.

References JSON_PARSE_ARRAY_NEXT, JSON_PARSE_ARRAY_START, JSON_TOKEN_ARRAY_END, JSON_TOKEN_ARRAY_START, JSON_TOKEN_COMMA, lex_accept(), lex_expect(), JsonLexContext::lex_level, lex_peek(), parse_array_element(), and JsonLexContext::strval.

Referenced by get_array_start().

368 {
369  JsonLexContext copylex;
370  int count;
371 
372  /*
373  * It's safe to do this with a shallow copy because the lexical routines
374  * don't scribble on the input. They do scribble on the other pointers
375  * etc, so doing this with a copy makes that safe.
376  */
377  memcpy(&copylex, lex, sizeof(JsonLexContext));
378  copylex.strval = NULL; /* not interested in values here */
379  copylex.lex_level++;
380 
381  count = 0;
383  if (lex_peek(&copylex) != JSON_TOKEN_ARRAY_END)
384  {
385  do
386  {
387  count++;
389  }
390  while (lex_accept(&copylex, JSON_TOKEN_COMMA, NULL));
391  }
393 
394  return count;
395 }
static void lex_expect(JsonParseContext ctx, JsonLexContext *lex, JsonTokenType token)
Definition: json.c:173
static bool lex_accept(JsonLexContext *lex, JsonTokenType token, char **lexeme)
Definition: json.c:139
int lex_level
Definition: jsonapi.h:59
static void parse_array_element(JsonLexContext *lex, JsonSemAction *sem)
Definition: json.c:539
StringInfo strval
Definition: jsonapi.h:62
static JsonTokenType lex_peek(JsonLexContext *lex)
Definition: json.c:124
static JsonSemAction nullSemAction
Definition: json.c:110

◆ makeJsonLexContext()

JsonLexContext* makeJsonLexContext ( text json,
bool  need_escapes 
)

Definition at line 301 of file json.c.

References makeJsonLexContextCstringLen(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

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

302 {
304  VARSIZE_ANY_EXHDR(json),
305  need_escapes);
306 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
JsonLexContext * makeJsonLexContextCstringLen(char *json, int len, bool need_escapes)
Definition: json.c:309
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340

◆ makeJsonLexContextCstringLen()

JsonLexContext* makeJsonLexContextCstringLen ( char *  json,
int  len,
bool  need_escapes 
)

Definition at line 309 of file json.c.

References JsonLexContext::input, JsonLexContext::input_length, JsonLexContext::line_number, JsonLexContext::line_start, makeStringInfo(), palloc0(), JsonLexContext::strval, and JsonLexContext::token_terminator.

Referenced by get_json_object_as_hash(), json_recv(), jsonb_from_cstring(), makeJsonLexContext(), and populate_array_json().

310 {
311  JsonLexContext *lex = palloc0(sizeof(JsonLexContext));
312 
313  lex->input = lex->token_terminator = lex->line_start = json;
314  lex->line_number = 1;
315  lex->input_length = len;
316  if (need_escapes)
317  lex->strval = makeStringInfo();
318  return lex;
319 }
int line_number
Definition: jsonapi.h:60
StringInfo makeStringInfo(void)
Definition: stringinfo.c:28
char * line_start
Definition: jsonapi.h:61
int input_length
Definition: jsonapi.h:54
StringInfo strval
Definition: jsonapi.h:62
void * palloc0(Size size)
Definition: mcxt.c:864
char * token_terminator
Definition: jsonapi.h:56
char * input
Definition: jsonapi.h:53

◆ pg_parse_json()

void pg_parse_json ( JsonLexContext lex,
JsonSemAction sem 
)

Definition at line 332 of file json.c.

References json_lex(), JSON_PARSE_END, JSON_TOKEN_ARRAY_START, JSON_TOKEN_END, JSON_TOKEN_OBJECT_START, lex_expect(), lex_peek(), parse_array(), parse_object(), and parse_scalar().

Referenced by datum_to_jsonb(), each_worker(), elements_worker(), get_json_object_as_hash(), get_worker(), iterate_json_string_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().

333 {
334  JsonTokenType tok;
335 
336  /* get the initial token */
337  json_lex(lex);
338 
339  tok = lex_peek(lex);
340 
341  /* parse by recursive descent */
342  switch (tok)
343  {
345  parse_object(lex, sem);
346  break;
348  parse_array(lex, sem);
349  break;
350  default:
351  parse_scalar(lex, sem); /* json can be a bare scalar */
352  }
353 
355 
356 }
static void lex_expect(JsonParseContext ctx, JsonLexContext *lex, JsonTokenType token)
Definition: json.c:173
static void parse_array(JsonLexContext *lex, JsonSemAction *sem)
Definition: json.c:570
static JsonTokenType lex_peek(JsonLexContext *lex)
Definition: json.c:124
static void json_lex(JsonLexContext *lex)
Definition: json.c:614
static void parse_scalar(JsonLexContext *lex, JsonSemAction *sem)
Definition: json.c:407
static void parse_object(JsonLexContext *lex, JsonSemAction *sem)
Definition: json.c:489
JsonTokenType
Definition: jsonapi.h:20

◆ transform_json_string_values()

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

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

5051 {
5052  JsonLexContext *lex = makeJsonLexContext(json, true);
5053  JsonSemAction *sem = palloc0(sizeof(JsonSemAction));
5055 
5056  state->lex = lex;
5057  state->strval = makeStringInfo();
5058  state->action = transform_action;
5059  state->action_state = action_state;
5060 
5061  sem->semstate = (void *) state;
5070 
5071  pg_parse_json(lex, sem);
5072 
5073  return cstring_to_text_with_len(state->strval->data, state->strval->len);
5074 }
json_struct_action array_end
Definition: jsonapi.h:88
json_struct_action object_end
Definition: jsonapi.h:86
StringInfo makeStringInfo(void)
Definition: stringinfo.c:28
json_struct_action object_start
Definition: jsonapi.h:85
static void transform_string_values_object_field_start(void *state, char *fname, bool isnull)
Definition: jsonfuncs.c:5114
json_scalar_action scalar
Definition: jsonapi.h:93
static void transform_string_values_scalar(void *state, char *token, JsonTokenType tokentype)
Definition: jsonfuncs.c:5139
JsonLexContext * makeJsonLexContext(text *json, bool need_escapes)
Definition: json.c:301
void pg_parse_json(JsonLexContext *lex, JsonSemAction *sem)
Definition: json.c:332
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:162
static void transform_string_values_object_end(void *state)
Definition: jsonfuncs.c:5090
static void transform_string_values_array_element_start(void *state, bool isnull)
Definition: jsonfuncs.c:5130
void * palloc0(Size size)
Definition: mcxt.c:864
json_aelem_action array_element_start
Definition: jsonapi.h:91
static void transform_string_values_object_start(void *state)
Definition: jsonfuncs.c:5082
json_struct_action array_start
Definition: jsonapi.h:87
Definition: regguts.h:298
static void transform_string_values_array_end(void *state)
Definition: jsonfuncs.c:5106
JsonTransformStringValuesAction action
Definition: jsonfuncs.c:70
static void transform_string_values_array_start(void *state)
Definition: jsonfuncs.c:5098
json_ofield_action object_field_start
Definition: jsonapi.h:89
void * semstate
Definition: jsonapi.h:84

◆ transform_jsonb_string_values()

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

Definition at line 5004 of file jsonfuncs.c.

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

5006 {
5007  JsonbIterator *it;
5008  JsonbValue v,
5009  *res = NULL;
5011  JsonbParseState *st = NULL;
5012  text *out;
5013  bool is_scalar = false;
5014 
5015  it = JsonbIteratorInit(&jsonb->root);
5016  is_scalar = it->isScalar;
5017 
5018  while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
5019  {
5020  if ((type == WJB_VALUE || type == WJB_ELEM) && v.type == jbvString)
5021  {
5022  out = transform_action(action_state, v.val.string.val, v.val.string.len);
5023  v.val.string.val = VARDATA_ANY(out);
5024  v.val.string.len = VARSIZE_ANY_EXHDR(out);
5025  res = pushJsonbValue(&st, type, type < WJB_BEGIN_ARRAY ? &v : NULL);
5026  }
5027  else
5028  {
5029  res = pushJsonbValue(&st, type, (type == WJB_KEY ||
5030  type == WJB_VALUE ||
5031  type == WJB_ELEM) ? &v : NULL);
5032  }
5033  }
5034 
5035  if (res->type == jbvArray)
5036  res->val.array.rawScalar = is_scalar;
5037 
5038  return JsonbValueToJsonb(res);
5039 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
Jsonb * JsonbValueToJsonb(JsonbValue *val)
Definition: jsonb_util.c:79
char * val
Definition: jsonb.h:259
Definition: jsonb.h:22
JsonbValue * pushJsonbValue(JsonbParseState **pstate, JsonbIteratorToken seq, JsonbValue *jbval)
Definition: jsonb_util.c:517
Definition: jsonb.h:23
JsonbIteratorToken
Definition: jsonb.h:20
JsonbContainer root
Definition: jsonb.h:218
bool isScalar
Definition: jsonb.h:330
JsonbIterator * JsonbIteratorInit(JsonbContainer *container)
Definition: jsonb_util.c:718
enum jbvType type
Definition: jsonb.h:250
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:497
JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
Definition: jsonb_util.c:754
Definition: jsonb.h:25