summaryrefslogtreecommitdiff
path: root/upb/json/parser.h
blob: 2a06fcf019a4266ea27cd4ea4c8dc2ecb0da07b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
** upb::json::Parser (upb_json_parser)
**
** Parses JSON according to a specific schema.
** Support for parsing arbitrary JSON (schema-less) will be added later.
*/

#ifndef UPB_JSON_PARSER_H_
#define UPB_JSON_PARSER_H_

#include "upb/sink.h"

#ifdef __cplusplus
namespace upb {
namespace json {
class CodeCache;
class ParserPtr;
class ParserMethodPtr;
}  /* namespace json */
}  /* namespace upb */
#endif

/* upb_json_parsermethod ******************************************************/

struct upb_json_parsermethod;
typedef struct upb_json_parsermethod upb_json_parsermethod;

#ifdef __cplusplus
extern "C" {
#endif

const upb_byteshandler* upb_json_parsermethod_inputhandler(
    const upb_json_parsermethod* m);

#ifdef __cplusplus
}  /* extern "C" */

class upb::json::ParserMethodPtr {
 public:
  ParserMethodPtr() : ptr_(nullptr) {}
  ParserMethodPtr(const upb_json_parsermethod* ptr) : ptr_(ptr) {}

  const upb_json_parsermethod* ptr() const { return ptr_; }

  const BytesHandler* input_handler() const {
    return upb_json_parsermethod_inputhandler(ptr());
  }

 private:
  const upb_json_parsermethod* ptr_;
};

#endif  /* __cplusplus */

/* upb_json_parser ************************************************************/

/* Preallocation hint: parser won't allocate more bytes than this when first
 * constructed.  This hint may be an overestimate for some build configurations.
 * But if the parser library is upgraded without recompiling the application,
 * it may be an underestimate. */
#define UPB_JSON_PARSER_SIZE 5712

struct upb_json_parser;
typedef struct upb_json_parser upb_json_parser;

#ifdef __cplusplus
extern "C" {
#endif

upb_json_parser* upb_json_parser_create(upb_arena* a,
                                        const upb_json_parsermethod* m,
                                        const upb_symtab* symtab,
                                        upb_sink output,
                                        bool ignore_json_unknown);
upb_bytessink upb_json_parser_input(upb_json_parser* p);

#ifdef __cplusplus
}  /* extern "C" */

/* Parses an incoming BytesStream, pushing the results to the destination
 * sink. */
class upb::json::ParserPtr {
 public:
  ParserPtr(upb_json_parser* ptr) : ptr_(ptr) {}

  static ParserPtr Create(Arena* arena, ParserMethodPtr method,
                          SymbolTable* symtab, Sink output,
                          bool ignore_json_unknown) {
    upb_symtab* symtab_ptr = symtab ? symtab->ptr() : nullptr;
    return ParserPtr(upb_json_parser_create(arena->ptr(), method.ptr(),
                                            symtab_ptr, output.sink(),
                                            ignore_json_unknown));
  }

  BytesSink input() { return upb_json_parser_input(ptr_); }

 private:
  upb_json_parser* ptr_;
};

#endif  /* __cplusplus */

/* upb_json_codecache *********************************************************/

struct upb_json_codecache;
typedef struct upb_json_codecache upb_json_codecache;

#ifdef __cplusplus
extern "C" {
#endif

upb_json_codecache *upb_json_codecache_new();
void upb_json_codecache_free(upb_json_codecache *cache);
const upb_json_parsermethod* upb_json_codecache_get(upb_json_codecache* cache,
                                                    const upb_msgdef* md);

#ifdef __cplusplus
}  /* extern "C" */

class upb::json::CodeCache {
 public:
  CodeCache() : ptr_(upb_json_codecache_new(), upb_json_codecache_free) {}

  /* Returns a DecoderMethod that can push data to the given handlers.
   * If a suitable method already exists, it will be returned from the cache. */
  ParserMethodPtr Get(MessageDefPtr md) {
    return upb_json_codecache_get(ptr_.get(), md.ptr());
  }

 private:
  std::unique_ptr<upb_json_codecache, decltype(&upb_json_codecache_free)> ptr_;
};

#endif

#endif  /* UPB_JSON_PARSER_H_ */
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback