printer.h 2.0 KB

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