2 ** This currently uses snprintf() to format primitives, and could be optimized
6 #include "upb/json/printer.h"
16 #include "upb/port_def.inc"
18 struct upb_json_printer {
20 /* BytesSink closure. */
22 upb_bytessink output_;
24 /* We track the depth so that we know when to emit startstr/endstr on the
28 /* Have we emitted the first element? This state is necessary to emit commas
29 * without leaving a trailing comma in arrays/maps. We keep this state per
32 * Why max_depth * 2? UPB_MAX_HANDLER_DEPTH counts depth as nested messages.
33 * We count frames (contexts in which we separate elements by commas) as both
34 * repeated fields and messages (maps), and the worst case is a
35 * message->repeated field->submessage->repeated field->... nesting. */
36 bool first_elem_[UPB_MAX_HANDLER_DEPTH * 2];
38 /* To print timestamp, printer needs to cache its seconds and nanos values
39 * and convert them when ending timestamp message. See comments of
40 * printer_sethandlers_timestamp for more detail. */
45 /* StringPiece; a pointer plus a length. */
51 void freestrpc(void *ptr) {
58 bool preserve_fieldnames;
59 } upb_json_printercache;
61 /* Convert fielddef name to JSON name and return as a string piece. */
62 strpc *newstrpc(upb_handlers *h, const upb_fielddef *f,
63 bool preserve_fieldnames) {
64 /* TODO(haberman): handle malloc failure. */
65 strpc *ret = upb_gmalloc(sizeof(*ret));
66 if (preserve_fieldnames) {
67 ret->ptr = upb_gstrdup(upb_fielddef_name(f));
68 ret->len = strlen(ret->ptr);
70 ret->ptr = upb_gstrdup(upb_fielddef_jsonname(f));
71 ret->len = strlen(ret->ptr);
74 upb_handlers_addcleanup(h, ret, freestrpc);
78 /* Convert a null-terminated const char* to a string piece. */
79 strpc *newstrpc_str(upb_handlers *h, const char * str) {
80 strpc * ret = upb_gmalloc(sizeof(*ret));
81 ret->ptr = upb_gstrdup(str);
82 ret->len = strlen(str);
83 upb_handlers_addcleanup(h, ret, freestrpc);
87 /* ------------ JSON string printing: values, maps, arrays ------------------ */
89 static void print_data(
90 upb_json_printer *p, const char *buf, size_t len) {
91 /* TODO: Will need to change if we support pushback from the sink. */
92 size_t n = upb_bytessink_putbuf(p->output_, p->subc_, buf, len, NULL);
96 static void print_comma(upb_json_printer *p) {
97 if (!p->first_elem_[p->depth_]) {
98 print_data(p, ",", 1);
100 p->first_elem_[p->depth_] = false;
103 /* Helpers that print properly formatted elements to the JSON output stream. */
105 /* Used for escaping control chars in strings. */
106 static const char kControlCharLimit = 0x20;
108 UPB_INLINE bool is_json_escaped(char c) {
110 unsigned char uc = (unsigned char)c;
111 return uc < kControlCharLimit || uc == '"' || uc == '\\';
114 UPB_INLINE const char* json_nice_escape(char c) {
116 case '"': return "\\\"";
117 case '\\': return "\\\\";
118 case '\b': return "\\b";
119 case '\f': return "\\f";
120 case '\n': return "\\n";
121 case '\r': return "\\r";
122 case '\t': return "\\t";
123 default: return NULL;
127 /* Write a properly escaped string chunk. The surrounding quotes are *not*
128 * printed; this is so that the caller has the option of emitting the string
129 * content in chunks. */
130 static void putstring(upb_json_printer *p, const char *buf, size_t len) {
131 const char* unescaped_run = NULL;
133 for (i = 0; i < len; i++) {
135 /* Handle escaping. */
136 if (is_json_escaped(c)) {
137 /* Use a "nice" escape, like \n, if one exists for this character. */
138 const char* escape = json_nice_escape(c);
139 /* If we don't have a specific 'nice' escape code, use a \uXXXX-style
143 unsigned char byte = (unsigned char)c;
144 snprintf(escape_buf, sizeof(escape_buf), "\\u%04x", (int)byte);
148 /* N.B. that we assume that the input encoding is equal to the output
149 * encoding (both UTF-8 for now), so for chars >= 0x20 and != \, ", we
150 * can simply pass the bytes through. */
152 /* If there's a current run of unescaped chars, print that run first. */
154 print_data(p, unescaped_run, &buf[i] - unescaped_run);
155 unescaped_run = NULL;
157 /* Then print the escape code. */
158 print_data(p, escape, strlen(escape));
160 /* Add to the current unescaped run of characters. */
161 if (unescaped_run == NULL) {
162 unescaped_run = &buf[i];
167 /* If the string ended in a run of unescaped characters, print that last run. */
169 print_data(p, unescaped_run, &buf[len] - unescaped_run);
173 #define CHKLENGTH(x) if (!(x)) return -1;
175 /* Helpers that format floating point values according to our custom formats.
176 * Right now we use %.8g and %.17g for float/double, respectively, to match
177 * proto2::util::JsonFormat's defaults. May want to change this later. */
179 const char neginf[] = "\"-Infinity\"";
180 const char inf[] = "\"Infinity\"";
182 static size_t fmt_double(double val, char* buf, size_t length) {
183 if (val == INFINITY) {
184 CHKLENGTH(length >= strlen(inf));
187 } else if (val == -INFINITY) {
188 CHKLENGTH(length >= strlen(neginf));
190 return strlen(neginf);
192 size_t n = snprintf(buf, length, "%.17g", val);
193 CHKLENGTH(n > 0 && n < length);
198 static size_t fmt_float(float val, char* buf, size_t length) {
199 size_t n = snprintf(buf, length, "%.8g", val);
200 CHKLENGTH(n > 0 && n < length);
204 static size_t fmt_bool(bool val, char* buf, size_t length) {
205 size_t n = snprintf(buf, length, "%s", (val ? "true" : "false"));
206 CHKLENGTH(n > 0 && n < length);
210 static size_t fmt_int64_as_number(int64_t val, char* buf, size_t length) {
211 size_t n = snprintf(buf, length, "%" PRId64, val);
212 CHKLENGTH(n > 0 && n < length);
216 static size_t fmt_uint64_as_number(uint64_t val, char* buf, size_t length) {
217 size_t n = snprintf(buf, length, "%" PRIu64, val);
218 CHKLENGTH(n > 0 && n < length);
222 static size_t fmt_int64_as_string(int64_t val, char* buf, size_t length) {
223 size_t n = snprintf(buf, length, "\"%" PRId64 "\"", val);
224 CHKLENGTH(n > 0 && n < length);
228 static size_t fmt_uint64_as_string(uint64_t val, char* buf, size_t length) {
229 size_t n = snprintf(buf, length, "\"%" PRIu64 "\"", val);
230 CHKLENGTH(n > 0 && n < length);
234 /* Print a map key given a field name. Called by scalar field handlers and by
235 * startseq for repeated fields. */
236 static bool putkey(void *closure, const void *handler_data) {
237 upb_json_printer *p = closure;
238 const strpc *key = handler_data;
240 print_data(p, "\"", 1);
241 putstring(p, key->ptr, key->len);
242 print_data(p, "\":", 2);
246 #define CHKFMT(val) if ((val) == (size_t)-1) return false;
247 #define CHK(val) if (!(val)) return false;
249 #define TYPE_HANDLERS(type, fmt_func) \
250 static bool put##type(void *closure, const void *handler_data, type val) { \
251 upb_json_printer *p = closure; \
253 size_t length = fmt_func(val, data, sizeof(data)); \
254 UPB_UNUSED(handler_data); \
256 print_data(p, data, length); \
259 static bool scalar_##type(void *closure, const void *handler_data, \
261 CHK(putkey(closure, handler_data)); \
262 CHK(put##type(closure, handler_data, val)); \
265 static bool repeated_##type(void *closure, const void *handler_data, \
267 upb_json_printer *p = closure; \
269 CHK(put##type(closure, handler_data, val)); \
273 #define TYPE_HANDLERS_MAPKEY(type, fmt_func) \
274 static bool putmapkey_##type(void *closure, const void *handler_data, \
276 upb_json_printer *p = closure; \
278 size_t length = fmt_func(val, data, sizeof(data)); \
279 UPB_UNUSED(handler_data); \
280 print_data(p, "\"", 1); \
281 print_data(p, data, length); \
282 print_data(p, "\":", 2); \
286 TYPE_HANDLERS(double, fmt_double)
287 TYPE_HANDLERS(float, fmt_float)
288 TYPE_HANDLERS(bool, fmt_bool)
289 TYPE_HANDLERS(int32_t, fmt_int64_as_number)
290 TYPE_HANDLERS(uint32_t, fmt_int64_as_number)
291 TYPE_HANDLERS(int64_t, fmt_int64_as_string)
292 TYPE_HANDLERS(uint64_t, fmt_uint64_as_string)
294 /* double and float are not allowed to be map keys. */
295 TYPE_HANDLERS_MAPKEY(bool, fmt_bool)
296 TYPE_HANDLERS_MAPKEY(int32_t, fmt_int64_as_number)
297 TYPE_HANDLERS_MAPKEY(uint32_t, fmt_int64_as_number)
298 TYPE_HANDLERS_MAPKEY(int64_t, fmt_int64_as_number)
299 TYPE_HANDLERS_MAPKEY(uint64_t, fmt_uint64_as_number)
302 #undef TYPE_HANDLERS_MAPKEY
306 const upb_enumdef *enumdef;
309 static bool scalar_enum(void *closure, const void *handler_data,
311 const EnumHandlerData *hd = handler_data;
312 upb_json_printer *p = closure;
313 const char *symbolic_name;
315 CHK(putkey(closure, hd->keyname));
317 symbolic_name = upb_enumdef_iton(hd->enumdef, val);
319 print_data(p, "\"", 1);
320 putstring(p, symbolic_name, strlen(symbolic_name));
321 print_data(p, "\"", 1);
323 putint32_t(closure, NULL, val);
329 static void print_enum_symbolic_name(upb_json_printer *p,
330 const upb_enumdef *def,
332 const char *symbolic_name = upb_enumdef_iton(def, val);
334 print_data(p, "\"", 1);
335 putstring(p, symbolic_name, strlen(symbolic_name));
336 print_data(p, "\"", 1);
338 putint32_t(p, NULL, val);
342 static bool repeated_enum(void *closure, const void *handler_data,
344 const EnumHandlerData *hd = handler_data;
345 upb_json_printer *p = closure;
348 print_enum_symbolic_name(p, hd->enumdef, val);
353 static bool mapvalue_enum(void *closure, const void *handler_data,
355 const EnumHandlerData *hd = handler_data;
356 upb_json_printer *p = closure;
358 print_enum_symbolic_name(p, hd->enumdef, val);
363 static void *scalar_startsubmsg(void *closure, const void *handler_data) {
364 return putkey(closure, handler_data) ? closure : UPB_BREAK;
367 static void *repeated_startsubmsg(void *closure, const void *handler_data) {
368 upb_json_printer *p = closure;
369 UPB_UNUSED(handler_data);
374 static void start_frame(upb_json_printer *p) {
376 p->first_elem_[p->depth_] = true;
377 print_data(p, "{", 1);
380 static void end_frame(upb_json_printer *p) {
381 print_data(p, "}", 1);
385 static bool printer_startmsg(void *closure, const void *handler_data) {
386 upb_json_printer *p = closure;
387 UPB_UNUSED(handler_data);
388 if (p->depth_ == 0) {
389 upb_bytessink_start(p->output_, 0, &p->subc_);
395 static bool printer_endmsg(void *closure, const void *handler_data, upb_status *s) {
396 upb_json_printer *p = closure;
397 UPB_UNUSED(handler_data);
400 if (p->depth_ == 0) {
401 upb_bytessink_end(p->output_);
406 static void *startseq(void *closure, const void *handler_data) {
407 upb_json_printer *p = closure;
408 CHK(putkey(closure, handler_data));
410 p->first_elem_[p->depth_] = true;
411 print_data(p, "[", 1);
415 static bool endseq(void *closure, const void *handler_data) {
416 upb_json_printer *p = closure;
417 UPB_UNUSED(handler_data);
418 print_data(p, "]", 1);
423 static void *startmap(void *closure, const void *handler_data) {
424 upb_json_printer *p = closure;
425 CHK(putkey(closure, handler_data));
427 p->first_elem_[p->depth_] = true;
428 print_data(p, "{", 1);
432 static bool endmap(void *closure, const void *handler_data) {
433 upb_json_printer *p = closure;
434 UPB_UNUSED(handler_data);
435 print_data(p, "}", 1);
440 static size_t putstr(void *closure, const void *handler_data, const char *str,
441 size_t len, const upb_bufhandle *handle) {
442 upb_json_printer *p = closure;
443 UPB_UNUSED(handler_data);
445 putstring(p, str, len);
449 /* This has to Base64 encode the bytes, because JSON has no "bytes" type. */
450 static size_t putbytes(void *closure, const void *handler_data, const char *str,
451 size_t len, const upb_bufhandle *handle) {
452 upb_json_printer *p = closure;
454 /* This is the regular base64, not the "web-safe" version. */
455 static const char base64[] =
456 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
460 const char *limit = data + sizeof(data);
461 const unsigned char *from = (const unsigned char*)str;
463 size_t remaining = len;
466 UPB_UNUSED(handler_data);
469 print_data(p, "\"", 1);
471 while (remaining > 2) {
472 if (limit - to < 4) {
474 putstring(p, data, bytes);
478 to[0] = base64[from[0] >> 2];
479 to[1] = base64[((from[0] & 0x3) << 4) | (from[1] >> 4)];
480 to[2] = base64[((from[1] & 0xf) << 2) | (from[2] >> 6)];
481 to[3] = base64[from[2] & 0x3f];
490 to[0] = base64[from[0] >> 2];
491 to[1] = base64[((from[0] & 0x3) << 4) | (from[1] >> 4)];
492 to[2] = base64[(from[1] & 0xf) << 2];
498 to[0] = base64[from[0] >> 2];
499 to[1] = base64[((from[0] & 0x3) << 4)];
508 putstring(p, data, bytes);
509 print_data(p, "\"", 1);
513 static void *scalar_startstr(void *closure, const void *handler_data,
515 upb_json_printer *p = closure;
516 UPB_UNUSED(handler_data);
517 UPB_UNUSED(size_hint);
518 CHK(putkey(closure, handler_data));
519 print_data(p, "\"", 1);
523 static size_t scalar_str(void *closure, const void *handler_data,
524 const char *str, size_t len,
525 const upb_bufhandle *handle) {
526 CHK(putstr(closure, handler_data, str, len, handle));
530 static bool scalar_endstr(void *closure, const void *handler_data) {
531 upb_json_printer *p = closure;
532 UPB_UNUSED(handler_data);
533 print_data(p, "\"", 1);
537 static void *repeated_startstr(void *closure, const void *handler_data,
539 upb_json_printer *p = closure;
540 UPB_UNUSED(handler_data);
541 UPB_UNUSED(size_hint);
543 print_data(p, "\"", 1);
547 static size_t repeated_str(void *closure, const void *handler_data,
548 const char *str, size_t len,
549 const upb_bufhandle *handle) {
550 CHK(putstr(closure, handler_data, str, len, handle));
554 static bool repeated_endstr(void *closure, const void *handler_data) {
555 upb_json_printer *p = closure;
556 UPB_UNUSED(handler_data);
557 print_data(p, "\"", 1);
561 static void *mapkeyval_startstr(void *closure, const void *handler_data,
563 upb_json_printer *p = closure;
564 UPB_UNUSED(handler_data);
565 UPB_UNUSED(size_hint);
566 print_data(p, "\"", 1);
570 static size_t mapkey_str(void *closure, const void *handler_data,
571 const char *str, size_t len,
572 const upb_bufhandle *handle) {
573 CHK(putstr(closure, handler_data, str, len, handle));
577 static bool mapkey_endstr(void *closure, const void *handler_data) {
578 upb_json_printer *p = closure;
579 UPB_UNUSED(handler_data);
580 print_data(p, "\":", 2);
584 static bool mapvalue_endstr(void *closure, const void *handler_data) {
585 upb_json_printer *p = closure;
586 UPB_UNUSED(handler_data);
587 print_data(p, "\"", 1);
591 static size_t scalar_bytes(void *closure, const void *handler_data,
592 const char *str, size_t len,
593 const upb_bufhandle *handle) {
594 CHK(putkey(closure, handler_data));
595 CHK(putbytes(closure, handler_data, str, len, handle));
599 static size_t repeated_bytes(void *closure, const void *handler_data,
600 const char *str, size_t len,
601 const upb_bufhandle *handle) {
602 upb_json_printer *p = closure;
604 CHK(putbytes(closure, handler_data, str, len, handle));
608 static size_t mapkey_bytes(void *closure, const void *handler_data,
609 const char *str, size_t len,
610 const upb_bufhandle *handle) {
611 upb_json_printer *p = closure;
612 CHK(putbytes(closure, handler_data, str, len, handle));
613 print_data(p, ":", 1);
617 static void set_enum_hd(upb_handlers *h,
618 const upb_fielddef *f,
619 bool preserve_fieldnames,
620 upb_handlerattr *attr) {
621 EnumHandlerData *hd = upb_gmalloc(sizeof(EnumHandlerData));
622 hd->enumdef = upb_fielddef_enumsubdef(f);
623 hd->keyname = newstrpc(h, f, preserve_fieldnames);
624 upb_handlers_addcleanup(h, hd, upb_gfree);
625 attr->handler_data = hd;
628 /* Set up handlers for a mapentry submessage (i.e., an individual key/value pair
631 * TODO: Handle missing key, missing value, out-of-order key/value, or repeated
632 * key or value cases properly. The right way to do this is to allocate a
633 * temporary structure at the start of a mapentry submessage, store key and
634 * value data in it as key and value handlers are called, and then print the
635 * key/value pair once at the end of the submessage. If we don't do this, we
636 * should at least detect the case and throw an error. However, so far all of
637 * our sources that emit mapentry messages do so canonically (with one key
638 * field, and then one value field), so this is not a pressing concern at the
640 void printer_sethandlers_mapentry(const void *closure, bool preserve_fieldnames,
642 const upb_msgdef *md = upb_handlers_msgdef(h);
644 /* A mapentry message is printed simply as '"key": value'. Rather than
645 * special-case key and value for every type below, we just handle both
646 * fields explicitly here. */
647 const upb_fielddef* key_field = upb_msgdef_itof(md, UPB_MAPENTRY_KEY);
648 const upb_fielddef* value_field = upb_msgdef_itof(md, UPB_MAPENTRY_VALUE);
650 upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
654 switch (upb_fielddef_type(key_field)) {
656 upb_handlers_setint32(h, key_field, putmapkey_int32_t, &empty_attr);
659 upb_handlers_setint64(h, key_field, putmapkey_int64_t, &empty_attr);
661 case UPB_TYPE_UINT32:
662 upb_handlers_setuint32(h, key_field, putmapkey_uint32_t, &empty_attr);
664 case UPB_TYPE_UINT64:
665 upb_handlers_setuint64(h, key_field, putmapkey_uint64_t, &empty_attr);
668 upb_handlers_setbool(h, key_field, putmapkey_bool, &empty_attr);
670 case UPB_TYPE_STRING:
671 upb_handlers_setstartstr(h, key_field, mapkeyval_startstr, &empty_attr);
672 upb_handlers_setstring(h, key_field, mapkey_str, &empty_attr);
673 upb_handlers_setendstr(h, key_field, mapkey_endstr, &empty_attr);
676 upb_handlers_setstring(h, key_field, mapkey_bytes, &empty_attr);
683 switch (upb_fielddef_type(value_field)) {
685 upb_handlers_setint32(h, value_field, putint32_t, &empty_attr);
688 upb_handlers_setint64(h, value_field, putint64_t, &empty_attr);
690 case UPB_TYPE_UINT32:
691 upb_handlers_setuint32(h, value_field, putuint32_t, &empty_attr);
693 case UPB_TYPE_UINT64:
694 upb_handlers_setuint64(h, value_field, putuint64_t, &empty_attr);
697 upb_handlers_setbool(h, value_field, putbool, &empty_attr);
700 upb_handlers_setfloat(h, value_field, putfloat, &empty_attr);
702 case UPB_TYPE_DOUBLE:
703 upb_handlers_setdouble(h, value_field, putdouble, &empty_attr);
705 case UPB_TYPE_STRING:
706 upb_handlers_setstartstr(h, value_field, mapkeyval_startstr, &empty_attr);
707 upb_handlers_setstring(h, value_field, putstr, &empty_attr);
708 upb_handlers_setendstr(h, value_field, mapvalue_endstr, &empty_attr);
711 upb_handlers_setstring(h, value_field, putbytes, &empty_attr);
713 case UPB_TYPE_ENUM: {
714 upb_handlerattr enum_attr = UPB_HANDLERATTR_INIT;
715 set_enum_hd(h, value_field, preserve_fieldnames, &enum_attr);
716 upb_handlers_setint32(h, value_field, mapvalue_enum, &enum_attr);
719 case UPB_TYPE_MESSAGE:
720 /* No handler necessary -- the submsg handlers will print the message
726 static bool putseconds(void *closure, const void *handler_data,
728 upb_json_printer *p = closure;
729 p->seconds = seconds;
730 UPB_UNUSED(handler_data);
734 static bool putnanos(void *closure, const void *handler_data,
736 upb_json_printer *p = closure;
738 UPB_UNUSED(handler_data);
742 static void *scalar_startstr_nokey(void *closure, const void *handler_data,
744 upb_json_printer *p = closure;
745 UPB_UNUSED(handler_data);
746 UPB_UNUSED(size_hint);
747 print_data(p, "\"", 1);
751 static size_t putstr_nokey(void *closure, const void *handler_data,
752 const char *str, size_t len,
753 const upb_bufhandle *handle) {
754 upb_json_printer *p = closure;
755 UPB_UNUSED(handler_data);
757 print_data(p, "\"", 1);
758 putstring(p, str, len);
759 print_data(p, "\"", 1);
763 static void *startseq_nokey(void *closure, const void *handler_data) {
764 upb_json_printer *p = closure;
765 UPB_UNUSED(handler_data);
767 p->first_elem_[p->depth_] = true;
768 print_data(p, "[", 1);
772 static void *startseq_fieldmask(void *closure, const void *handler_data) {
773 upb_json_printer *p = closure;
774 UPB_UNUSED(handler_data);
776 p->first_elem_[p->depth_] = true;
780 static bool endseq_fieldmask(void *closure, const void *handler_data) {
781 upb_json_printer *p = closure;
782 UPB_UNUSED(handler_data);
787 static void *repeated_startstr_fieldmask(
788 void *closure, const void *handler_data,
790 upb_json_printer *p = closure;
791 UPB_UNUSED(handler_data);
792 UPB_UNUSED(size_hint);
797 static size_t repeated_str_fieldmask(
798 void *closure, const void *handler_data,
799 const char *str, size_t len,
800 const upb_bufhandle *handle) {
801 const char* limit = str + len;
803 size_t result_len = 0;
804 for (; str < limit; str++) {
809 if (upper && *str >= 'a' && *str <= 'z') {
810 char upper_char = toupper(*str);
811 CHK(putstr(closure, handler_data, &upper_char, 1, handle));
813 CHK(putstr(closure, handler_data, str, 1, handle));
821 static void *startmap_nokey(void *closure, const void *handler_data) {
822 upb_json_printer *p = closure;
823 UPB_UNUSED(handler_data);
825 p->first_elem_[p->depth_] = true;
826 print_data(p, "{", 1);
830 static bool putnull(void *closure, const void *handler_data,
832 upb_json_printer *p = closure;
833 print_data(p, "null", 4);
834 UPB_UNUSED(handler_data);
839 static bool printer_startdurationmsg(void *closure, const void *handler_data) {
840 upb_json_printer *p = closure;
841 UPB_UNUSED(handler_data);
842 if (p->depth_ == 0) {
843 upb_bytessink_start(p->output_, 0, &p->subc_);
848 #define UPB_DURATION_MAX_JSON_LEN 23
849 #define UPB_DURATION_MAX_NANO_LEN 9
851 static bool printer_enddurationmsg(void *closure, const void *handler_data,
853 upb_json_printer *p = closure;
854 char buffer[UPB_DURATION_MAX_JSON_LEN];
859 memset(buffer, 0, UPB_DURATION_MAX_JSON_LEN);
861 if (p->seconds < -315576000000) {
862 upb_status_seterrf(s, "error parsing duration: "
863 "minimum acceptable value is "
868 if (p->seconds > 315576000000) {
869 upb_status_seterrf(s, "error serializing duration: "
870 "maximum acceptable value is "
875 snprintf(buffer, sizeof(buffer), "%ld", (long)p->seconds);
876 base_len = strlen(buffer);
879 char nanos_buffer[UPB_DURATION_MAX_NANO_LEN + 3];
880 snprintf(nanos_buffer, sizeof(nanos_buffer), "%.9f",
881 p->nanos / 1000000000.0);
882 /* Remove trailing 0. */
883 for (i = UPB_DURATION_MAX_NANO_LEN + 2;
884 nanos_buffer[i] == '0'; i--) {
887 strcpy(buffer + base_len, nanos_buffer + 1);
890 curr = strlen(buffer);
891 strcpy(buffer + curr, "s");
896 print_data(p, "\"", 1);
897 print_data(p, buffer, strlen(buffer));
898 print_data(p, "\"", 1);
900 if (p->depth_ == 0) {
901 upb_bytessink_end(p->output_);
904 UPB_UNUSED(handler_data);
908 static bool printer_starttimestampmsg(void *closure, const void *handler_data) {
909 upb_json_printer *p = closure;
910 UPB_UNUSED(handler_data);
911 if (p->depth_ == 0) {
912 upb_bytessink_start(p->output_, 0, &p->subc_);
917 #define UPB_TIMESTAMP_MAX_JSON_LEN 31
918 #define UPB_TIMESTAMP_BEFORE_NANO_LEN 19
919 #define UPB_TIMESTAMP_MAX_NANO_LEN 9
921 static bool printer_endtimestampmsg(void *closure, const void *handler_data,
923 upb_json_printer *p = closure;
924 char buffer[UPB_TIMESTAMP_MAX_JSON_LEN];
925 time_t time = p->seconds;
929 strftime(buffer, UPB_TIMESTAMP_MAX_JSON_LEN, "%Y", gmtime(&time));
931 if (p->seconds < -62135596800) {
932 upb_status_seterrf(s, "error parsing timestamp: "
933 "minimum acceptable value is "
934 "0001-01-01T00:00:00Z");
938 if (p->seconds > 253402300799) {
939 upb_status_seterrf(s, "error parsing timestamp: "
940 "maximum acceptable value is "
941 "9999-12-31T23:59:59Z");
945 /* strftime doesn't guarantee 4 digits for year. Prepend 0 by ourselves. */
946 for (i = 0; i < 4 - year_length; i++) {
950 strftime(buffer + (4 - year_length), UPB_TIMESTAMP_MAX_JSON_LEN,
951 "%Y-%m-%dT%H:%M:%S", gmtime(&time));
953 char nanos_buffer[UPB_TIMESTAMP_MAX_NANO_LEN + 3];
954 snprintf(nanos_buffer, sizeof(nanos_buffer), "%.9f",
955 p->nanos / 1000000000.0);
956 /* Remove trailing 0. */
957 for (i = UPB_TIMESTAMP_MAX_NANO_LEN + 2;
958 nanos_buffer[i] == '0'; i--) {
961 strcpy(buffer + UPB_TIMESTAMP_BEFORE_NANO_LEN, nanos_buffer + 1);
964 curr = strlen(buffer);
965 strcpy(buffer + curr, "Z");
970 print_data(p, "\"", 1);
971 print_data(p, buffer, strlen(buffer));
972 print_data(p, "\"", 1);
974 if (p->depth_ == 0) {
975 upb_bytessink_end(p->output_);
978 UPB_UNUSED(handler_data);
983 static bool printer_startmsg_noframe(void *closure, const void *handler_data) {
984 upb_json_printer *p = closure;
985 UPB_UNUSED(handler_data);
986 if (p->depth_ == 0) {
987 upb_bytessink_start(p->output_, 0, &p->subc_);
992 static bool printer_endmsg_noframe(
993 void *closure, const void *handler_data, upb_status *s) {
994 upb_json_printer *p = closure;
995 UPB_UNUSED(handler_data);
997 if (p->depth_ == 0) {
998 upb_bytessink_end(p->output_);
1003 static bool printer_startmsg_fieldmask(
1004 void *closure, const void *handler_data) {
1005 upb_json_printer *p = closure;
1006 UPB_UNUSED(handler_data);
1007 if (p->depth_ == 0) {
1008 upb_bytessink_start(p->output_, 0, &p->subc_);
1010 print_data(p, "\"", 1);
1014 static bool printer_endmsg_fieldmask(
1015 void *closure, const void *handler_data, upb_status *s) {
1016 upb_json_printer *p = closure;
1017 UPB_UNUSED(handler_data);
1019 print_data(p, "\"", 1);
1020 if (p->depth_ == 0) {
1021 upb_bytessink_end(p->output_);
1026 static void *scalar_startstr_onlykey(
1027 void *closure, const void *handler_data, size_t size_hint) {
1028 upb_json_printer *p = closure;
1029 UPB_UNUSED(size_hint);
1030 CHK(putkey(closure, handler_data));
1034 /* Set up handlers for an Any submessage. */
1035 void printer_sethandlers_any(const void *closure, upb_handlers *h) {
1036 const upb_msgdef *md = upb_handlers_msgdef(h);
1038 const upb_fielddef* type_field = upb_msgdef_itof(md, UPB_ANY_TYPE);
1039 const upb_fielddef* value_field = upb_msgdef_itof(md, UPB_ANY_VALUE);
1041 upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
1043 /* type_url's json name is "@type" */
1044 upb_handlerattr type_name_attr = UPB_HANDLERATTR_INIT;
1045 upb_handlerattr value_name_attr = UPB_HANDLERATTR_INIT;
1046 strpc *type_url_json_name = newstrpc_str(h, "@type");
1047 strpc *value_json_name = newstrpc_str(h, "value");
1049 type_name_attr.handler_data = type_url_json_name;
1050 value_name_attr.handler_data = value_json_name;
1052 /* Set up handlers. */
1053 upb_handlers_setstartmsg(h, printer_startmsg, &empty_attr);
1054 upb_handlers_setendmsg(h, printer_endmsg, &empty_attr);
1056 upb_handlers_setstartstr(h, type_field, scalar_startstr, &type_name_attr);
1057 upb_handlers_setstring(h, type_field, scalar_str, &empty_attr);
1058 upb_handlers_setendstr(h, type_field, scalar_endstr, &empty_attr);
1060 /* This is not the full and correct JSON encoding for the Any value field. It
1061 * requires further processing by the wrapper code based on the type URL.
1063 upb_handlers_setstartstr(h, value_field, scalar_startstr_onlykey,
1066 UPB_UNUSED(closure);
1069 /* Set up handlers for a fieldmask submessage. */
1070 void printer_sethandlers_fieldmask(const void *closure, upb_handlers *h) {
1071 const upb_msgdef *md = upb_handlers_msgdef(h);
1072 const upb_fielddef* f = upb_msgdef_itof(md, 1);
1074 upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
1076 upb_handlers_setstartseq(h, f, startseq_fieldmask, &empty_attr);
1077 upb_handlers_setendseq(h, f, endseq_fieldmask, &empty_attr);
1079 upb_handlers_setstartmsg(h, printer_startmsg_fieldmask, &empty_attr);
1080 upb_handlers_setendmsg(h, printer_endmsg_fieldmask, &empty_attr);
1082 upb_handlers_setstartstr(h, f, repeated_startstr_fieldmask, &empty_attr);
1083 upb_handlers_setstring(h, f, repeated_str_fieldmask, &empty_attr);
1085 UPB_UNUSED(closure);
1088 /* Set up handlers for a duration submessage. */
1089 void printer_sethandlers_duration(const void *closure, upb_handlers *h) {
1090 const upb_msgdef *md = upb_handlers_msgdef(h);
1092 const upb_fielddef* seconds_field =
1093 upb_msgdef_itof(md, UPB_DURATION_SECONDS);
1094 const upb_fielddef* nanos_field =
1095 upb_msgdef_itof(md, UPB_DURATION_NANOS);
1097 upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
1099 upb_handlers_setstartmsg(h, printer_startdurationmsg, &empty_attr);
1100 upb_handlers_setint64(h, seconds_field, putseconds, &empty_attr);
1101 upb_handlers_setint32(h, nanos_field, putnanos, &empty_attr);
1102 upb_handlers_setendmsg(h, printer_enddurationmsg, &empty_attr);
1104 UPB_UNUSED(closure);
1107 /* Set up handlers for a timestamp submessage. Instead of printing fields
1108 * separately, the json representation of timestamp follows RFC 3339 */
1109 void printer_sethandlers_timestamp(const void *closure, upb_handlers *h) {
1110 const upb_msgdef *md = upb_handlers_msgdef(h);
1112 const upb_fielddef* seconds_field =
1113 upb_msgdef_itof(md, UPB_TIMESTAMP_SECONDS);
1114 const upb_fielddef* nanos_field =
1115 upb_msgdef_itof(md, UPB_TIMESTAMP_NANOS);
1117 upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
1119 upb_handlers_setstartmsg(h, printer_starttimestampmsg, &empty_attr);
1120 upb_handlers_setint64(h, seconds_field, putseconds, &empty_attr);
1121 upb_handlers_setint32(h, nanos_field, putnanos, &empty_attr);
1122 upb_handlers_setendmsg(h, printer_endtimestampmsg, &empty_attr);
1124 UPB_UNUSED(closure);
1127 void printer_sethandlers_value(const void *closure, upb_handlers *h) {
1128 const upb_msgdef *md = upb_handlers_msgdef(h);
1131 upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
1133 upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr);
1134 upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr);
1136 n = upb_msgdef_fieldcount(md);
1137 for (i = 0; i < n; i++) {
1138 const upb_fielddef *f = upb_msgdef_field(md, i);
1140 switch (upb_fielddef_type(f)) {
1142 upb_handlers_setint32(h, f, putnull, &empty_attr);
1144 case UPB_TYPE_DOUBLE:
1145 upb_handlers_setdouble(h, f, putdouble, &empty_attr);
1147 case UPB_TYPE_STRING:
1148 upb_handlers_setstartstr(h, f, scalar_startstr_nokey, &empty_attr);
1149 upb_handlers_setstring(h, f, scalar_str, &empty_attr);
1150 upb_handlers_setendstr(h, f, scalar_endstr, &empty_attr);
1153 upb_handlers_setbool(h, f, putbool, &empty_attr);
1155 case UPB_TYPE_MESSAGE:
1163 UPB_UNUSED(closure);
1166 #define WRAPPER_SETHANDLERS(wrapper, type, putmethod) \
1167 void printer_sethandlers_##wrapper(const void *closure, upb_handlers *h) { \
1168 const upb_msgdef *md = upb_handlers_msgdef(h); \
1169 const upb_fielddef* f = upb_msgdef_itof(md, 1); \
1170 upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT; \
1171 upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr); \
1172 upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr); \
1173 upb_handlers_set##type(h, f, putmethod, &empty_attr); \
1174 UPB_UNUSED(closure); \
1177 WRAPPER_SETHANDLERS(doublevalue, double, putdouble)
1178 WRAPPER_SETHANDLERS(floatvalue, float, putfloat)
1179 WRAPPER_SETHANDLERS(int64value, int64, putint64_t)
1180 WRAPPER_SETHANDLERS(uint64value, uint64, putuint64_t)
1181 WRAPPER_SETHANDLERS(int32value, int32, putint32_t)
1182 WRAPPER_SETHANDLERS(uint32value, uint32, putuint32_t)
1183 WRAPPER_SETHANDLERS(boolvalue, bool, putbool)
1184 WRAPPER_SETHANDLERS(stringvalue, string, putstr_nokey)
1185 WRAPPER_SETHANDLERS(bytesvalue, string, putbytes)
1187 #undef WRAPPER_SETHANDLERS
1189 void printer_sethandlers_listvalue(const void *closure, upb_handlers *h) {
1190 const upb_msgdef *md = upb_handlers_msgdef(h);
1191 const upb_fielddef* f = upb_msgdef_itof(md, 1);
1193 upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
1195 upb_handlers_setstartseq(h, f, startseq_nokey, &empty_attr);
1196 upb_handlers_setendseq(h, f, endseq, &empty_attr);
1198 upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr);
1199 upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr);
1201 upb_handlers_setstartsubmsg(h, f, repeated_startsubmsg, &empty_attr);
1203 UPB_UNUSED(closure);
1206 void printer_sethandlers_structvalue(const void *closure, upb_handlers *h) {
1207 const upb_msgdef *md = upb_handlers_msgdef(h);
1208 const upb_fielddef* f = upb_msgdef_itof(md, 1);
1210 upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
1212 upb_handlers_setstartseq(h, f, startmap_nokey, &empty_attr);
1213 upb_handlers_setendseq(h, f, endmap, &empty_attr);
1215 upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr);
1216 upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr);
1218 upb_handlers_setstartsubmsg(h, f, repeated_startsubmsg, &empty_attr);
1220 UPB_UNUSED(closure);
1223 void printer_sethandlers(const void *closure, upb_handlers *h) {
1224 const upb_msgdef *md = upb_handlers_msgdef(h);
1225 bool is_mapentry = upb_msgdef_mapentry(md);
1226 upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
1228 const upb_json_printercache *cache = closure;
1229 const bool preserve_fieldnames = cache->preserve_fieldnames;
1232 /* mapentry messages are sufficiently different that we handle them
1234 printer_sethandlers_mapentry(closure, preserve_fieldnames, h);
1238 switch (upb_msgdef_wellknowntype(md)) {
1239 case UPB_WELLKNOWN_UNSPECIFIED:
1241 case UPB_WELLKNOWN_ANY:
1242 printer_sethandlers_any(closure, h);
1244 case UPB_WELLKNOWN_FIELDMASK:
1245 printer_sethandlers_fieldmask(closure, h);
1247 case UPB_WELLKNOWN_DURATION:
1248 printer_sethandlers_duration(closure, h);
1250 case UPB_WELLKNOWN_TIMESTAMP:
1251 printer_sethandlers_timestamp(closure, h);
1253 case UPB_WELLKNOWN_VALUE:
1254 printer_sethandlers_value(closure, h);
1256 case UPB_WELLKNOWN_LISTVALUE:
1257 printer_sethandlers_listvalue(closure, h);
1259 case UPB_WELLKNOWN_STRUCT:
1260 printer_sethandlers_structvalue(closure, h);
1262 #define WRAPPER(wellknowntype, name) \
1263 case wellknowntype: \
1264 printer_sethandlers_##name(closure, h); \
1267 WRAPPER(UPB_WELLKNOWN_DOUBLEVALUE, doublevalue);
1268 WRAPPER(UPB_WELLKNOWN_FLOATVALUE, floatvalue);
1269 WRAPPER(UPB_WELLKNOWN_INT64VALUE, int64value);
1270 WRAPPER(UPB_WELLKNOWN_UINT64VALUE, uint64value);
1271 WRAPPER(UPB_WELLKNOWN_INT32VALUE, int32value);
1272 WRAPPER(UPB_WELLKNOWN_UINT32VALUE, uint32value);
1273 WRAPPER(UPB_WELLKNOWN_BOOLVALUE, boolvalue);
1274 WRAPPER(UPB_WELLKNOWN_STRINGVALUE, stringvalue);
1275 WRAPPER(UPB_WELLKNOWN_BYTESVALUE, bytesvalue);
1280 upb_handlers_setstartmsg(h, printer_startmsg, &empty_attr);
1281 upb_handlers_setendmsg(h, printer_endmsg, &empty_attr);
1283 #define TYPE(type, name, ctype) \
1285 if (upb_fielddef_isseq(f)) { \
1286 upb_handlers_set##name(h, f, repeated_##ctype, &empty_attr); \
1288 upb_handlers_set##name(h, f, scalar_##ctype, &name_attr); \
1292 n = upb_msgdef_fieldcount(md);
1293 for (i = 0; i < n; i++) {
1294 const upb_fielddef *f = upb_msgdef_field(md, i);
1296 upb_handlerattr name_attr = UPB_HANDLERATTR_INIT;
1297 name_attr.handler_data = newstrpc(h, f, preserve_fieldnames);
1299 if (upb_fielddef_ismap(f)) {
1300 upb_handlers_setstartseq(h, f, startmap, &name_attr);
1301 upb_handlers_setendseq(h, f, endmap, &name_attr);
1302 } else if (upb_fielddef_isseq(f)) {
1303 upb_handlers_setstartseq(h, f, startseq, &name_attr);
1304 upb_handlers_setendseq(h, f, endseq, &empty_attr);
1307 switch (upb_fielddef_type(f)) {
1308 TYPE(UPB_TYPE_FLOAT, float, float);
1309 TYPE(UPB_TYPE_DOUBLE, double, double);
1310 TYPE(UPB_TYPE_BOOL, bool, bool);
1311 TYPE(UPB_TYPE_INT32, int32, int32_t);
1312 TYPE(UPB_TYPE_UINT32, uint32, uint32_t);
1313 TYPE(UPB_TYPE_INT64, int64, int64_t);
1314 TYPE(UPB_TYPE_UINT64, uint64, uint64_t);
1315 case UPB_TYPE_ENUM: {
1316 /* For now, we always emit symbolic names for enums. We may want an
1317 * option later to control this behavior, but we will wait for a real
1319 upb_handlerattr enum_attr = UPB_HANDLERATTR_INIT;
1320 set_enum_hd(h, f, preserve_fieldnames, &enum_attr);
1322 if (upb_fielddef_isseq(f)) {
1323 upb_handlers_setint32(h, f, repeated_enum, &enum_attr);
1325 upb_handlers_setint32(h, f, scalar_enum, &enum_attr);
1330 case UPB_TYPE_STRING:
1331 if (upb_fielddef_isseq(f)) {
1332 upb_handlers_setstartstr(h, f, repeated_startstr, &empty_attr);
1333 upb_handlers_setstring(h, f, repeated_str, &empty_attr);
1334 upb_handlers_setendstr(h, f, repeated_endstr, &empty_attr);
1336 upb_handlers_setstartstr(h, f, scalar_startstr, &name_attr);
1337 upb_handlers_setstring(h, f, scalar_str, &empty_attr);
1338 upb_handlers_setendstr(h, f, scalar_endstr, &empty_attr);
1341 case UPB_TYPE_BYTES:
1342 /* XXX: this doesn't support strings that span buffers yet. The base64
1343 * encoder will need to be made resumable for this to work properly. */
1344 if (upb_fielddef_isseq(f)) {
1345 upb_handlers_setstring(h, f, repeated_bytes, &empty_attr);
1347 upb_handlers_setstring(h, f, scalar_bytes, &name_attr);
1350 case UPB_TYPE_MESSAGE:
1351 if (upb_fielddef_isseq(f)) {
1352 upb_handlers_setstartsubmsg(h, f, repeated_startsubmsg, &name_attr);
1354 upb_handlers_setstartsubmsg(h, f, scalar_startsubmsg, &name_attr);
1363 static void json_printer_reset(upb_json_printer *p) {
1368 /* Public API *****************************************************************/
1370 upb_json_printer *upb_json_printer_create(upb_arena *a, const upb_handlers *h,
1371 upb_bytessink output) {
1372 upb_json_printer *p = upb_arena_malloc(a, sizeof(upb_json_printer));
1373 if (!p) return NULL;
1375 p->output_ = output;
1376 json_printer_reset(p);
1377 upb_sink_reset(&p->input_, h, p);
1384 upb_sink upb_json_printer_input(upb_json_printer *p) {
1388 upb_handlercache *upb_json_printer_newcache(bool preserve_proto_fieldnames) {
1389 upb_json_printercache *cache = upb_gmalloc(sizeof(*cache));
1390 upb_handlercache *ret = upb_handlercache_new(printer_sethandlers, cache);
1392 cache->preserve_fieldnames = preserve_proto_fieldnames;
1393 upb_handlercache_addcleanup(ret, cache, upb_gfree);