PostgreSQL Source Code  git master
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
}
 
enum  JsonParseErrorType {
  JSON_SUCCESS , JSON_ESCAPING_INVALID , JSON_ESCAPING_REQUIRED , JSON_EXPECTED_ARRAY_FIRST ,
  JSON_EXPECTED_ARRAY_NEXT , JSON_EXPECTED_COLON , JSON_EXPECTED_END , JSON_EXPECTED_JSON ,
  JSON_EXPECTED_MORE , JSON_EXPECTED_OBJECT_FIRST , JSON_EXPECTED_OBJECT_NEXT , JSON_EXPECTED_STRING ,
  JSON_INVALID_TOKEN , JSON_UNICODE_CODE_POINT_ZERO , JSON_UNICODE_ESCAPE_FORMAT , JSON_UNICODE_HIGH_ESCAPE ,
  JSON_UNICODE_HIGH_SURROGATE , JSON_UNICODE_LOW_SURROGATE
}
 

Functions

JsonParseErrorType pg_parse_json (JsonLexContext *lex, JsonSemAction *sem)
 
JsonParseErrorType json_count_array_elements (JsonLexContext *lex, int *elements)
 
JsonLexContextmakeJsonLexContextCstringLen (char *json, int len, int encoding, bool need_escapes)
 
JsonParseErrorType json_lex (JsonLexContext *lex)
 
char * json_errdetail (JsonParseErrorType error, JsonLexContext *lex)
 
bool IsValidJsonNumber (const char *str, int len)
 

Variables

PGDLLIMPORT JsonSemAction nullSemAction
 

Typedef Documentation

◆ json_aelem_action

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

Definition at line 89 of file jsonapi.h.

◆ json_ofield_action

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

Definition at line 88 of file jsonapi.h.

◆ json_scalar_action

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

Definition at line 90 of file jsonapi.h.

◆ json_struct_action

typedef void(* json_struct_action) (void *state)

Definition at line 87 of file jsonapi.h.

◆ JsonLexContext

◆ JsonSemAction

typedef struct JsonSemAction JsonSemAction

Enumeration Type Documentation

◆ JsonParseErrorType

Enumerator
JSON_SUCCESS 
JSON_ESCAPING_INVALID 
JSON_ESCAPING_REQUIRED 
JSON_EXPECTED_ARRAY_FIRST 
JSON_EXPECTED_ARRAY_NEXT 
JSON_EXPECTED_COLON 
JSON_EXPECTED_END 
JSON_EXPECTED_JSON 
JSON_EXPECTED_MORE 
JSON_EXPECTED_OBJECT_FIRST 
JSON_EXPECTED_OBJECT_NEXT 
JSON_EXPECTED_STRING 
JSON_INVALID_TOKEN 
JSON_UNICODE_CODE_POINT_ZERO 
JSON_UNICODE_ESCAPE_FORMAT 
JSON_UNICODE_HIGH_ESCAPE 
JSON_UNICODE_HIGH_SURROGATE 
JSON_UNICODE_LOW_SURROGATE 

Definition at line 36 of file jsonapi.h.

