jsonapi.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*-------------------------------------------------------------------------
  2. *
  3. * jsonapi.h
  4. * Declarations for JSON API support.
  5. *
  6. * Portions Copyright (c) 1996-2016, 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 "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. /*
  33. * All the fields in this structure should be treated as read-only.
  34. *
  35. * If strval is not null, then it should contain the de-escaped value
  36. * of the lexeme if it's a string. Otherwise most of these field names
  37. * should be self-explanatory.
  38. *
  39. * line_number and line_start are principally for use by the parser's
  40. * error reporting routines.
  41. * token_terminator and prev_token_terminator point to the character
  42. * AFTER the end of the token, i.e. where there would be a nul byte
  43. * if we were using nul-terminated strings.
  44. */
  45. typedef struct JsonLexContext
  46. {
  47. char *input;
  48. int input_length;
  49. char *token_start;
  50. char *token_terminator;
  51. char *prev_token_terminator;
  52. JsonTokenType token_type;
  53. int lex_level;
  54. int line_number;
  55. char *line_start;
  56. StringInfo strval;
  57. } JsonLexContext;
  58. typedef void (*json_struct_action) (void *state);
  59. typedef void (*json_ofield_action) (void *state, char *fname, bool isnull);
  60. typedef void (*json_aelem_action) (void *state, bool isnull);
  61. typedef void (*json_scalar_action) (void *state, char *token, JsonTokenType tokentype);
  62. /*
  63. * Semantic Action structure for use in parsing json.
  64. * Any of these actions can be NULL, in which case nothing is done at that
  65. * point, Likewise, semstate can be NULL. Using an all-NULL structure amounts
  66. * to doing a pure parse with no side-effects, and is therefore exactly
  67. * what the json input routines do.
  68. *
  69. * The 'fname' and 'token' strings passed to these actions are palloc'd.
  70. * They are not free'd or used further by the parser, so the action function
  71. * is free to do what it wishes with them.
  72. */
  73. typedef struct JsonSemAction
  74. {
  75. void *semstate;
  76. json_struct_action object_start;
  77. json_struct_action object_end;
  78. json_struct_action array_start;
  79. json_struct_action array_end;
  80. json_ofield_action object_field_start;
  81. json_ofield_action object_field_end;
  82. json_aelem_action array_element_start;
  83. json_aelem_action array_element_end;
  84. json_scalar_action scalar;
  85. } JsonSemAction;
  86. /*
  87. * parse_json will parse the string in the lex calling the
  88. * action functions in sem at the appropriate points. It is
  89. * up to them to keep what state they need in semstate. If they
  90. * need access to the state of the lexer, then its pointer
  91. * should be passed to them as a member of whatever semstate
  92. * points to. If the action pointers are NULL the parser
  93. * does nothing and just continues.
  94. */
  95. extern void pg_parse_json(JsonLexContext *lex, JsonSemAction *sem);
  96. /*
  97. * json_count_array_elements performs a fast secondary parse to determine the
  98. * number of elements in passed array lex context. It should be called from an
  99. * array_start action.
  100. */
  101. extern int json_count_array_elements(JsonLexContext *lex);
  102. /*
  103. * constructors for JsonLexContext, with or without strval element.
  104. * If supplied, the strval element will contain a de-escaped version of
  105. * the lexeme. However, doing this imposes a performance penalty, so
  106. * it should be avoided if the de-escaped lexeme is not required.
  107. *
  108. * If you already have the json as a text* value, use the first of these
  109. * functions, otherwise use makeJsonLexContextCstringLen().
  110. */
  111. extern JsonLexContext *makeJsonLexContext(text *json, bool need_escapes);
  112. extern JsonLexContext *makeJsonLexContextCstringLen(char *json,
  113. int len,
  114. bool need_escapes);
  115. /*
  116. * Utility function to check if a string is a valid JSON number.
  117. *
  118. * str agrument does not need to be nul-terminated.
  119. */
  120. extern bool IsValidJsonNumber(const char *str, int len);
  121. #endif /* JSONAPI_H */