2
0

jsonapi.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*-------------------------------------------------------------------------
  2. *
  3. * jsonapi.h
  4. * Declarations for JSON API support.
  5. *
  6. * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  7. * Portions Copyright (c) 1994, Regents of the University of California
  8. *
  9. * src/include/common/jsonapi.h
  10. *
  11. *-------------------------------------------------------------------------
  12. */
  13. #ifndef JSONAPI_H
  14. #define JSONAPI_H
  15. #include "lib/stringinfo.h"
  16. typedef enum
  17. {
  18. JSON_TOKEN_INVALID,
  19. JSON_TOKEN_STRING,
  20. JSON_TOKEN_NUMBER,
  21. JSON_TOKEN_OBJECT_START,
  22. JSON_TOKEN_OBJECT_END,
  23. JSON_TOKEN_ARRAY_START,
  24. JSON_TOKEN_ARRAY_END,
  25. JSON_TOKEN_COMMA,
  26. JSON_TOKEN_COLON,
  27. JSON_TOKEN_TRUE,
  28. JSON_TOKEN_FALSE,
  29. JSON_TOKEN_NULL,
  30. JSON_TOKEN_END
  31. } JsonTokenType;
  32. typedef enum
  33. {
  34. JSON_SUCCESS,
  35. JSON_ESCAPING_INVALID,
  36. JSON_ESCAPING_REQUIRED,
  37. JSON_EXPECTED_ARRAY_FIRST,
  38. JSON_EXPECTED_ARRAY_NEXT,
  39. JSON_EXPECTED_COLON,
  40. JSON_EXPECTED_END,
  41. JSON_EXPECTED_JSON,
  42. JSON_EXPECTED_MORE,
  43. JSON_EXPECTED_OBJECT_FIRST,
  44. JSON_EXPECTED_OBJECT_NEXT,
  45. JSON_EXPECTED_STRING,
  46. JSON_INVALID_TOKEN,
  47. JSON_UNICODE_CODE_POINT_ZERO,
  48. JSON_UNICODE_ESCAPE_FORMAT,
  49. JSON_UNICODE_HIGH_ESCAPE,
  50. JSON_UNICODE_HIGH_SURROGATE,
  51. JSON_UNICODE_LOW_SURROGATE
  52. } JsonParseErrorType;
  53. /*
  54. * All the fields in this structure should be treated as read-only.
  55. *
  56. * If strval is not null, then it should contain the de-escaped value
  57. * of the lexeme if it's a string. Otherwise most of these field names
  58. * should be self-explanatory.
  59. *
  60. * line_number and line_start are principally for use by the parser's
  61. * error reporting routines.
  62. * token_terminator and prev_token_terminator point to the character
  63. * AFTER the end of the token, i.e. where there would be a nul byte
  64. * if we were using nul-terminated strings.
  65. */
  66. typedef struct JsonLexContext
  67. {
  68. char *input;
  69. int input_length;
  70. int input_encoding;
  71. char *token_start;
  72. char *token_terminator;
  73. char *prev_token_terminator;
  74. JsonTokenType token_type;
  75. int lex_level;
  76. int line_number; /* line number, starting from 1 */
  77. char *line_start; /* where that line starts within input */
  78. StringInfo strval;
  79. } JsonLexContext;
  80. typedef void (*json_struct_action) (void *state);
  81. typedef void (*json_ofield_action) (void *state, char *fname, bool isnull);
  82. typedef void (*json_aelem_action) (void *state, bool isnull);
  83. typedef void (*json_scalar_action) (void *state, char *token, JsonTokenType tokentype);
  84. /*
  85. * Semantic Action structure for use in parsing json.
  86. * Any of these actions can be NULL, in which case nothing is done at that
  87. * point, Likewise, semstate can be NULL. Using an all-NULL structure amounts
  88. * to doing a pure parse with no side-effects, and is therefore exactly
  89. * what the json input routines do.
  90. *
  91. * The 'fname' and 'token' strings passed to these actions are palloc'd.
  92. * They are not free'd or used further by the parser, so the action function
  93. * is free to do what it wishes with them.
  94. */
  95. typedef struct JsonSemAction
  96. {
  97. void *semstate;
  98. json_struct_action object_start;
  99. json_struct_action object_end;
  100. json_struct_action array_start;
  101. json_struct_action array_end;
  102. json_ofield_action object_field_start;
  103. json_ofield_action object_field_end;
  104. json_aelem_action array_element_start;
  105. json_aelem_action array_element_end;
  106. json_scalar_action scalar;
  107. } JsonSemAction;
  108. /*
  109. * pg_parse_json will parse the string in the lex calling the
  110. * action functions in sem at the appropriate points. It is
  111. * up to them to keep what state they need in semstate. If they
  112. * need access to the state of the lexer, then its pointer
  113. * should be passed to them as a member of whatever semstate
  114. * points to. If the action pointers are NULL the parser
  115. * does nothing and just continues.
  116. */
  117. extern JsonParseErrorType pg_parse_json(JsonLexContext *lex,
  118. JsonSemAction *sem);
  119. /* the null action object used for pure validation */
  120. extern PGDLLIMPORT JsonSemAction nullSemAction;
  121. /*
  122. * json_count_array_elements performs a fast secondary parse to determine the
  123. * number of elements in passed array lex context. It should be called from an
  124. * array_start action.
  125. *
  126. * The return value indicates whether any error occurred, while the number
  127. * of elements is stored into *elements (but only if the return value is
  128. * JSON_SUCCESS).
  129. */
  130. extern JsonParseErrorType json_count_array_elements(JsonLexContext *lex,
  131. int *elements);
  132. /*
  133. * constructor for JsonLexContext, with or without strval element.
  134. * If supplied, the strval element will contain a de-escaped version of
  135. * the lexeme. However, doing this imposes a performance penalty, so
  136. * it should be avoided if the de-escaped lexeme is not required.
  137. */
  138. extern JsonLexContext *makeJsonLexContextCstringLen(char *json,
  139. int len,
  140. int encoding,
  141. bool need_escapes);
  142. /* lex one token */
  143. extern JsonParseErrorType json_lex(JsonLexContext *lex);
  144. /* construct an error detail string for a json error */
  145. extern char *json_errdetail(JsonParseErrorType error, JsonLexContext *lex);
  146. /*
  147. * Utility function to check if a string is a valid JSON number.
  148. *
  149. * str argument does not need to be nul-terminated.
  150. */
  151. extern bool IsValidJsonNumber(const char *str, int len);
  152. #endif /* JSONAPI_H */