PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
jsonapi.h File Reference
#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
 

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)
 

Typedef Documentation

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

Definition at line 66 of file jsonapi.h.

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

Definition at line 65 of file jsonapi.h.

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

Definition at line 67 of file jsonapi.h.

typedef void(* json_struct_action)(void *state)

Definition at line 64 of file jsonapi.h.

Enumeration Type Documentation

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 19 of file jsonapi.h.

Function Documentation

bool IsValidJsonNumber ( const char *  str,
int  len 
)

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

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

Definition at line 366 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(), NULL, parse_array_element(), and JsonLexContext::strval.

Referenced by get_array_start().

367 {
368  JsonLexContext copylex;
369  int count;
370 
371  /*
372  * It's safe to do this with a shallow copy because the lexical routines
373  * don't scribble on the input. They do scribble on the other pointers
374  * etc, so doing this with a copy makes that safe.
375  */
376  memcpy(&copylex, lex, sizeof(JsonLexContext));
377  copylex.strval = NULL; /* not interested in values here */
378  copylex.lex_level++;
379 
380  count = 0;
382  if (lex_peek(&copylex) != JSON_TOKEN_ARRAY_END)
383  {
384  do
385  {
386  count++;
388  }
389  while (lex_accept(&copylex, JSON_TOKEN_COMMA, NULL));
390  }
392 
393  return count;
394 }
static void lex_expect(JsonParseContext ctx, JsonLexContext *lex, JsonTokenType token)
Definition: json.c:172
static bool lex_accept(JsonLexContext *lex, JsonTokenType token, char **lexeme)
Definition: json.c:138
int lex_level
Definition: jsonapi.h:58
static void parse_array_element(JsonLexContext *lex, JsonSemAction *sem)
Definition: json.c:538
StringInfo strval
Definition: jsonapi.h:61
static JsonTokenType lex_peek(JsonLexContext *lex)
Definition: json.c:123
#define NULL
Definition: c.h:226
static JsonSemAction nullSemAction
Definition: json.c:109
JsonLexContext* makeJsonLexContext ( text json,
bool  need_escapes 
)

Definition at line 300 of file json.c.

References makeJsonLexContextCstringLen(), VARDATA, VARHDRSZ, and VARSIZE.

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

301 {
303  VARSIZE(json) - VARHDRSZ,
304  need_escapes);
305 }
#define VARDATA(PTR)
Definition: postgres.h:305
#define VARSIZE(PTR)
Definition: postgres.h:306
#define VARHDRSZ
Definition: c.h:441
JsonLexContext * makeJsonLexContextCstringLen(char *json, int len, bool need_escapes)
Definition: json.c:308
JsonLexContext* makeJsonLexContextCstringLen ( char *  json,
int  len,
bool  need_escapes 
)

Definition at line 308 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 json_recv(), jsonb_from_cstring(), and makeJsonLexContext().

309 {
310  JsonLexContext *lex = palloc0(sizeof(JsonLexContext));
311 
312  lex->input = lex->token_terminator = lex->line_start = json;
313  lex->line_number = 1;
314  lex->input_length = len;
315  if (need_escapes)
316  lex->strval = makeStringInfo();
317  return lex;
318 }
int line_number
Definition: jsonapi.h:59
StringInfo makeStringInfo(void)
Definition: stringinfo.c:29
char * line_start
Definition: jsonapi.h:60
int input_length
Definition: jsonapi.h:53
StringInfo strval
Definition: jsonapi.h:61
void * palloc0(Size size)
Definition: mcxt.c:920
char * token_terminator
Definition: jsonapi.h:55
char * input
Definition: jsonapi.h:52
void pg_parse_json ( JsonLexContext lex,
JsonSemAction sem 
)

Definition at line 331 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(), json_array_length(), json_in(), json_object_keys(), json_recv(), json_strip_nulls(), jsonb_from_cstring(), and populate_recordset_worker().

332 {
333  JsonTokenType tok;
334 
335  /* get the initial token */
336  json_lex(lex);
337 
338  tok = lex_peek(lex);
339 
340  /* parse by recursive descent */
341  switch (tok)
342  {
344  parse_object(lex, sem);
345  break;
347  parse_array(lex, sem);
348  break;
349  default:
350  parse_scalar(lex, sem); /* json can be a bare scalar */
351  }
352 
354 
355 }
static void lex_expect(JsonParseContext ctx, JsonLexContext *lex, JsonTokenType token)
Definition: json.c:172
static void parse_array(JsonLexContext *lex, JsonSemAction *sem)
Definition: json.c:569
static JsonTokenType lex_peek(JsonLexContext *lex)
Definition: json.c:123
static void json_lex(JsonLexContext *lex)
Definition: json.c:613
static void parse_scalar(JsonLexContext *lex, JsonSemAction *sem)
Definition: json.c:406
static void parse_object(JsonLexContext *lex, JsonSemAction *sem)
Definition: json.c:488
JsonTokenType
Definition: jsonapi.h:19