summaryrefslogtreecommitdiff
path: root/upb/json/typed_printer.h
diff options
context:
space:
mode:
authorJosh Haberman <jhaberman@gmail.com>2014-06-26 20:24:32 -0700
committerJosh Haberman <jhaberman@gmail.com>2014-06-26 20:24:32 -0700
commit2d10fa33071d52d7a35ce3b13bc459cd16a0aa33 (patch)
treebf47d38e2e1cc8ddb4711b23b26e7fd10742e07d /upb/json/typed_printer.h
parent7d565f1e7a0f107506d3cf31ef2e33e22a504d2b (diff)
Sync from internal Google development.
Diffstat (limited to 'upb/json/typed_printer.h')
-rw-r--r--upb/json/typed_printer.h116
1 files changed, 116 insertions, 0 deletions
diff --git a/upb/json/typed_printer.h b/upb/json/typed_printer.h
new file mode 100644
index 0000000..190aeb3
--- /dev/null
+++ b/upb/json/typed_printer.h
@@ -0,0 +1,116 @@
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2014 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ *
+ * upb::json::TypedPrinter allows you to create handlers that emit JSON
+ * according to a specific protobuf schema.
+ */
+
+#ifndef UPB_JSON_TYPED_PRINTER_H_
+#define UPB_JSON_TYPED_PRINTER_H_
+
+#include "upb/sink.h"
+
+#ifdef __cplusplus
+namespace upb {
+namespace json {
+class TypedPrinter;
+} // namespace json
+} // namespace upb
+
+typedef upb::json::TypedPrinter upb_json_typedprinter;
+#else
+struct upb_json_typedprinter;
+typedef struct upb_json_typedprinter upb_json_typedprinter;
+#endif
+
+
+/* upb::json::TypedPrinter ****************************************************/
+
+#ifdef __cplusplus
+
+// Prints an incoming stream of data to a BytesSink in JSON format.
+class upb::json::TypedPrinter {
+ public:
+ TypedPrinter(const upb::Handlers* handlers);
+ ~TypedPrinter();
+
+ // Resets the state of the printer, so that it will expect to begin a new
+ // document.
+ void Reset();
+
+ // Resets the output pointer which will serve as our closure. Implies
+ // Reset().
+ void ResetOutput(BytesSink* output);
+
+ // The input to the printer.
+ Sink* input();
+
+ // Returns handlers for printing according to the specified schema.
+ static reffed_ptr<const Handlers> NewHandlers(const upb::MessageDef* md);
+
+ private:
+#else
+struct upb_json_typedprinter {
+#endif
+ upb_sink input_;
+ // Pointer to yajl_gen; void* here so we don't have to include YAJL headers.
+ void *yajl_gen_;
+ void *subc_;
+ upb_bytessink *output_;
+ // We track the depth so that we know when to emit startstr/endstr on the
+ // output.
+ int depth_;
+};
+
+// Native C API.
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void upb_json_typedprinter_init(upb_json_typedprinter *p,
+ const upb_handlers *h);
+void upb_json_typedprinter_uninit(upb_json_typedprinter *p);
+void upb_json_typedprinter_reset(upb_json_typedprinter *p);
+void upb_json_typedprinter_resetoutput(upb_json_typedprinter *p,
+ upb_bytessink *output);
+upb_sink *upb_json_typedprinter_input(upb_json_typedprinter *p);
+const upb_handlers *upb_json_typedprinter_newhandlers(const upb_msgdef *md,
+ const void *owner);
+
+#ifdef __cplusplus
+} // extern "C"
+#endif
+
+#ifdef __cplusplus
+
+namespace upb {
+namespace json {
+inline TypedPrinter::TypedPrinter(const upb::Handlers* handlers) {
+ upb_json_typedprinter_init(this, handlers);
+}
+inline TypedPrinter::~TypedPrinter() {
+ upb_json_typedprinter_uninit(this);
+}
+inline void TypedPrinter::Reset() {
+ upb_json_typedprinter_reset(this);
+}
+inline void TypedPrinter::ResetOutput(BytesSink* output) {
+ upb_json_typedprinter_resetoutput(this, output);
+}
+inline Sink* TypedPrinter::input() {
+ return upb_json_typedprinter_input(this);
+}
+inline reffed_ptr<const Handlers> TypedPrinter::NewHandlers(
+ const upb::MessageDef *md) {
+ const Handlers* h = upb_json_typedprinter_newhandlers(md, &h);
+ return reffed_ptr<const Handlers>(h, &h);
+}
+} // namespace json
+} // namespace upb
+
+#endif
+
+#endif // UPB_JSON_TYPED_PRINTER_H_
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback