2 ** This currently uses snprintf() to format primitives, and could be optimized
6 #include "upb/json/printer.h"
13 #include "upb/port_def.inc"
15 struct upb_json_printer {
17 /* BytesSink closure. */
19 upb_bytessink output_;
21 /* We track the depth so that we know when to emit startstr/endstr on the
25 /* Have we emitted the first element? This state is necessary to emit commas
26 * without leaving a trailing comma in arrays/maps. We keep this state per
29 * Why max_depth * 2? UPB_MAX_HANDLER_DEPTH counts depth as nested messages.
30 * We count frames (contexts in which we separate elements by commas) as both
31 * repeated fields and messages (maps), and the worst case is a
32 * message->repeated field->submessage->repeated field->... nesting. */
33 bool first_elem_[UPB_MAX_HANDLER_DEPTH * 2];
35 /* To print timestamp, printer needs to cache its seconds and nanos values
36 * and convert them when ending timestamp message. See comments of
37 * printer_sethandlers_timestamp for more detail. */
42 /* StringPiece; a pointer plus a length. */
48 void freestrpc(void *ptr) {
55 bool preserve_fieldnames;
56 } upb_json_printercache;
58 /* Convert fielddef name to JSON name and return as a string piece. */
59 strpc *newstrpc(upb_handlers *h, const upb_fielddef *f,
60 bool preserve_fieldnames) {
61 /* TODO(haberman): handle malloc failure. */
62 strpc *ret = upb_gmalloc(sizeof(*ret));
63 if (preserve_fieldnames) {
64 ret->ptr = upb_gstrdup(upb_fielddef_name(f));
65 ret->len = strlen(ret->ptr);
68 ret->len = upb_fielddef_getjsonname(f, NULL, 0);
69 ret->ptr = upb_gmalloc(ret->len);
70 len = upb_fielddef_getjsonname(f, ret->ptr, ret->len);
71 UPB_ASSERT(len == ret->len);
72 ret->len--; /* NULL */
75 upb_handlers_addcleanup(h, ret, freestrpc);
79 /* Convert a null-terminated const char* to a string piece. */
80 strpc *newstrpc_str(upb_handlers *h, const char * str) {
81 strpc * ret = upb_gmalloc(sizeof(*ret));
82 ret->ptr = upb_gstrdup(str);
83 ret->len = strlen(str);
84 upb_handlers_addcleanup(h, ret, freestrpc);
88 /* ------------ JSON string printing: values, maps, arrays ------------------ */
90 static void print_data(
91 upb_json_printer *p, const char *buf, unsigned int len) {
92 /* TODO: Will need to change if we support pushback from the sink. */
93 size_t n = upb_bytessink_putbuf(p->output_, p->subc_, buf, len, NULL);
97 static void print_comma(upb_json_printer *p) {
98 if (!p->first_elem_[p->depth_]) {
99 print_data(p, ",", 1);
101 p->first_elem_[p->depth_] = false;
104 /* Helpers that print properly formatted elements to the JSON output stream. */
106 /* Used for escaping control chars in strings. */
107 static const char kControlCharLimit = 0x20;
109 UPB_INLINE bool is_json_escaped(char c) {
111 unsigned char uc = (unsigned char)c;
112 return uc < kControlCharLimit || uc == '"' || uc == '\\';
115 UPB_INLINE const char* json_nice_escape(char c) {
117 case '"': return "\\\"";
118 case '\\': return "\\\\";
119 case '\b': return "\\b";
120 case '\f': return "\\f";
121 case '\n': return "\\n";
122 case '\r': return "\\r";
123 case '\t': return "\\t";
124 default: return NULL;
128 /* Write a properly escaped string chunk. The surrounding quotes are *not*
129 * printed; this is so that the caller has the option of emitting the string
130 * content in chunks. */
131 static void putstring(upb_json_printer *p, const char *buf, unsigned int len) {
132 const char* unescaped_run = NULL;
134 for (i = 0; i < len; i++) {
136 /* Handle escaping. */
137 if (is_json_escaped(c)) {
138 /* Use a "nice" escape, like \n, if one exists for this character. */
139 const char* escape = json_nice_escape(c);
140 /* If we don't have a specific 'nice' escape code, use a \uXXXX-style
144 unsigned char byte = (unsigned char)c;
145 _upb_snprintf(escape_buf, sizeof(escape_buf), "\\u%04x", (int)byte);
149 /* N.B. that we assume that the input encoding is equal to the output
150 * encoding (both UTF-8 for now), so for chars >= 0x20 and != \, ", we
151 * can simply pass the bytes through. */
153 /* If there's a current run of unescaped chars, print that run first. */
155 print_data(p, unescaped_run, &buf[i] - unescaped_run);
156 unescaped_run = NULL;
158 /* Then print the escape code. */
159 print_data(p, escape, strlen(escape));
161 /* Add to the current unescaped run of characters. */
162 if (unescaped_run == NULL) {
163 unescaped_run = &buf[i];
168 /* If the string ended in a run of unescaped characters, print that last run. */
170 print_data(p, unescaped_run, &buf[len] - unescaped_run);
174 #define CHKLENGTH(x) if (!(x)) return -1;
176 /* Helpers that format floating point values according to our custom formats.
177 * Right now we use %.8g and %.17g for float/double, respectively, to match
178 * proto2::util::JsonFormat's defaults. May want to change this later. */
180 const char neginf[] = "\"-Infinity\"";
181 const char inf[] = "\"Infinity\"";
183 static size_t fmt_double(double val, char* buf, size_t length) {
184 if (val == UPB_INFINITY) {
185 CHKLENGTH(length >= strlen(inf));
188 } else if (val == -UPB_INFINITY) {
189 CHKLENGTH(length >= strlen(neginf));
191 return strlen(neginf);
193 size_t n = _upb_snprintf(buf, length, "%.17g", val);
194 CHKLENGTH(n > 0 && n < length);
199 static size_t fmt_float(float val, char* buf, size_t length) {
200 size_t n = _upb_snprintf(buf, length, "%.8g", val);
201 CHKLENGTH(n > 0 && n < length);
205 static size_t fmt_bool(bool val, char* buf, size_t length) {
206 size_t n = _upb_snprintf(buf, length, "%s", (val ? "true" : "false"));
207 CHKLENGTH(n > 0 && n < length);
211 static size_t fmt_int64_as_number(long long val, char* buf, size_t length) {
212 size_t n = _upb_snprintf(buf, length, "%lld", val);
213 CHKLENGTH(n > 0 && n < length);
217 static size_t fmt_uint64_as_number(
218 unsigned long long val, char* buf, size_t length) {
219 size_t n = _upb_snprintf(buf, length, "%llu", val);
220 CHKLENGTH(n > 0 && n < length);
224 static size_t fmt_int64_as_string(long long val, char* buf, size_t length) {
225 size_t n = _upb_snprintf(buf, length, "\"%lld\"", val);
226 CHKLENGTH(n > 0 && n < length);
230 static size_t fmt_uint64_as_string(
231 unsigned long long val, char* buf, size_t length) {
232 size_t n = _upb_snprintf(buf, length, "\"%llu\"", val);
233 CHKLENGTH(n > 0 && n < length);
237 /* Print a map key given a field name. Called by scalar field handlers and by
238 * startseq for repeated fields. */
239 static bool putkey(void *closure, const void *handler_data) {
240 upb_json_printer *p = closure;
241 const strpc *key = handler_data;
243 print_data(p, "\"", 1);
244 putstring(p, key->ptr, key->len);
245 print_data(p, "\":", 2);
249 #define CHKFMT(val) if ((val) == (size_t)-1) return false;
250 #define CHK(val) if (!(val)) return false;
252 #define TYPE_HANDLERS(type, fmt_func) \
253 static bool put##type(void *closure, const void *handler_data, type val) { \
254 upb_json_printer *p = closure; \
256 size_t length = fmt_func(val, data, sizeof(data)); \
257 UPB_UNUSED(handler_data); \
259 print_data(p, data, length); \
262 static bool scalar_##type(void *closure, const void *handler_data, \
264 CHK(putkey(closure, handler_data)); \
265 CHK(put##type(closure, handler_data, val)); \
268 static bool repeated_##type(void *closure, const void *handler_data, \
270 upb_json_printer *p = closure; \
272 CHK(put##type(closure, handler_data, val)); \
276 #define TYPE_HANDLERS_MAPKEY(type, fmt_func) \
277 static bool putmapkey_##type(void *closure, const void *handler_data, \
279 upb_json_printer *p = closure; \
281 size_t length = fmt_func(val, data, sizeof(data)); \
282 UPB_UNUSED(handler_data); \
283 print_data(p, "\"", 1); \
284 print_data(p, data, length); \
285 print_data(p, "\":", 2); \
289 TYPE_HANDLERS(double, fmt_double)
290 TYPE_HANDLERS(float, fmt_float)
291 TYPE_HANDLERS(bool, fmt_bool)
292 TYPE_HANDLERS(int32_t, fmt_int64_as_number)
293 TYPE_HANDLERS(uint32_t, fmt_int64_as_number)
294 TYPE_HANDLERS(int64_t, fmt_int64_as_string)
295 TYPE_HANDLERS(uint64_t, fmt_uint64_as_string)
297 /* double and float are not allowed to be map keys. */
298 TYPE_HANDLERS_MAPKEY(bool, fmt_bool)
299 TYPE_HANDLERS_MAPKEY(int32_t, fmt_int64_as_number)
300 TYPE_HANDLERS_MAPKEY(uint32_t, fmt_int64_as_number)
301 TYPE_HANDLERS_MAPKEY(int64_t, fmt_int64_as_number)
302 TYPE_HANDLERS_MAPKEY(uint64_t, fmt_uint64_as_number)
305 #undef TYPE_HANDLERS_MAPKEY
309 const upb_enumdef *enumdef;
312 static bool scalar_enum(void *closure, const void *handler_data,
314 const EnumHandlerData *hd = handler_data;
315 upb_json_printer *p = closure;
316 const char *symbolic_name;
318 CHK(putkey(closure, hd->keyname));
320 symbolic_name = upb_enumdef_iton(hd->enumdef, val);
322 print_data(p, "\"", 1);
323 putstring(p, symbolic_name, strlen(symbolic_name));
324 print_data(p, "\"", 1);
326 putint32_t(closure, NULL, val);
332 static void print_enum_symbolic_name(upb_json_printer *p,
333 const upb_enumdef *def,
335 const char *symbolic_name = upb_enumdef_iton(def, val);
337 print_data(p, "\"", 1);
338 putstring(p, symbolic_name, strlen(symbolic_name));
339 print_data(p, "\"", 1);
341 putint32_t(p, NULL, val);
345 static bool repeated_enum(void *closure, const void *handler_data,
347 const EnumHandlerData *hd = handler_data;
348 upb_json_printer *p = closure;
351 print_enum_symbolic_name(p, hd->enumdef, val);
356 static bool mapvalue_enum(void *closure, const void *handler_data,
358 const EnumHandlerData *hd = handler_data;
359 upb_json_printer *p = closure;
361 print_enum_symbolic_name(p, hd->enumdef, val);
366 static void *scalar_startsubmsg(void *closure, const void *handler_data) {
367 return putkey(closure, handler_data) ? closure : UPB_BREAK;
370 static void *repeated_startsubmsg(void *closure, const void *handler_data) {
371 upb_json_printer *p = closure;
372 UPB_UNUSED(handler_data);
377 static void start_frame(upb_json_printer *p) {
379 p->first_elem_[p->depth_] = true;
380 print_data(p, "{", 1);
383 static void end_frame(upb_json_printer *p) {
384 print_data(p, "}", 1);
388 static bool printer_startmsg(void *closure, const void *handler_data) {
389 upb_json_printer *p = closure;
390 UPB_UNUSED(handler_data);
391 if (p->depth_ == 0) {
392 upb_bytessink_start(p->output_, 0, &p->subc_);
398 static bool printer_endmsg(void *closure, const void *handler_data, upb_status *s) {
399 upb_json_printer *p = closure;
400 UPB_UNUSED(handler_data);
403 if (p->depth_ == 0) {
404 upb_bytessink_end(p->output_);
409 static void *startseq(void *closure, const void *handler_data) {
410 upb_json_printer *p = closure;
411 CHK(putkey(closure, handler_data));
413 p->first_elem_[p->depth_] = true;
414 print_data(p, "[", 1);
418 static bool endseq(void *closure, const void *handler_data) {
419 upb_json_printer *p = closure;
420 UPB_UNUSED(handler_data);
421 print_data(p, "]", 1);
426 static void *startmap(void *closure, const void *handler_data) {
427 upb_json_printer *p = closure;
428 CHK(putkey(closure, handler_data));
430 p->first_elem_[p->depth_] = true;
431 print_data(p, "{", 1);
435 static bool endmap(void *closure, const void *handler_data) {
436 upb_json_printer *p = closure;
437 UPB_UNUSED(handler_data);
438 print_data(p, "}", 1);
443 static size_t putstr(void *closure, const void *handler_data, const char *str,
444 size_t len, const upb_bufhandle *handle) {
445 upb_json_printer *p = closure;
446 UPB_UNUSED(handler_data);
448 putstring(p, str, len);
452 /* This has to Base64 encode the bytes, because JSON has no "bytes" type. */
453 static size_t putbytes(void *closure, const void *handler_data, const char *str,
454 size_t len, const upb_bufhandle *handle) {
455 upb_json_printer *p = closure;
457 /* This is the regular base64, not the "web-safe" version. */
458 static const char base64[] =
459 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
463 const char *limit = data + sizeof(data);
464 const unsigned char *from = (const unsigned char*)str;
466 size_t remaining = len;
469 UPB_UNUSED(handler_data);
472 print_data(p, "\"", 1);
474 while (remaining > 2) {
475 if (limit - to < 4) {
477 putstring(p, data, bytes);
481 to[0] = base64[from[0] >> 2];
482 to[1] = base64[((from[0] & 0x3) << 4) | (from[1] >> 4)];
483 to[2] = base64[((from[1] & 0xf) << 2) | (from[2] >> 6)];
484 to[3] = base64[from[2] & 0x3f];
493 to[0] = base64[from[0] >> 2];
494 to[1] = base64[((from[0] & 0x3) << 4) | (from[1] >> 4)];
495 to[2] = base64[(from[1] & 0xf) << 2];
501 to[0] = base64[from[0] >> 2];
502 to[1] = base64[((from[0] & 0x3) << 4)];
511 putstring(p, data, bytes);
512 print_data(p, "\"", 1);
516 static void *scalar_startstr(void *closure, const void *handler_data,
518 upb_json_printer *p = closure;
519 UPB_UNUSED(handler_data);
520 UPB_UNUSED(size_hint);
521 CHK(putkey(closure, handler_data));
522 print_data(p, "\"", 1);
526 static size_t scalar_str(void *closure, const void *handler_data,
527 const char *str, size_t len,
528 const upb_bufhandle *handle) {
529 CHK(putstr(closure, handler_data, str, len, handle));
533 static bool scalar_endstr(void *closure, const void *handler_data) {
534 upb_json_printer *p = closure;
535 UPB_UNUSED(handler_data);
536 print_data(p, "\"", 1);
540 static void *repeated_startstr(void *closure, const void *handler_data,
542 upb_json_printer *p = closure;
543 UPB_UNUSED(handler_data);
544 UPB_UNUSED(size_hint);
546 print_data(p, "\"", 1);
550 static size_t repeated_str(void *closure, const void *handler_data,
551 const char *str, size_t len,
552 const upb_bufhandle *handle) {
553 CHK(putstr(closure, handler_data, str, len, handle));
557 static bool repeated_endstr(void *closure, const void *handler_data) {
558 upb_json_printer *p = closure;
559 UPB_UNUSED(handler_data);
560 print_data(p, "\"", 1);
564 static void *mapkeyval_startstr(void *closure, const void *handler_data,
566 upb_json_printer *p = closure;
567 UPB_UNUSED(handler_data);
568 UPB_UNUSED(size_hint);
569 print_data(p, "\"", 1);
573 static size_t mapkey_str(void *closure, const void *handler_data,
574 const char *str, size_t len,
575 const upb_bufhandle *handle) {
576 CHK(putstr(closure, handler_data, str, len, handle));
580 static bool mapkey_endstr(void *closure, const void *handler_data) {
581 upb_json_printer *p = closure;
582 UPB_UNUSED(handler_data);
583 print_data(p, "\":", 2);
587 static bool mapvalue_endstr(void *closure, const void *handler_data) {
588 upb_json_printer *p = closure;
589 UPB_UNUSED(handler_data);
590 print_data(p, "\"", 1);
594 static size_t scalar_bytes(void *closure, const void *handler_data,
595 const char *str, size_t len,
596 const upb_bufhandle *handle) {
597 CHK(putkey(closure, handler_data));
598 CHK(putbytes(closure, handler_data, str, len, handle));
602 static size_t repeated_bytes(void *closure, const void *handler_data,
603 const char *str, size_t len,
604 const upb_bufhandle *handle) {
605 upb_json_printer *p = closure;
607 CHK(putbytes(closure, handler_data, str, len, handle));
611 static size_t mapkey_bytes(void *closure, const void *handler_data,
612 const char *str, size_t len,
613 const upb_bufhandle *handle) {
614 upb_json_printer *p = closure;
615 CHK(putbytes(closure, handler_data, str, len, handle));
616 print_data(p, ":", 1);
620 static void set_enum_hd(upb_handlers *h,
621 const upb_fielddef *f,
622 bool preserve_fieldnames,
623 upb_handlerattr *attr) {
624 EnumHandlerData *hd = upb_gmalloc(sizeof(EnumHandlerData));
625 hd->enumdef = upb_fielddef_enumsubdef(f);
626 hd->keyname = newstrpc(h, f, preserve_fieldnames);
627 upb_handlers_addcleanup(h, hd, upb_gfree);
628 attr->handler_data = hd;
631 /* Set up handlers for a mapentry submessage (i.e., an individual key/value pair
634 * TODO: Handle missing key, missing value, out-of-order key/value, or repeated
635 * key or value cases properly. The right way to do this is to allocate a
636 * temporary structure at the start of a mapentry submessage, store key and
637 * value data in it as key and value handlers are called, and then print the
638 * key/value pair once at the end of the submessage. If we don't do this, we
639 * should at least detect the case and throw an error. However, so far all of
640 * our sources that emit mapentry messages do so canonically (with one key
641 * field, and then one value field), so this is not a pressing concern at the
643 void printer_sethandlers_mapentry(const void *closure, bool preserve_fieldnames,
645 const upb_msgdef *md = upb_handlers_msgdef(h);
647 /* A mapentry message is printed simply as '"key": value'. Rather than
648 * special-case key and value for every type below, we just handle both
649 * fields explicitly here. */
650 const upb_fielddef* key_field = upb_msgdef_itof(md, UPB_MAPENTRY_KEY);
651 const upb_fielddef* value_field = upb_msgdef_itof(md, UPB_MAPENTRY_VALUE);
653 upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
657 switch (upb_fielddef_type(key_field)) {
659 upb_handlers_setint32(h, key_field, putmapkey_int32_t, &empty_attr);
662 upb_handlers_setint64(h, key_field, putmapkey_int64_t, &empty_attr);
664 case UPB_TYPE_UINT32:
665 upb_handlers_setuint32(h, key_field, putmapkey_uint32_t, &empty_attr);
667 case UPB_TYPE_UINT64:
668 upb_handlers_setuint64(h, key_field, putmapkey_uint64_t, &empty_attr);
671 upb_handlers_setbool(h, key_field, putmapkey_bool, &empty_attr);
673 case UPB_TYPE_STRING:
674 upb_handlers_setstartstr(h, key_field, mapkeyval_startstr, &empty_attr);
675 upb_handlers_setstring(h, key_field, mapkey_str, &empty_attr);
676 upb_handlers_setendstr(h, key_field, mapkey_endstr, &empty_attr);
679 upb_handlers_setstring(h, key_field, mapkey_bytes, &empty_attr);
686 switch (upb_fielddef_type(value_field)) {
688 upb_handlers_setint32(h, value_field, putint32_t, &empty_attr);
691 upb_handlers_setint64(h, value_field, putint64_t, &empty_attr);
693 case UPB_TYPE_UINT32:
694 upb_handlers_setuint32(h, value_field, putuint32_t, &empty_attr);
696 case UPB_TYPE_UINT64:
697 upb_handlers_setuint64(h, value_field, putuint64_t, &empty_attr);
700 upb_handlers_setbool(h, value_field, putbool, &empty_attr);
703 upb_handlers_setfloat(h, value_field, putfloat, &empty_attr);
705 case UPB_TYPE_DOUBLE:
706 upb_handlers_setdouble(h, value_field, putdouble, &empty_attr);
708 case UPB_TYPE_STRING:
709 upb_handlers_setstartstr(h, value_field, mapkeyval_startstr, &empty_attr);
710 upb_handlers_setstring(h, value_field, putstr, &empty_attr);
711 upb_handlers_setendstr(h, value_field, mapvalue_endstr, &empty_attr);
714 upb_handlers_setstring(h, value_field, putbytes, &empty_attr);
716 case UPB_TYPE_ENUM: {
717 upb_handlerattr enum_attr = UPB_HANDLERATTR_INIT;
718 set_enum_hd(h, value_field, preserve_fieldnames, &enum_attr);
719 upb_handlers_setint32(h, value_field, mapvalue_enum, &enum_attr);
722 case UPB_TYPE_MESSAGE:
723 /* No handler necessary -- the submsg handlers will print the message
729 static bool putseconds(void *closure, const void *handler_data,
731 upb_json_printer *p = closure;
732 p->seconds = seconds;
733 UPB_UNUSED(handler_data);
737 static bool putnanos(void *closure, const void *handler_data,
739 upb_json_printer *p = closure;
741 UPB_UNUSED(handler_data);
745 static void *scalar_startstr_nokey(void *closure, const void *handler_data,
747 upb_json_printer *p = closure;
748 UPB_UNUSED(handler_data);
749 UPB_UNUSED(size_hint);
750 print_data(p, "\"", 1);
754 static size_t putstr_nokey(void *closure, const void *handler_data,
755 const char *str, size_t len,
756 const upb_bufhandle *handle) {
757 upb_json_printer *p = closure;
758 UPB_UNUSED(handler_data);
760 print_data(p, "\"", 1);
761 putstring(p, str, len);
762 print_data(p, "\"", 1);
766 static void *startseq_nokey(void *closure, const void *handler_data) {
767 upb_json_printer *p = closure;
768 UPB_UNUSED(handler_data);
770 p->first_elem_[p->depth_] = true;
771 print_data(p, "[", 1);
775 static void *startseq_fieldmask(void *closure, const void *handler_data) {
776 upb_json_printer *p = closure;
777 UPB_UNUSED(handler_data);
779 p->first_elem_[p->depth_] = true;
783 static bool endseq_fieldmask(void *closure, const void *handler_data) {
784 upb_json_printer *p = closure;
785 UPB_UNUSED(handler_data);
790 static void *repeated_startstr_fieldmask(
791 void *closure, const void *handler_data,
793 upb_json_printer *p = closure;
794 UPB_UNUSED(handler_data);
795 UPB_UNUSED(size_hint);
800 static size_t repeated_str_fieldmask(
801 void *closure, const void *handler_data,
802 const char *str, size_t len,
803 const upb_bufhandle *handle) {
804 const char* limit = str + len;
806 size_t result_len = 0;
807 for (; str < limit; str++) {
812 if (upper && *str >= 'a' && *str <= 'z') {
813 char upper_char = toupper(*str);
814 CHK(putstr(closure, handler_data, &upper_char, 1, handle));
816 CHK(putstr(closure, handler_data, str, 1, handle));
824 static void *startmap_nokey(void *closure, const void *handler_data) {
825 upb_json_printer *p = closure;
826 UPB_UNUSED(handler_data);
828 p->first_elem_[p->depth_] = true;
829 print_data(p, "{", 1);
833 static bool putnull(void *closure, const void *handler_data,
835 upb_json_printer *p = closure;
836 print_data(p, "null", 4);
837 UPB_UNUSED(handler_data);
842 static bool printer_startdurationmsg(void *closure, const void *handler_data) {
843 upb_json_printer *p = closure;
844 UPB_UNUSED(handler_data);
845 if (p->depth_ == 0) {
846 upb_bytessink_start(p->output_, 0, &p->subc_);
851 #define UPB_DURATION_MAX_JSON_LEN 23
852 #define UPB_DURATION_MAX_NANO_LEN 9
854 static bool printer_enddurationmsg(void *closure, const void *handler_data,
856 upb_json_printer *p = closure;
857 char buffer[UPB_DURATION_MAX_JSON_LEN];
862 memset(buffer, 0, UPB_DURATION_MAX_JSON_LEN);
864 if (p->seconds < -315576000000) {
865 upb_status_seterrf(s, "error parsing duration: "
866 "minimum acceptable value is "
871 if (p->seconds > 315576000000) {
872 upb_status_seterrf(s, "error serializing duration: "
873 "maximum acceptable value is "
878 _upb_snprintf(buffer, sizeof(buffer), "%ld", (long)p->seconds);
879 base_len = strlen(buffer);
882 char nanos_buffer[UPB_DURATION_MAX_NANO_LEN + 3];
883 _upb_snprintf(nanos_buffer, sizeof(nanos_buffer), "%.9f",
884 p->nanos / 1000000000.0);
885 /* Remove trailing 0. */
886 for (i = UPB_DURATION_MAX_NANO_LEN + 2;
887 nanos_buffer[i] == '0'; i--) {
890 strcpy(buffer + base_len, nanos_buffer + 1);
893 curr = strlen(buffer);
894 strcpy(buffer + curr, "s");
899 print_data(p, "\"", 1);
900 print_data(p, buffer, strlen(buffer));
901 print_data(p, "\"", 1);
903 if (p->depth_ == 0) {
904 upb_bytessink_end(p->output_);
907 UPB_UNUSED(handler_data);
911 static bool printer_starttimestampmsg(void *closure, const void *handler_data) {
912 upb_json_printer *p = closure;
913 UPB_UNUSED(handler_data);
914 if (p->depth_ == 0) {
915 upb_bytessink_start(p->output_, 0, &p->subc_);
920 #define UPB_TIMESTAMP_MAX_JSON_LEN 31
921 #define UPB_TIMESTAMP_BEFORE_NANO_LEN 19
922 #define UPB_TIMESTAMP_MAX_NANO_LEN 9
924 static bool printer_endtimestampmsg(void *closure, const void *handler_data,
926 upb_json_printer *p = closure;
927 char buffer[UPB_TIMESTAMP_MAX_JSON_LEN];
928 time_t time = p->seconds;
932 strftime(buffer, UPB_TIMESTAMP_MAX_JSON_LEN, "%Y", gmtime(&time));
934 if (p->seconds < -62135596800) {
935 upb_status_seterrf(s, "error parsing timestamp: "
936 "minimum acceptable value is "
937 "0001-01-01T00:00:00Z");
941 if (p->seconds > 253402300799) {
942 upb_status_seterrf(s, "error parsing timestamp: "
943 "maximum acceptable value is "
944 "9999-12-31T23:59:59Z");
948 /* strftime doesn't guarantee 4 digits for year. Prepend 0 by ourselves. */
949 for (i = 0; i < 4 - year_length; i++) {
953 strftime(buffer + (4 - year_length), UPB_TIMESTAMP_MAX_JSON_LEN,
954 "%Y-%m-%dT%H:%M:%S", gmtime(&time));
956 char nanos_buffer[UPB_TIMESTAMP_MAX_NANO_LEN + 3];
957 _upb_snprintf(nanos_buffer, sizeof(nanos_buffer), "%.9f",
958 p->nanos / 1000000000.0);
959 /* Remove trailing 0. */
960 for (i = UPB_TIMESTAMP_MAX_NANO_LEN + 2;
961 nanos_buffer[i] == '0'; i--) {
964 strcpy(buffer + UPB_TIMESTAMP_BEFORE_NANO_LEN, nanos_buffer + 1);
967 curr = strlen(buffer);
968 strcpy(buffer + curr, "Z");
973 print_data(p, "\"", 1);
974 print_data(p, buffer, strlen(buffer));
975 print_data(p, "\"", 1);
977 if (p->depth_ == 0) {
978 upb_bytessink_end(p->output_);
981 UPB_UNUSED(handler_data);
986 static bool printer_startmsg_noframe(void *closure, const void *handler_data) {
987 upb_json_printer *p = closure;
988 UPB_UNUSED(handler_data);
989 if (p->depth_ == 0) {
990 upb_bytessink_start(p->output_, 0, &p->subc_);
995 static bool printer_endmsg_noframe(
996 void *closure, const void *handler_data, upb_status *s) {
997 upb_json_printer *p = closure;
998 UPB_UNUSED(handler_data);
1000 if (p->depth_ == 0) {
1001 upb_bytessink_end(p->output_);
1006 static bool printer_startmsg_fieldmask(
1007 void *closure, const void *handler_data) {
1008 upb_json_printer *p = closure;
1009 UPB_UNUSED(handler_data);
1010 if (p->depth_ == 0) {
1011 upb_bytessink_start(p->output_, 0, &p->subc_);
1013 print_data(p, "\"", 1);
1017 static bool printer_endmsg_fieldmask(
1018 void *closure, const void *handler_data, upb_status *s) {
1019 upb_json_printer *p = closure;
1020 UPB_UNUSED(handler_data);
1022 print_data(p, "\"", 1);
1023 if (p->depth_ == 0) {
1024 upb_bytessink_end(p->output_);
1029 static void *scalar_startstr_onlykey(
1030 void *closure, const void *handler_data, size_t size_hint) {
1031 upb_json_printer *p = closure;
1032 UPB_UNUSED(size_hint);
1033 CHK(putkey(closure, handler_data));
1037 /* Set up handlers for an Any submessage. */
1038 void printer_sethandlers_any(const void *closure, upb_handlers *h) {
1039 const upb_msgdef *md = upb_handlers_msgdef(h);
1041 const upb_fielddef* type_field = upb_msgdef_itof(md, UPB_ANY_TYPE);
1042 const upb_fielddef* value_field = upb_msgdef_itof(md, UPB_ANY_VALUE);
1044 upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
1046 /* type_url's json name is "@type" */
1047 upb_handlerattr type_name_attr = UPB_HANDLERATTR_INIT;
1048 upb_handlerattr value_name_attr = UPB_HANDLERATTR_INIT;
1049 strpc *type_url_json_name = newstrpc_str(h, "@type");
1050 strpc *value_json_name = newstrpc_str(h, "value");
1052 type_name_attr.handler_data = type_url_json_name;
1053 value_name_attr.handler_data = value_json_name;
1055 /* Set up handlers. */
1056 upb_handlers_setstartmsg(h, printer_startmsg, &empty_attr);
1057 upb_handlers_setendmsg(h, printer_endmsg, &empty_attr);
1059 upb_handlers_setstartstr(h, type_field, scalar_startstr, &type_name_attr);
1060 upb_handlers_setstring(h, type_field, scalar_str, &empty_attr);
1061 upb_handlers_setendstr(h, type_field, scalar_endstr, &empty_attr);
1063 /* This is not the full and correct JSON encoding for the Any value field. It
1064 * requires further processing by the wrapper code based on the type URL.
1066 upb_handlers_setstartstr(h, value_field, scalar_startstr_onlykey,
1069 UPB_UNUSED(closure);
1072 /* Set up handlers for a fieldmask submessage. */
1073 void printer_sethandlers_fieldmask(const void *closure, upb_handlers *h) {
1074 const upb_msgdef *md = upb_handlers_msgdef(h);
1075 const upb_fielddef* f = upb_msgdef_itof(md, 1);
1077 upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
1079 upb_handlers_setstartseq(h, f, startseq_fieldmask, &empty_attr);
1080 upb_handlers_setendseq(h, f, endseq_fieldmask, &empty_attr);
1082 upb_handlers_setstartmsg(h, printer_startmsg_fieldmask, &empty_attr);
1083 upb_handlers_setendmsg(h, printer_endmsg_fieldmask, &empty_attr);
1085 upb_handlers_setstartstr(h, f, repeated_startstr_fieldmask, &empty_attr);
1086 upb_handlers_setstring(h, f, repeated_str_fieldmask, &empty_attr);
1088 UPB_UNUSED(closure);
1091 /* Set up handlers for a duration submessage. */
1092 void printer_sethandlers_duration(const void *closure, upb_handlers *h) {
1093 const upb_msgdef *md = upb_handlers_msgdef(h);
1095 const upb_fielddef* seconds_field =
1096 upb_msgdef_itof(md, UPB_DURATION_SECONDS);
1097 const upb_fielddef* nanos_field =
1098 upb_msgdef_itof(md, UPB_DURATION_NANOS);
1100 upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
1102 upb_handlers_setstartmsg(h, printer_startdurationmsg, &empty_attr);
1103 upb_handlers_setint64(h, seconds_field, putseconds, &empty_attr);
1104 upb_handlers_setint32(h, nanos_field, putnanos, &empty_attr);
1105 upb_handlers_setendmsg(h, printer_enddurationmsg, &empty_attr);
1107 UPB_UNUSED(closure);
1110 /* Set up handlers for a timestamp submessage. Instead of printing fields
1111 * separately, the json representation of timestamp follows RFC 3339 */
1112 void printer_sethandlers_timestamp(const void *closure, upb_handlers *h) {
1113 const upb_msgdef *md = upb_handlers_msgdef(h);
1115 const upb_fielddef* seconds_field =
1116 upb_msgdef_itof(md, UPB_TIMESTAMP_SECONDS);
1117 const upb_fielddef* nanos_field =
1118 upb_msgdef_itof(md, UPB_TIMESTAMP_NANOS);
1120 upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
1122 upb_handlers_setstartmsg(h, printer_starttimestampmsg, &empty_attr);
1123 upb_handlers_setint64(h, seconds_field, putseconds, &empty_attr);
1124 upb_handlers_setint32(h, nanos_field, putnanos, &empty_attr);
1125 upb_handlers_setendmsg(h, printer_endtimestampmsg, &empty_attr);
1127 UPB_UNUSED(closure);
1130 void printer_sethandlers_value(const void *closure, upb_handlers *h) {
1131 const upb_msgdef *md = upb_handlers_msgdef(h);
1132 upb_msg_field_iter i;
1134 upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
1136 upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr);
1137 upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr);
1139 upb_msg_field_begin(&i, md);
1140 for(; !upb_msg_field_done(&i); upb_msg_field_next(&i)) {
1141 const upb_fielddef *f = upb_msg_iter_field(&i);
1143 switch (upb_fielddef_type(f)) {
1145 upb_handlers_setint32(h, f, putnull, &empty_attr);
1147 case UPB_TYPE_DOUBLE:
1148 upb_handlers_setdouble(h, f, putdouble, &empty_attr);
1150 case UPB_TYPE_STRING:
1151 upb_handlers_setstartstr(h, f, scalar_startstr_nokey, &empty_attr);
1152 upb_handlers_setstring(h, f, scalar_str, &empty_attr);
1153 upb_handlers_setendstr(h, f, scalar_endstr, &empty_attr);
1156 upb_handlers_setbool(h, f, putbool, &empty_attr);
1158 case UPB_TYPE_MESSAGE:
1166 UPB_UNUSED(closure);
1169 #define WRAPPER_SETHANDLERS(wrapper, type, putmethod) \
1170 void printer_sethandlers_##wrapper(const void *closure, upb_handlers *h) { \
1171 const upb_msgdef *md = upb_handlers_msgdef(h); \
1172 const upb_fielddef* f = upb_msgdef_itof(md, 1); \
1173 upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT; \
1174 upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr); \
1175 upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr); \
1176 upb_handlers_set##type(h, f, putmethod, &empty_attr); \
1177 UPB_UNUSED(closure); \
1180 WRAPPER_SETHANDLERS(doublevalue, double, putdouble)
1181 WRAPPER_SETHANDLERS(floatvalue, float, putfloat)
1182 WRAPPER_SETHANDLERS(int64value, int64, putint64_t)
1183 WRAPPER_SETHANDLERS(uint64value, uint64, putuint64_t)
1184 WRAPPER_SETHANDLERS(int32value, int32, putint32_t)
1185 WRAPPER_SETHANDLERS(uint32value, uint32, putuint32_t)
1186 WRAPPER_SETHANDLERS(boolvalue, bool, putbool)
1187 WRAPPER_SETHANDLERS(stringvalue, string, putstr_nokey)
1188 WRAPPER_SETHANDLERS(bytesvalue, string, putbytes)
1190 #undef WRAPPER_SETHANDLERS
1192 void printer_sethandlers_listvalue(const void *closure, upb_handlers *h) {
1193 const upb_msgdef *md = upb_handlers_msgdef(h);
1194 const upb_fielddef* f = upb_msgdef_itof(md, 1);
1196 upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
1198 upb_handlers_setstartseq(h, f, startseq_nokey, &empty_attr);
1199 upb_handlers_setendseq(h, f, endseq, &empty_attr);
1201 upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr);
1202 upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr);
1204 upb_handlers_setstartsubmsg(h, f, repeated_startsubmsg, &empty_attr);
1206 UPB_UNUSED(closure);
1209 void printer_sethandlers_structvalue(const void *closure, upb_handlers *h) {
1210 const upb_msgdef *md = upb_handlers_msgdef(h);
1211 const upb_fielddef* f = upb_msgdef_itof(md, 1);
1213 upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
1215 upb_handlers_setstartseq(h, f, startmap_nokey, &empty_attr);
1216 upb_handlers_setendseq(h, f, endmap, &empty_attr);
1218 upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr);
1219 upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr);
1221 upb_handlers_setstartsubmsg(h, f, repeated_startsubmsg, &empty_attr);
1223 UPB_UNUSED(closure);
1226 void printer_sethandlers(const void *closure, upb_handlers *h) {
1227 const upb_msgdef *md = upb_handlers_msgdef(h);
1228 bool is_mapentry = upb_msgdef_mapentry(md);
1229 upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
1230 upb_msg_field_iter i;
1231 const upb_json_printercache *cache = closure;
1232 const bool preserve_fieldnames = cache->preserve_fieldnames;
1235 /* mapentry messages are sufficiently different that we handle them
1237 printer_sethandlers_mapentry(closure, preserve_fieldnames, h);
1241 switch (upb_msgdef_wellknowntype(md)) {
1242 case UPB_WELLKNOWN_UNSPECIFIED:
1244 case UPB_WELLKNOWN_ANY:
1245 printer_sethandlers_any(closure, h);
1247 case UPB_WELLKNOWN_FIELDMASK:
1248 printer_sethandlers_fieldmask(closure, h);
1250 case UPB_WELLKNOWN_DURATION:
1251 printer_sethandlers_duration(closure, h);
1253 case UPB_WELLKNOWN_TIMESTAMP:
1254 printer_sethandlers_timestamp(closure, h);
1256 case UPB_WELLKNOWN_VALUE:
1257 printer_sethandlers_value(closure, h);
1259 case UPB_WELLKNOWN_LISTVALUE:
1260 printer_sethandlers_listvalue(closure, h);
1262 case UPB_WELLKNOWN_STRUCT:
1263 printer_sethandlers_structvalue(closure, h);
1265 #define WRAPPER(wellknowntype, name) \
1266 case wellknowntype: \
1267 printer_sethandlers_##name(closure, h); \
1270 WRAPPER(UPB_WELLKNOWN_DOUBLEVALUE, doublevalue);
1271 WRAPPER(UPB_WELLKNOWN_FLOATVALUE, floatvalue);
1272 WRAPPER(UPB_WELLKNOWN_INT64VALUE, int64value);
1273 WRAPPER(UPB_WELLKNOWN_UINT64VALUE, uint64value);
1274 WRAPPER(UPB_WELLKNOWN_INT32VALUE, int32value);
1275 WRAPPER(UPB_WELLKNOWN_UINT32VALUE, uint32value);
1276 WRAPPER(UPB_WELLKNOWN_BOOLVALUE, boolvalue);
1277 WRAPPER(UPB_WELLKNOWN_STRINGVALUE, stringvalue);
1278 WRAPPER(UPB_WELLKNOWN_BYTESVALUE, bytesvalue);
1283 upb_handlers_setstartmsg(h, printer_startmsg, &empty_attr);
1284 upb_handlers_setendmsg(h, printer_endmsg, &empty_attr);
1286 #define TYPE(type, name, ctype) \
1288 if (upb_fielddef_isseq(f)) { \
1289 upb_handlers_set##name(h, f, repeated_##ctype, &empty_attr); \
1291 upb_handlers_set##name(h, f, scalar_##ctype, &name_attr); \
1295 upb_msg_field_begin(&i, md);
1296 for(; !upb_msg_field_done(&i); upb_msg_field_next(&i)) {
1297 const upb_fielddef *f = upb_msg_iter_field(&i);
1299 upb_handlerattr name_attr = UPB_HANDLERATTR_INIT;
1300 name_attr.handler_data = newstrpc(h, f, preserve_fieldnames);
1302 if (upb_fielddef_ismap(f)) {
1303 upb_handlers_setstartseq(h, f, startmap, &name_attr);
1304 upb_handlers_setendseq(h, f, endmap, &name_attr);
1305 } else if (upb_fielddef_isseq(f)) {
1306 upb_handlers_setstartseq(h, f, startseq, &name_attr);
1307 upb_handlers_setendseq(h, f, endseq, &empty_attr);
1310 switch (upb_fielddef_type(f)) {
1311 TYPE(UPB_TYPE_FLOAT, float, float);
1312 TYPE(UPB_TYPE_DOUBLE, double, double);
1313 TYPE(UPB_TYPE_BOOL, bool, bool);
1314 TYPE(UPB_TYPE_INT32, int32, int32_t);
1315 TYPE(UPB_TYPE_UINT32, uint32, uint32_t);
1316 TYPE(UPB_TYPE_INT64, int64, int64_t);
1317 TYPE(UPB_TYPE_UINT64, uint64, uint64_t);
1318 case UPB_TYPE_ENUM: {
1319 /* For now, we always emit symbolic names for enums. We may want an
1320 * option later to control this behavior, but we will wait for a real
1322 upb_handlerattr enum_attr = UPB_HANDLERATTR_INIT;
1323 set_enum_hd(h, f, preserve_fieldnames, &enum_attr);
1325 if (upb_fielddef_isseq(f)) {
1326 upb_handlers_setint32(h, f, repeated_enum, &enum_attr);
1328 upb_handlers_setint32(h, f, scalar_enum, &enum_attr);
1333 case UPB_TYPE_STRING:
1334 if (upb_fielddef_isseq(f)) {
1335 upb_handlers_setstartstr(h, f, repeated_startstr, &empty_attr);
1336 upb_handlers_setstring(h, f, repeated_str, &empty_attr);
1337 upb_handlers_setendstr(h, f, repeated_endstr, &empty_attr);
1339 upb_handlers_setstartstr(h, f, scalar_startstr, &name_attr);
1340 upb_handlers_setstring(h, f, scalar_str, &empty_attr);
1341 upb_handlers_setendstr(h, f, scalar_endstr, &empty_attr);
1344 case UPB_TYPE_BYTES:
1345 /* XXX: this doesn't support strings that span buffers yet. The base64
1346 * encoder will need to be made resumable for this to work properly. */
1347 if (upb_fielddef_isseq(f)) {
1348 upb_handlers_setstring(h, f, repeated_bytes, &empty_attr);
1350 upb_handlers_setstring(h, f, scalar_bytes, &name_attr);
1353 case UPB_TYPE_MESSAGE:
1354 if (upb_fielddef_isseq(f)) {
1355 upb_handlers_setstartsubmsg(h, f, repeated_startsubmsg, &name_attr);
1357 upb_handlers_setstartsubmsg(h, f, scalar_startsubmsg, &name_attr);
1366 static void json_printer_reset(upb_json_printer *p) {
1371 /* Public API *****************************************************************/
1373 upb_json_printer *upb_json_printer_create(upb_arena *a, const upb_handlers *h,
1374 upb_bytessink output) {
1376 size_t size_before = upb_arena_bytesallocated(a);
1379 upb_json_printer *p = upb_arena_malloc(a, sizeof(upb_json_printer));
1380 if (!p) return NULL;
1382 p->output_ = output;
1383 json_printer_reset(p);
1384 upb_sink_reset(&p->input_, h, p);
1388 /* If this fails, increase the value in printer.h. */
1389 UPB_ASSERT_DEBUGVAR(upb_arena_bytesallocated(a) - size_before <=
1390 UPB_JSON_PRINTER_SIZE);
1394 upb_sink upb_json_printer_input(upb_json_printer *p) {
1398 upb_handlercache *upb_json_printer_newcache(bool preserve_proto_fieldnames) {
1399 upb_json_printercache *cache = upb_gmalloc(sizeof(*cache));
1400 upb_handlercache *ret = upb_handlercache_new(printer_sethandlers, cache);
1402 cache->preserve_fieldnames = preserve_proto_fieldnames;
1403 upb_handlercache_addcleanup(ret, cache, upb_gfree);