1 /*
2 ** upb::json::Printer
3 **
4 ** Handlers that emit JSON according to a specific protobuf schema.
5 */
6 
7 #ifndef UPB_JSON_TYPED_PRINTER_H_
8 #define UPB_JSON_TYPED_PRINTER_H_
9 
10 #include "upb/sink.h"
11 
12 #ifdef __cplusplus
13 namespace upb {
14 namespace json {
15 class PrinterPtr;
16 }  /* namespace json */
17 }  /* namespace upb */
18 #endif
19 
20 /* upb_json_printer ***********************************************************/
21 
22 #define UPB_JSON_PRINTER_SIZE 192
23 
24 struct upb_json_printer;
25 typedef struct upb_json_printer upb_json_printer;
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 /* Native C API. */
32 upb_json_printer *upb_json_printer_create(upb_arena *a, const upb_handlers *h,
33                                           upb_bytessink output);
34 upb_sink upb_json_printer_input(upb_json_printer *p);
35 const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md,
36                                                  bool preserve_fieldnames,
37                                                  const void *owner);
38 
39 /* Lazily builds and caches handlers that will push encoded data to a bytessink.
40  * Any msgdef objects used with this object must outlive it. */
41 upb_handlercache *upb_json_printer_newcache(bool preserve_proto_fieldnames);
42 
43 #ifdef __cplusplus
44 }  /* extern "C" */
45 
46 /* Prints an incoming stream of data to a BytesSink in JSON format. */
47 class upb::json::PrinterPtr {
48  public:
PrinterPtr(upb_json_printer * ptr)49   PrinterPtr(upb_json_printer* ptr) : ptr_(ptr) {}
50 
Create(Arena * arena,const upb::Handlers * handlers,BytesSink output)51   static PrinterPtr Create(Arena *arena, const upb::Handlers *handlers,
52                            BytesSink output) {
53     return PrinterPtr(
54         upb_json_printer_create(arena->ptr(), handlers, output.sink()));
55   }
56 
57   /* The input to the printer. */
input()58   Sink input() { return upb_json_printer_input(ptr_); }
59 
60   static const size_t kSize = UPB_JSON_PRINTER_SIZE;
61 
NewCache(bool preserve_proto_fieldnames)62   static HandlerCache NewCache(bool preserve_proto_fieldnames) {
63     return upb_json_printer_newcache(preserve_proto_fieldnames);
64   }
65 
66  private:
67   upb_json_printer* ptr_;
68 };
69 
70 #endif  /* __cplusplus */
71 
72 #endif  /* UPB_JSON_TYPED_PRINTER_H_ */
73