parser.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. ** upb::json::Parser (upb_json_parser)
  3. **
  4. ** Parses JSON according to a specific schema.
  5. ** Support for parsing arbitrary JSON (schema-less) will be added later.
  6. */
  7. #ifndef UPB_JSON_PARSER_H_
  8. #define UPB_JSON_PARSER_H_
  9. #include "upb/sink.h"
  10. #ifdef __cplusplus
  11. namespace upb {
  12. namespace json {
  13. class CodeCache;
  14. class ParserPtr;
  15. class ParserMethodPtr;
  16. } /* namespace json */
  17. } /* namespace upb */
  18. #endif
  19. /* upb_json_parsermethod ******************************************************/
  20. struct upb_json_parsermethod;
  21. typedef struct upb_json_parsermethod upb_json_parsermethod;
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. const upb_byteshandler* upb_json_parsermethod_inputhandler(
  26. const upb_json_parsermethod* m);
  27. #ifdef __cplusplus
  28. } /* extern "C" */
  29. class upb::json::ParserMethodPtr {
  30. public:
  31. ParserMethodPtr() : ptr_(nullptr) {}
  32. ParserMethodPtr(const upb_json_parsermethod* ptr) : ptr_(ptr) {}
  33. const upb_json_parsermethod* ptr() const { return ptr_; }
  34. const BytesHandler* input_handler() const {
  35. return upb_json_parsermethod_inputhandler(ptr());
  36. }
  37. private:
  38. const upb_json_parsermethod* ptr_;
  39. };
  40. #endif /* __cplusplus */
  41. /* upb_json_parser ************************************************************/
  42. /* Preallocation hint: parser won't allocate more bytes than this when first
  43. * constructed. This hint may be an overestimate for some build configurations.
  44. * But if the parser library is upgraded without recompiling the application,
  45. * it may be an underestimate. */
  46. #define UPB_JSON_PARSER_SIZE 5712
  47. struct upb_json_parser;
  48. typedef struct upb_json_parser upb_json_parser;
  49. #ifdef __cplusplus
  50. extern "C" {
  51. #endif
  52. upb_json_parser* upb_json_parser_create(upb_arena* a,
  53. const upb_json_parsermethod* m,
  54. const upb_symtab* symtab,
  55. upb_sink output,
  56. upb_status *status,
  57. bool ignore_json_unknown);
  58. upb_bytessink upb_json_parser_input(upb_json_parser* p);
  59. #ifdef __cplusplus
  60. } /* extern "C" */
  61. /* Parses an incoming BytesStream, pushing the results to the destination
  62. * sink. */
  63. class upb::json::ParserPtr {
  64. public:
  65. ParserPtr(upb_json_parser* ptr) : ptr_(ptr) {}
  66. static ParserPtr Create(Arena* arena, ParserMethodPtr method,
  67. SymbolTable* symtab, Sink output, Status* status,
  68. bool ignore_json_unknown) {
  69. upb_symtab* symtab_ptr = symtab ? symtab->ptr() : nullptr;
  70. return ParserPtr(upb_json_parser_create(
  71. arena->ptr(), method.ptr(), symtab_ptr, output.sink(), status->ptr(),
  72. ignore_json_unknown));
  73. }
  74. BytesSink input() { return upb_json_parser_input(ptr_); }
  75. private:
  76. upb_json_parser* ptr_;
  77. };
  78. #endif /* __cplusplus */
  79. /* upb_json_codecache *********************************************************/
  80. /* Lazily builds and caches decoder methods that will push data to the given
  81. * handlers. The upb_symtab object(s) must outlive this object. */
  82. struct upb_json_codecache;
  83. typedef struct upb_json_codecache upb_json_codecache;
  84. #ifdef __cplusplus
  85. extern "C" {
  86. #endif
  87. upb_json_codecache *upb_json_codecache_new(void);
  88. void upb_json_codecache_free(upb_json_codecache *cache);
  89. const upb_json_parsermethod* upb_json_codecache_get(upb_json_codecache* cache,
  90. const upb_msgdef* md);
  91. #ifdef __cplusplus
  92. } /* extern "C" */
  93. class upb::json::CodeCache {
  94. public:
  95. CodeCache() : ptr_(upb_json_codecache_new(), upb_json_codecache_free) {}
  96. /* Returns a DecoderMethod that can push data to the given handlers.
  97. * If a suitable method already exists, it will be returned from the cache. */
  98. ParserMethodPtr Get(MessageDefPtr md) {
  99. return upb_json_codecache_get(ptr_.get(), md.ptr());
  100. }
  101. private:
  102. std::unique_ptr<upb_json_codecache, decltype(&upb_json_codecache_free)> ptr_;
  103. };
  104. #endif
  105. #endif /* UPB_JSON_PARSER_H_ */