gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

167 行
5.5KB

  1. /*-------------------------------------------------------------------------
  2. *
  3. * jsonapi.h
  4. * Declarations for JSON API support.
  5. *
  6. * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
  7. * Portions Copyright (c) 1994, Regents of the University of California
  8. *
  9. * src/include/utils/jsonapi.h
  10. *
  11. *-------------------------------------------------------------------------
  12. */
  13. #ifndef JSONAPI_H
  14. #define JSONAPI_H
  15. #include "jsonb.h"
  16. #include "lib/stringinfo.h"
  17. typedef enum
  18. {
  19. JSON_TOKEN_INVALID,
  20. JSON_TOKEN_STRING,
  21. JSON_TOKEN_NUMBER,
  22. JSON_TOKEN_OBJECT_START,
  23. JSON_TOKEN_OBJECT_END,
  24. JSON_TOKEN_ARRAY_START,
  25. JSON_TOKEN_ARRAY_END,
  26. JSON_TOKEN_COMMA,
  27. JSON_TOKEN_COLON,
  28. JSON_TOKEN_TRUE,
  29. JSON_TOKEN_FALSE,
  30. JSON_TOKEN_NULL,
  31. JSON_TOKEN_END
  32. } JsonTokenType;
  33. /*
  34. * All the fields in this structure should be treated as read-only.
  35. *
  36. * If strval is not null, then it should contain the de-escaped value
  37. * of the lexeme if it's a string. Otherwise most of these field names
  38. * should be self-explanatory.
  39. *
  40. * line_number and line_start are principally for use by the parser's
  41. * error reporting routines.
  42. * token_terminator and prev_token_terminator point to the character
  43. * AFTER the end of the token, i.e. where there would be a nul byte
  44. * if we were using nul-terminated strings.
  45. */
  46. typedef struct JsonLexContext
  47. {
  48. char *input;
  49. int input_length;
  50. char *token_start;
  51. char *token_terminator;
  52. char *prev_token_terminator;
  53. JsonTokenType token_type;
  54. int lex_level;
  55. int line_number;
  56. char *line_start;
  57. StringInfo strval;
  58. } JsonLexContext;
  59. typedef void (*json_struct_action) (void *state);
  60. typedef void (*json_ofield_action) (void *state, char *fname, bool isnull);
  61. typedef void (*json_aelem_action) (void *state, bool isnull);
  62. typedef void (*json_scalar_action) (void *state, char *token, JsonTokenType tokentype);
  63. /*
  64. * Semantic Action structure for use in parsing json.
  65. * Any of these actions can be NULL, in which case nothing is done at that
  66. * point, Likewise, semstate can be NULL. Using an all-NULL structure amounts
  67. * to doing a pure parse with no side-effects, and is therefore exactly
  68. * what the json input routines do.
  69. *
  70. * The 'fname' and 'token' strings passed to these actions are palloc'd.
  71. * They are not free'd or used further by the parser, so the action function
  72. * is free to do what it wishes with them.
  73. */
  74. typedef struct JsonSemAction
  75. {
  76. void *semstate;
  77. json_struct_action object_start;
  78. json_struct_action object_end;
  79. json_struct_action array_start;
  80. json_struct_action array_end;
  81. json_ofield_action object_field_start;
  82. json_ofield_action object_field_end;
  83. json_aelem_action array_element_start;
  84. json_aelem_action array_element_end;
  85. json_scalar_action scalar;
  86. } JsonSemAction;
  87. /*
  88. * parse_json will parse the string in the lex calling the
  89. * action functions in sem at the appropriate points. It is
  90. * up to them to keep what state they need in semstate. If they
  91. * need access to the state of the lexer, then its pointer
  92. * should be passed to them as a member of whatever semstate
  93. * points to. If the action pointers are NULL the parser
  94. * does nothing and just continues.
  95. */
  96. extern void pg_parse_json(JsonLexContext *lex, JsonSemAction *sem);
  97. /*
  98. * json_count_array_elements performs a fast secondary parse to determine the
  99. * number of elements in passed array lex context. It should be called from an
  100. * array_start action.
  101. */
  102. extern int json_count_array_elements(JsonLexContext *lex);
  103. /*
  104. * constructors for JsonLexContext, with or without strval element.
  105. * If supplied, the strval element will contain a de-escaped version of
  106. * the lexeme. However, doing this imposes a performance penalty, so
  107. * it should be avoided if the de-escaped lexeme is not required.
  108. *
  109. * If you already have the json as a text* value, use the first of these
  110. * functions, otherwise use makeJsonLexContextCstringLen().
  111. */
  112. extern JsonLexContext *makeJsonLexContext(text *json, bool need_escapes);
  113. extern JsonLexContext *makeJsonLexContextCstringLen(char *json,
  114. int len,
  115. bool need_escapes);
  116. /*
  117. * Utility function to check if a string is a valid JSON number.
  118. *
  119. * str argument does not need to be nul-terminated.
  120. */
  121. extern bool IsValidJsonNumber(const char *str, int len);
  122. /*
  123. * Flag types for iterate_json(b)_values to specify what elements from a
  124. * json(b) document we want to iterate.
  125. */
  126. typedef enum JsonToIndex
  127. {
  128. jtiKey = 0x01,
  129. jtiString = 0x02,
  130. jtiNumeric = 0x04,
  131. jtiBool = 0x08,
  132. jtiAll = jtiKey | jtiString | jtiNumeric | jtiBool
  133. } JsonToIndex;
  134. /* an action that will be applied to each value in iterate_json(b)_values functions */
  135. typedef void (*JsonIterateStringValuesAction) (void *state, char *elem_value, int elem_len);
  136. /* an action that will be applied to each value in transform_json(b)_values functions */
  137. typedef text *(*JsonTransformStringValuesAction) (void *state, char *elem_value, int elem_len);
  138. extern uint32 parse_jsonb_index_flags(Jsonb *jb);
  139. extern void iterate_jsonb_values(Jsonb *jb, uint32 flags, void *state,
  140. JsonIterateStringValuesAction action);
  141. extern void iterate_json_values(text *json, uint32 flags, void *action_state,
  142. JsonIterateStringValuesAction action);
  143. extern Jsonb *transform_jsonb_string_values(Jsonb *jsonb, void *action_state,
  144. JsonTransformStringValuesAction transform_action);
  145. extern text *transform_json_string_values(text *json, void *action_state,
  146. JsonTransformStringValuesAction transform_action);
  147. extern char *JsonEncodeDateTime(char *buf, Datum value, Oid typid);
  148. #endif /* JSONAPI_H */
上海开阖软件有限公司 沪ICP备12045867号-1