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

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.

◆ 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.

Function Documentation

◆ IsValidJsonNumber()

bool IsValidJsonNumber ( const char *  str,
int  len 
)

Definition at line 104 of file jsonapi.c.

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

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

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

◆ json_count_array_elements()

JsonParseErrorType json_count_array_elements ( JsonLexContext lex,
int *  elements 
)

Definition at line 208 of file jsonapi.c.

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(), parse_array_element(), JsonLexContext::strval, and JsonLexContext::token_type.

Referenced by get_array_start().

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

◆ json_errdetail()

char* json_errdetail ( JsonParseErrorType  error,
JsonLexContext lex 
)

Definition at line 1070 of file jsonapi.c.

References _, elog, 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().

1071 {
1072  switch (error)
1073  {
1074  case JSON_SUCCESS:
1075  /* fall through to the error code after switch */
1076  break;
1077  case JSON_ESCAPING_INVALID:
1078  return psprintf(_("Escape sequence \"\\%s\" is invalid."),
1079  extract_token(lex));
1081  return psprintf(_("Character with value 0x%02x must be escaped."),
1082  (unsigned char) *(lex->token_terminator));
1083  case JSON_EXPECTED_END:
1084  return psprintf(_("Expected end of input, but found \"%s\"."),
1085  extract_token(lex));
1087  return psprintf(_("Expected array element or \"]\", but found \"%s\"."),
1088  extract_token(lex));
1090  return psprintf(_("Expected \",\" or \"]\", but found \"%s\"."),
1091  extract_token(lex));
1092  case JSON_EXPECTED_COLON:
1093  return psprintf(_("Expected \":\", but found \"%s\"."),
1094  extract_token(lex));
1095  case JSON_EXPECTED_JSON:
1096  return psprintf(_("Expected JSON value, but found \"%s\"."),
1097  extract_token(lex));
1098  case JSON_EXPECTED_MORE:
1099  return _("The input string ended unexpectedly.");
1101  return psprintf(_("Expected string or \"}\", but found \"%s\"."),
1102  extract_token(lex));
1104  return psprintf(_("Expected \",\" or \"}\", but found \"%s\"."),
1105  extract_token(lex));
1106  case JSON_EXPECTED_STRING:
1107  return psprintf(_("Expected string, but found \"%s\"."),
1108  extract_token(lex));
1109  case JSON_INVALID_TOKEN:
1110  return psprintf(_("Token \"%s\" is invalid."),
1111  extract_token(lex));
1113  return _("\\u0000 cannot be converted to text.");
1115  return _("\"\\u\" must be followed by four hexadecimal digits.");
1117  /* note: this case is only reachable in frontend not backend */
1118  return _("Unicode escape values cannot be used for code point values above 007F when the encoding is not UTF8.");
1120  return _("Unicode high surrogate must not follow a high surrogate.");
1122  return _("Unicode low surrogate must follow a high surrogate.");
1123  }
1124 
1125  /*
1126  * We don't use a default: case, so that the compiler will warn about
1127  * unhandled enum values. But this needs to be here anyway to cover the
1128  * possibility of an incorrect input.
1129  */
1130  elog(ERROR, "unexpected json parse error type: %d", (int) error);
1131  return NULL;
1132 }
static void error(void)
Definition: sql-dyntest.c:147
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static char * extract_token(JsonLexContext *lex)
Definition: jsonapi.c:1052
#define ERROR
Definition: elog.h:46
char * token_terminator
Definition: jsonapi.h:78
#define elog(elevel,...)
Definition: elog.h:232
#define _(x)
Definition: elog.c:89

◆ json_lex()

JsonParseErrorType json_lex ( JsonLexContext lex)

Definition at line 519 of file jsonapi.c.

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

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

◆ makeJsonLexContextCstringLen()

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

Definition at line 144 of file jsonapi.c.

References encoding, JsonLexContext::input, JsonLexContext::input_encoding, 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_parse_manifest(), json_recv(), jsonb_from_cstring(), makeJsonLexContext(), and populate_array_json().

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

◆ pg_parse_json()

JsonParseErrorType pg_parse_json ( JsonLexContext lex,
JsonSemAction sem 
)

Definition at line 168 of file jsonapi.c.

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

169 {
170  JsonTokenType tok;
171  JsonParseErrorType result;
172 
173  /* get the initial token */
174  result = json_lex(lex);
175  if (result != JSON_SUCCESS)
176  return result;
177 
178  tok = lex_peek(lex);
179 
180  /* parse by recursive descent */
181  switch (tok)
182  {
184  result = parse_object(lex, sem);
185  break;
187  result = parse_array(lex, sem);
188  break;
189  default:
190  result = parse_scalar(lex, sem); /* json can be a bare scalar */
191  }
192 
193  if (result == JSON_SUCCESS)
194  result = lex_expect(JSON_PARSE_END, lex, JSON_TOKEN_END);
195 
196  return result;
197 }
static JsonParseErrorType parse_scalar(JsonLexContext *lex, JsonSemAction *sem)
Definition: jsonapi.c:262
JsonParseErrorType
Definition: jsonapi.h:36
JsonParseErrorType json_lex(JsonLexContext *lex)
Definition: jsonapi.c:519
static JsonParseErrorType parse_object(JsonLexContext *lex, JsonSemAction *sem)
Definition: jsonapi.c:359
static JsonTokenType lex_peek(JsonLexContext *lex)
Definition: jsonapi.c:70
static JsonParseErrorType lex_expect(JsonParseContext ctx, JsonLexContext *lex, JsonTokenType token)
Definition: jsonapi.c:82
static JsonParseErrorType parse_array(JsonLexContext *lex, JsonSemAction *sem)
Definition: jsonapi.c:462
JsonTokenType
Definition: jsonapi.h:19

Variable Documentation

◆ nullSemAction

JsonSemAction nullSemAction

Definition at line 56 of file jsonapi.c.

Referenced by json_in(), and json_recv().