37 {
JsonParseErrorType
Definition: jsonapi.h:37
@ JSON_EXPECTED_ARRAY_FIRST
Definition: jsonapi.h:41
@ JSON_EXPECTED_MORE
Definition: jsonapi.h:46
@ JSON_UNICODE_HIGH_SURROGATE
Definition: jsonapi.h:54
@ JSON_EXPECTED_COLON
Definition: jsonapi.h:43
@ JSON_EXPECTED_OBJECT_FIRST
Definition: jsonapi.h:47
@ JSON_UNICODE_CODE_POINT_ZERO
Definition: jsonapi.h:51
@ JSON_EXPECTED_STRING
Definition: jsonapi.h:49
@ JSON_UNICODE_ESCAPE_FORMAT
Definition: jsonapi.h:52
@ JSON_SUCCESS
Definition: jsonapi.h:38
@ JSON_EXPECTED_OBJECT_NEXT
Definition: jsonapi.h:48
@ JSON_ESCAPING_REQUIRED
Definition: jsonapi.h:40
@ JSON_EXPECTED_JSON
Definition: jsonapi.h:45
@ JSON_INVALID_TOKEN
Definition: jsonapi.h:50
@ JSON_ESCAPING_INVALID
Definition: jsonapi.h:39
@ JSON_EXPECTED_END
Definition: jsonapi.h:44
@ JSON_EXPECTED_ARRAY_NEXT
Definition: jsonapi.h:42
@ JSON_UNICODE_HIGH_ESCAPE
Definition: jsonapi.h:53
@ JSON_UNICODE_LOW_SURROGATE
Definition: jsonapi.h:55

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

20 {
JsonTokenType
Definition: jsonapi.h:20
@ JSON_TOKEN_INVALID
Definition: jsonapi.h:21
@ JSON_TOKEN_COMMA
Definition: jsonapi.h:28
@ JSON_TOKEN_FALSE
Definition: jsonapi.h:31
@ JSON_TOKEN_END
Definition: jsonapi.h:33
@ JSON_TOKEN_TRUE
Definition: jsonapi.h:30
@ JSON_TOKEN_OBJECT_END
Definition: jsonapi.h:25
@ JSON_TOKEN_NULL
Definition: jsonapi.h:32
@ JSON_TOKEN_ARRAY_END
Definition: jsonapi.h:27
@ JSON_TOKEN_OBJECT_START
Definition: jsonapi.h:24
@ JSON_TOKEN_NUMBER
Definition: jsonapi.h:23
@ JSON_TOKEN_STRING
Definition: jsonapi.h:22
@ JSON_TOKEN_COLON
Definition: jsonapi.h:29
@ JSON_TOKEN_ARRAY_START
Definition: jsonapi.h:26

Function Documentation

◆ IsValidJsonNumber()

bool IsValidJsonNumber ( const char *  str,
int  len 
)

Definition at line 105 of file jsonapi.c.

106 {
107  bool numeric_error;
108  int total_len;
109  JsonLexContext dummy_lex;
110 
111  if (len <= 0)
112  return false;
113 
114  /*
115  * json_lex_number expects a leading '-' to have been eaten already.
116  *
117  * having to cast away the constness of str is ugly, but there's not much
118  * easy alternative.
119  */
120  if (*str == '-')
121  {
122  dummy_lex.input = unconstify(char *, str) + 1;
123  dummy_lex.input_length = len - 1;
124  }
125  else
126  {
127  dummy_lex.input = unconstify(char *, str);
128  dummy_lex.input_length = len;
129  }
130 
131  json_lex_number(&dummy_lex, dummy_lex.input, &numeric_error, &total_len);
132 
133  return (!numeric_error) && (total_len == dummy_lex.input_length);
134 }
#define unconstify(underlying_type, expr)
Definition: c.h:1181
static JsonParseErrorType json_lex_number(JsonLexContext *lex, char *s, bool *num_err, int *total_len)
Definition: jsonapi.c:928
const void size_t len
char * input
Definition: jsonapi.h:74
int input_length
Definition: jsonapi.h:75

References JsonLexContext::input, JsonLexContext::input_length, json_lex_number(), len, generate_unaccent_rules::str, and unconstify.

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

◆ json_count_array_elements()

JsonParseErrorType json_count_array_elements ( JsonLexContext lex,
int *  elements 
)

Definition at line 209 of file jsonapi.c.

210 {
211  JsonLexContext copylex;
212  int count;
213  JsonParseErrorType result;
214 
215  /*
216  * It's safe to do this with a shallow copy because the lexical routines
217  * don't scribble on the input. They do scribble on the other pointers
218  * etc, so doing this with a copy makes that safe.
219  */
220  memcpy(&copylex, lex, sizeof(JsonLexContext));
221  copylex.strval = NULL; /* not interested in values here */
222  copylex.lex_level++;
223 
224  count = 0;
225  result = lex_expect(JSON_PARSE_ARRAY_START, &copylex,
227  if (result != JSON_SUCCESS)
228  return result;
229  if (lex_peek(&copylex) != JSON_TOKEN_ARRAY_END)
230  {
231  while (1)
232  {
233  count++;
234  result = parse_array_element(&copylex, &nullSemAction);
235  if (result != JSON_SUCCESS)
236  return result;
237  if (copylex.token_type != JSON_TOKEN_COMMA)
238  break;
239  result = json_lex(&copylex);
240  if (result != JSON_SUCCESS)
241  return result;
242  }
243  }
244  result = lex_expect(JSON_PARSE_ARRAY_NEXT, &copylex,
246  if (result != JSON_SUCCESS)
247  return result;
248 
249  *elements = count;
250  return JSON_SUCCESS;
251 }
@ JSON_PARSE_ARRAY_START
Definition: jsonapi.c:37
@ JSON_PARSE_ARRAY_NEXT
Definition: jsonapi.c:38
static JsonParseErrorType parse_array_element(JsonLexContext *lex, JsonSemAction *sem)
Definition: jsonapi.c:426
static JsonTokenType lex_peek(JsonLexContext *lex)
Definition: jsonapi.c:71
JsonSemAction nullSemAction
Definition: jsonapi.c:57
static JsonParseErrorType lex_expect(JsonParseContext ctx, JsonLexContext *lex, JsonTokenType token)
Definition: jsonapi.c:83
JsonParseErrorType json_lex(JsonLexContext *lex)
Definition: jsonapi.c:520
StringInfo strval
Definition: jsonapi.h:84
int lex_level
Definition: jsonapi.h:81
JsonTokenType token_type
Definition: jsonapi.h:80

References json_lex(), JSON_PARSE_ARRAY_NEXT, JSON_PARSE_ARRAY_START, JSON_SUCCESS, JSON_TOKEN_ARRAY_END, JSON_TOKEN_ARRAY_START, JSON_TOKEN_COMMA, lex_expect(), JsonLexContext::lex_level, lex_peek(), nullSemAction, parse_array_element(), JsonLexContext::strval, and JsonLexContext::token_type.

Referenced by get_array_start().

◆ json_errdetail()

char* json_errdetail ( JsonParseErrorType  error,
JsonLexContext lex 
)

Definition at line 1089 of file jsonapi.c.

1090 {
1091  switch (error)
1092  {
1093  case JSON_SUCCESS:
1094  /* fall through to the error code after switch */
1095  break;
1096  case JSON_ESCAPING_INVALID:
1097  return psprintf(_("Escape sequence \"\\%s\" is invalid."),
1098  extract_token(lex));
1100  return psprintf(_("Character with value 0x%02x must be escaped."),
1101  (unsigned char) *(lex->token_terminator));
1102  case JSON_EXPECTED_END:
1103  return psprintf(_("Expected end of input, but found \"%s\"."),
1104  extract_token(lex));
1106  return psprintf(_("Expected array element or \"]\", but found \"%s\"."),
1107  extract_token(lex));
1109  return psprintf(_("Expected \",\" or \"]\", but found \"%s\"."),
1110  extract_token(lex));
1111  case JSON_EXPECTED_COLON:
1112  return psprintf(_("Expected \":\", but found \"%s\"."),
1113  extract_token(lex));
1114  case JSON_EXPECTED_JSON:
1115  return psprintf(_("Expected JSON value, but found \"%s\"."),
1116  extract_token(lex));
1117  case JSON_EXPECTED_MORE:
1118  return _("The input string ended unexpectedly.");
1120  return psprintf(_("Expected string or \"}\", but found \"%s\"."),
1121  extract_token(lex));
1123  return psprintf(_("Expected \",\" or \"}\", but found \"%s\"."),
1124  extract_token(lex));
1125  case JSON_EXPECTED_STRING:
1126  return psprintf(_("Expected string, but found \"%s\"."),
1127  extract_token(lex));
1128  case JSON_INVALID_TOKEN:
1129  return psprintf(_("Token \"%s\" is invalid."),
1130  extract_token(lex));
1132  return _("\\u0000 cannot be converted to text.");
1134  return _("\"\\u\" must be followed by four hexadecimal digits.");
1136  /* note: this case is only reachable in frontend not backend */
1137  return _("Unicode escape values cannot be used for code point values above 007F when the encoding is not UTF8.");
1139  return _("Unicode high surrogate must not follow a high surrogate.");
1141  return _("Unicode low surrogate must follow a high surrogate.");
1142  }
1143 
1144  /*
1145  * We don't use a default: case, so that the compiler will warn about
1146  * unhandled enum values. But this needs to be here anyway to cover the
1147  * possibility of an incorrect input.
1148  */
1149  elog(ERROR, "unexpected json parse error type: %d", (int) error);
1150  return NULL;
1151 }
#define _(x)
Definition: elog.c:90
#define ERROR
Definition: elog.h:35
static char * extract_token(JsonLexContext *lex)
Definition: jsonapi.c:1071
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static void error(void)
Definition: sql-dyntest.c:147
char * token_terminator
Definition: jsonapi.h:78

References _, elog(), ERROR, error(), extract_token(), JSON_ESCAPING_INVALID, JSON_ESCAPING_REQUIRED, JSON_EXPECTED_ARRAY_FIRST, JSON_EXPECTED_ARRAY_NEXT, JSON_EXPECTED_COLON, JSON_EXPECTED_END, JSON_EXPECTED_JSON, JSON_EXPECTED_MORE, JSON_EXPECTED_OBJECT_FIRST, JSON_EXPECTED_OBJECT_NEXT, JSON_EXPECTED_STRING, JSON_INVALID_TOKEN, JSON_SUCCESS, JSON_UNICODE_CODE_POINT_ZERO, JSON_UNICODE_ESCAPE_FORMAT, JSON_UNICODE_HIGH_ESCAPE, JSON_UNICODE_HIGH_SURROGATE, JSON_UNICODE_LOW_SURROGATE, psprintf(), and JsonLexContext::token_terminator.

Referenced by json_ereport_error().

◆ json_lex()

JsonParseErrorType json_lex ( JsonLexContext lex)

Definition at line 520 of file jsonapi.c.

521 {
522  char *s;
523  char *const end = lex->input + lex->input_length;
524  JsonParseErrorType result;
525 
526  /* Skip leading whitespace. */
527  s = lex->token_terminator;
528  while (s < end && (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r'))
529  {
530  if (*s++ == '\n')
531  {
532  ++lex->line_number;
533  lex->line_start = s;
534  }
535  }
536  lex->token_start = s;
537 
538  /* Determine token type. */
539  if (s >= end)
540  {
541  lex->token_start = NULL;
543  lex->token_terminator = s;
544  lex->token_type = JSON_TOKEN_END;
545  }
546  else
547  {
548  switch (*s)
549  {
550  /* Single-character token, some kind of punctuation mark. */
551  case '{':
553  lex->token_terminator = s + 1;
555  break;
556  case '}':
558  lex->token_terminator = s + 1;
560  break;
561  case '[':
563  lex->token_terminator = s + 1;
565  break;
566  case ']':
568  lex->token_terminator = s + 1;
570  break;
571  case ',':
573  lex->token_terminator = s + 1;
575  break;
576  case ':':
578  lex->token_terminator = s + 1;
580  break;
581  case '"':
582  /* string */
583  result = json_lex_string(lex);
584  if (result != JSON_SUCCESS)
585  return result;
587  break;
588  case '-':
589  /* Negative number. */
590  result = json_lex_number(lex, s + 1, NULL, NULL);
591  if (result != JSON_SUCCESS)
592  return result;
594  break;
595  case '0':
596  case '1':
597  case '2':
598  case '3':
599  case '4':
600  case '5':
601  case '6':
602  case '7':
603  case '8':
604  case '9':
605  /* Positive number. */
606  result = json_lex_number(lex, s, NULL, NULL);
607  if (result != JSON_SUCCESS)
608  return result;
610  break;
611  default:
612  {
613  char *p;
614 
615  /*
616  * We're not dealing with a string, number, legal
617  * punctuation mark, or end of string. The only legal
618  * tokens we might find here are true, false, and null,
619  * but for error reporting purposes we scan until we see a
620  * non-alphanumeric character. That way, we can report
621  * the whole word as an unexpected token, rather than just
622  * some unintuitive prefix thereof.
623  */
624  for (p = s; p < end && JSON_ALPHANUMERIC_CHAR(*p); p++)
625  /* skip */ ;
626 
627  /*
628  * We got some sort of unexpected punctuation or an
629  * otherwise unexpected character, so just complain about
630  * that one character.
631  */
632  if (p == s)
633  {
635  lex->token_terminator = s + 1;
636  return JSON_INVALID_TOKEN;
637  }
638 
639  /*
640  * We've got a real alphanumeric token here. If it
641  * happens to be true, false, or null, all is well. If
642  * not, error out.
643  */
645  lex->token_terminator = p;
646  if (p - s == 4)
647  {
648  if (memcmp(s, "true", 4) == 0)
650  else if (memcmp(s, "null", 4) == 0)
652  else
653  return JSON_INVALID_TOKEN;
654  }
655  else if (p - s == 5 && memcmp(s, "false", 5) == 0)
657  else
658  return JSON_INVALID_TOKEN;
659  }
660  } /* end of switch */
661  }
662 
663  return JSON_SUCCESS;
664 }
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
static JsonParseErrorType json_lex_string(JsonLexContext *lex)
Definition: jsonapi.c:670
#define JSON_ALPHANUMERIC_CHAR(c)
Definition: jsonapi.c:92
char * token_start
Definition: jsonapi.h:77
char * prev_token_terminator
Definition: jsonapi.h:79
char * line_start
Definition: jsonapi.h:83
int line_number
Definition: jsonapi.h:82

References if(), JsonLexContext::input, JsonLexContext::input_length, JSON_ALPHANUMERIC_CHAR, JSON_INVALID_TOKEN, json_lex_number(), json_lex_string(), JSON_SUCCESS, JSON_TOKEN_ARRAY_END, JSON_TOKEN_ARRAY_START, JSON_TOKEN_COLON, JSON_TOKEN_COMMA, JSON_TOKEN_END, JSON_TOKEN_FALSE, JSON_TOKEN_NULL, JSON_TOKEN_NUMBER, JSON_TOKEN_OBJECT_END, JSON_TOKEN_OBJECT_START, JSON_TOKEN_STRING, JSON_TOKEN_TRUE, JsonLexContext::line_number, JsonLexContext::line_start, JsonLexContext::prev_token_terminator, JsonLexContext::token_start, JsonLexContext::token_terminator, and JsonLexContext::token_type.

Referenced by json_count_array_elements(), json_typeof(), lex_expect(), parse_array(), parse_object(), parse_object_field(), parse_scalar(), and pg_parse_json().

◆ makeJsonLexContextCstringLen()

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

Definition at line 145 of file jsonapi.c.

146 {
147  JsonLexContext *lex = palloc0(sizeof(JsonLexContext));
148 
149  lex->input = lex->token_terminator = lex->line_start = json;
150  lex->line_number = 1;
151  lex->input_length = len;
152  lex->input_encoding = encoding;
153  if (need_escapes)
154  lex->strval = makeStringInfo();
155  return lex;
156 }
void * palloc0(Size size)
Definition: mcxt.c:1230
int32 encoding
Definition: pg_database.h:41
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
int input_encoding
Definition: jsonapi.h:76

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

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

◆ pg_parse_json()

JsonParseErrorType pg_parse_json ( JsonLexContext lex,
JsonSemAction sem 
)

Definition at line 169 of file jsonapi.c.

170 {
171  JsonTokenType tok;
172  JsonParseErrorType result;
173 
174  /* get the initial token */
175  result = json_lex(lex);
176  if (result != JSON_SUCCESS)
177  return result;
178 
179  tok = lex_peek(lex);
180 
181  /* parse by recursive descent */
182  switch (tok)
183  {
185  result = parse_object(lex, sem);
186  break;
188  result = parse_array(lex, sem);
189  break;
190  default:
191  result = parse_scalar(lex, sem); /* json can be a bare scalar */
192  }
193 
194  if (result == JSON_SUCCESS)
195  result = lex_expect(JSON_PARSE_END, lex, JSON_TOKEN_END);
196 
197  return result;
198 }
@ JSON_PARSE_END
Definition: jsonapi.c:43
static JsonParseErrorType parse_object(JsonLexContext *lex, JsonSemAction *sem)
Definition: jsonapi.c:360
static JsonParseErrorType parse_array(JsonLexContext *lex, JsonSemAction *sem)
Definition: jsonapi.c:463
static JsonParseErrorType parse_scalar(JsonLexContext *lex, JsonSemAction *sem)
Definition: jsonapi.c:263

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

Referenced by json_parse_manifest(), and pg_parse_json_or_ereport().

Variable Documentation

◆ nullSemAction

PGDLLIMPORT JsonSemAction nullSemAction
extern

Definition at line 57 of file jsonapi.c.

Referenced by json_count_array_elements(), json_in(), and json_recv().