Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc / deps / grpc / third_party / upb / upb / json / printer.c
1 /*
2 ** This currently uses snprintf() to format primitives, and could be optimized
3 ** further.
4 */
5
6 #include "upb/json/printer.h"
7
8 #include <ctype.h>
9 #include <stdint.h>
10 #include <string.h>
11 #include <time.h>
12
13 #include "upb/port_def.inc"
14
15 struct upb_json_printer {
16   upb_sink input_;
17   /* BytesSink closure. */
18   void *subc_;
19   upb_bytessink output_;
20
21   /* We track the depth so that we know when to emit startstr/endstr on the
22    * output. */
23   int depth_;
24
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
27    * frame depth.
28    *
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];
34
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. */
38   int64_t seconds;
39   int32_t nanos;
40 };
41
42 /* StringPiece; a pointer plus a length. */
43 typedef struct {
44   char *ptr;
45   size_t len;
46 } strpc;
47
48 void freestrpc(void *ptr) {
49   strpc *pc = ptr;
50   upb_gfree(pc->ptr);
51   upb_gfree(pc);
52 }
53
54 typedef struct {
55   bool preserve_fieldnames;
56 } upb_json_printercache;
57
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);
66   } else {
67     size_t len;
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 */
73   }
74
75   upb_handlers_addcleanup(h, ret, freestrpc);
76   return ret;
77 }
78
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);
85   return ret;
86 }
87
88 /* ------------ JSON string printing: values, maps, arrays ------------------ */
89
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);
94   UPB_ASSERT(n == len);
95 }
96
97 static void print_comma(upb_json_printer *p) {
98   if (!p->first_elem_[p->depth_]) {
99     print_data(p, ",", 1);
100   }
101   p->first_elem_[p->depth_] = false;
102 }
103
104 /* Helpers that print properly formatted elements to the JSON output stream. */
105
106 /* Used for escaping control chars in strings. */
107 static const char kControlCharLimit = 0x20;
108
109 UPB_INLINE bool is_json_escaped(char c) {
110   /* See RFC 4627. */
111   unsigned char uc = (unsigned char)c;
112   return uc < kControlCharLimit || uc == '"' || uc == '\\';
113 }
114
115 UPB_INLINE const char* json_nice_escape(char c) {
116   switch (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;
125   }
126 }
127
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;
133   unsigned int i;
134   for (i = 0; i < len; i++) {
135     char c = buf[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
141        * escape. */
142       char escape_buf[8];
143       if (!escape) {
144         unsigned char byte = (unsigned char)c;
145         _upb_snprintf(escape_buf, sizeof(escape_buf), "\\u%04x", (int)byte);
146         escape = escape_buf;
147       }
148
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. */
152
153       /* If there's a current run of unescaped chars, print that run first. */
154       if (unescaped_run) {
155         print_data(p, unescaped_run, &buf[i] - unescaped_run);
156         unescaped_run = NULL;
157       }
158       /* Then print the escape code. */
159       print_data(p, escape, strlen(escape));
160     } else {
161       /* Add to the current unescaped run of characters. */
162       if (unescaped_run == NULL) {
163         unescaped_run = &buf[i];
164       }
165     }
166   }
167
168   /* If the string ended in a run of unescaped characters, print that last run. */
169   if (unescaped_run) {
170     print_data(p, unescaped_run, &buf[len] - unescaped_run);
171   }
172 }
173
174 #define CHKLENGTH(x) if (!(x)) return -1;
175
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. */
179
180 const char neginf[] = "\"-Infinity\"";
181 const char inf[] = "\"Infinity\"";
182
183 static size_t fmt_double(double val, char* buf, size_t length) {
184   if (val == UPB_INFINITY) {
185     CHKLENGTH(length >= strlen(inf));
186     strcpy(buf, inf);
187     return strlen(inf);
188   } else if (val == -UPB_INFINITY) {
189     CHKLENGTH(length >= strlen(neginf));
190     strcpy(buf, neginf);
191     return strlen(neginf);
192   } else {
193     size_t n = _upb_snprintf(buf, length, "%.17g", val);
194     CHKLENGTH(n > 0 && n < length);
195     return n;
196   }
197 }
198
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);
202   return n;
203 }
204
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);
208   return n;
209 }
210
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);
214   return n;
215 }
216
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);
221   return n;
222 }
223
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);
227   return n;
228 }
229
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);
234   return n;
235 }
236
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;
242   print_comma(p);
243   print_data(p, "\"", 1);
244   putstring(p, key->ptr, key->len);
245   print_data(p, "\":", 2);
246   return true;
247 }
248
249 #define CHKFMT(val) if ((val) == (size_t)-1) return false;
250 #define CHK(val)    if (!(val)) return false;
251
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;                                           \
255     char data[64];                                                           \
256     size_t length = fmt_func(val, data, sizeof(data));                       \
257     UPB_UNUSED(handler_data);                                                \
258     CHKFMT(length);                                                          \
259     print_data(p, data, length);                                             \
260     return true;                                                             \
261   }                                                                          \
262   static bool scalar_##type(void *closure, const void *handler_data,         \
263                             type val) {                                      \
264     CHK(putkey(closure, handler_data));                                      \
265     CHK(put##type(closure, handler_data, val));                              \
266     return true;                                                             \
267   }                                                                          \
268   static bool repeated_##type(void *closure, const void *handler_data,       \
269                               type val) {                                    \
270     upb_json_printer *p = closure;                                           \
271     print_comma(p);                                                          \
272     CHK(put##type(closure, handler_data, val));                              \
273     return true;                                                             \
274   }
275
276 #define TYPE_HANDLERS_MAPKEY(type, fmt_func)                                 \
277   static bool putmapkey_##type(void *closure, const void *handler_data,      \
278                             type val) {                                      \
279     upb_json_printer *p = closure;                                           \
280     char data[64];                                                           \
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);                                                 \
286     return true;                                                             \
287   }
288
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)
296
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)
303
304 #undef TYPE_HANDLERS
305 #undef TYPE_HANDLERS_MAPKEY
306
307 typedef struct {
308   void *keyname;
309   const upb_enumdef *enumdef;
310 } EnumHandlerData;
311
312 static bool scalar_enum(void *closure, const void *handler_data,
313                         int32_t val) {
314   const EnumHandlerData *hd = handler_data;
315   upb_json_printer *p = closure;
316   const char *symbolic_name;
317
318   CHK(putkey(closure, hd->keyname));
319
320   symbolic_name = upb_enumdef_iton(hd->enumdef, val);
321   if (symbolic_name) {
322     print_data(p, "\"", 1);
323     putstring(p, symbolic_name, strlen(symbolic_name));
324     print_data(p, "\"", 1);
325   } else {
326     putint32_t(closure, NULL, val);
327   }
328
329   return true;
330 }
331
332 static void print_enum_symbolic_name(upb_json_printer *p,
333                                      const upb_enumdef *def,
334                                      int32_t val) {
335   const char *symbolic_name = upb_enumdef_iton(def, val);
336   if (symbolic_name) {
337     print_data(p, "\"", 1);
338     putstring(p, symbolic_name, strlen(symbolic_name));
339     print_data(p, "\"", 1);
340   } else {
341     putint32_t(p, NULL, val);
342   }
343 }
344
345 static bool repeated_enum(void *closure, const void *handler_data,
346                           int32_t val) {
347   const EnumHandlerData *hd = handler_data;
348   upb_json_printer *p = closure;
349   print_comma(p);
350
351   print_enum_symbolic_name(p, hd->enumdef, val);
352
353   return true;
354 }
355
356 static bool mapvalue_enum(void *closure, const void *handler_data,
357                           int32_t val) {
358   const EnumHandlerData *hd = handler_data;
359   upb_json_printer *p = closure;
360
361   print_enum_symbolic_name(p, hd->enumdef, val);
362
363   return true;
364 }
365
366 static void *scalar_startsubmsg(void *closure, const void *handler_data) {
367   return putkey(closure, handler_data) ? closure : UPB_BREAK;
368 }
369
370 static void *repeated_startsubmsg(void *closure, const void *handler_data) {
371   upb_json_printer *p = closure;
372   UPB_UNUSED(handler_data);
373   print_comma(p);
374   return closure;
375 }
376
377 static void start_frame(upb_json_printer *p) {
378   p->depth_++;
379   p->first_elem_[p->depth_] = true;
380   print_data(p, "{", 1);
381 }
382
383 static void end_frame(upb_json_printer *p) {
384   print_data(p, "}", 1);
385   p->depth_--;
386 }
387
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_);
393   }
394   start_frame(p);
395   return true;
396 }
397
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);
401   UPB_UNUSED(s);
402   end_frame(p);
403   if (p->depth_ == 0) {
404     upb_bytessink_end(p->output_);
405   }
406   return true;
407 }
408
409 static void *startseq(void *closure, const void *handler_data) {
410   upb_json_printer *p = closure;
411   CHK(putkey(closure, handler_data));
412   p->depth_++;
413   p->first_elem_[p->depth_] = true;
414   print_data(p, "[", 1);
415   return closure;
416 }
417
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);
422   p->depth_--;
423   return true;
424 }
425
426 static void *startmap(void *closure, const void *handler_data) {
427   upb_json_printer *p = closure;
428   CHK(putkey(closure, handler_data));
429   p->depth_++;
430   p->first_elem_[p->depth_] = true;
431   print_data(p, "{", 1);
432   return closure;
433 }
434
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);
439   p->depth_--;
440   return true;
441 }
442
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);
447   UPB_UNUSED(handle);
448   putstring(p, str, len);
449   return len;
450 }
451
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;
456
457   /* This is the regular base64, not the "web-safe" version. */
458   static const char base64[] =
459       "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
460
461   /* Base64-encode. */
462   char data[16000];
463   const char *limit = data + sizeof(data);
464   const unsigned char *from = (const unsigned char*)str;
465   char *to = data;
466   size_t remaining = len;
467   size_t bytes;
468
469   UPB_UNUSED(handler_data);
470   UPB_UNUSED(handle);
471
472   print_data(p, "\"", 1);
473
474   while (remaining > 2) {
475     if (limit - to < 4) {
476       bytes = to - data;
477       putstring(p, data, bytes);
478       to = data;
479     }
480
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];
485
486     remaining -= 3;
487     to += 4;
488     from += 3;
489   }
490
491   switch (remaining) {
492     case 2:
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];
496       to[3] = '=';
497       to += 4;
498       from += 2;
499       break;
500     case 1:
501       to[0] = base64[from[0] >> 2];
502       to[1] = base64[((from[0] & 0x3) << 4)];
503       to[2] = '=';
504       to[3] = '=';
505       to += 4;
506       from += 1;
507       break;
508   }
509
510   bytes = to - data;
511   putstring(p, data, bytes);
512   print_data(p, "\"", 1);
513   return len;
514 }
515
516 static void *scalar_startstr(void *closure, const void *handler_data,
517                              size_t size_hint) {
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);
523   return p;
524 }
525
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));
530   return len;
531 }
532
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);
537   return true;
538 }
539
540 static void *repeated_startstr(void *closure, const void *handler_data,
541                                size_t size_hint) {
542   upb_json_printer *p = closure;
543   UPB_UNUSED(handler_data);
544   UPB_UNUSED(size_hint);
545   print_comma(p);
546   print_data(p, "\"", 1);
547   return p;
548 }
549
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));
554   return len;
555 }
556
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);
561   return true;
562 }
563
564 static void *mapkeyval_startstr(void *closure, const void *handler_data,
565                                 size_t size_hint) {
566   upb_json_printer *p = closure;
567   UPB_UNUSED(handler_data);
568   UPB_UNUSED(size_hint);
569   print_data(p, "\"", 1);
570   return p;
571 }
572
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));
577   return len;
578 }
579
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);
584   return true;
585 }
586
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);
591   return true;
592 }
593
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));
599   return len;
600 }
601
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;
606   print_comma(p);
607   CHK(putbytes(closure, handler_data, str, len, handle));
608   return len;
609 }
610
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);
617   return len;
618 }
619
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;
629 }
630
631 /* Set up handlers for a mapentry submessage (i.e., an individual key/value pair
632  * in a map).
633  *
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
642  * moment. */
643 void printer_sethandlers_mapentry(const void *closure, bool preserve_fieldnames,
644                                   upb_handlers *h) {
645   const upb_msgdef *md = upb_handlers_msgdef(h);
646
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);
652
653   upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
654
655   UPB_UNUSED(closure);
656
657   switch (upb_fielddef_type(key_field)) {
658     case UPB_TYPE_INT32:
659       upb_handlers_setint32(h, key_field, putmapkey_int32_t, &empty_attr);
660       break;
661     case UPB_TYPE_INT64:
662       upb_handlers_setint64(h, key_field, putmapkey_int64_t, &empty_attr);
663       break;
664     case UPB_TYPE_UINT32:
665       upb_handlers_setuint32(h, key_field, putmapkey_uint32_t, &empty_attr);
666       break;
667     case UPB_TYPE_UINT64:
668       upb_handlers_setuint64(h, key_field, putmapkey_uint64_t, &empty_attr);
669       break;
670     case UPB_TYPE_BOOL:
671       upb_handlers_setbool(h, key_field, putmapkey_bool, &empty_attr);
672       break;
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);
677       break;
678     case UPB_TYPE_BYTES:
679       upb_handlers_setstring(h, key_field, mapkey_bytes, &empty_attr);
680       break;
681     default:
682       UPB_ASSERT(false);
683       break;
684   }
685
686   switch (upb_fielddef_type(value_field)) {
687     case UPB_TYPE_INT32:
688       upb_handlers_setint32(h, value_field, putint32_t, &empty_attr);
689       break;
690     case UPB_TYPE_INT64:
691       upb_handlers_setint64(h, value_field, putint64_t, &empty_attr);
692       break;
693     case UPB_TYPE_UINT32:
694       upb_handlers_setuint32(h, value_field, putuint32_t, &empty_attr);
695       break;
696     case UPB_TYPE_UINT64:
697       upb_handlers_setuint64(h, value_field, putuint64_t, &empty_attr);
698       break;
699     case UPB_TYPE_BOOL:
700       upb_handlers_setbool(h, value_field, putbool, &empty_attr);
701       break;
702     case UPB_TYPE_FLOAT:
703       upb_handlers_setfloat(h, value_field, putfloat, &empty_attr);
704       break;
705     case UPB_TYPE_DOUBLE:
706       upb_handlers_setdouble(h, value_field, putdouble, &empty_attr);
707       break;
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);
712       break;
713     case UPB_TYPE_BYTES:
714       upb_handlers_setstring(h, value_field, putbytes, &empty_attr);
715       break;
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);
720       break;
721     }
722     case UPB_TYPE_MESSAGE:
723       /* No handler necessary -- the submsg handlers will print the message
724        * as appropriate. */
725       break;
726   }
727 }
728
729 static bool putseconds(void *closure, const void *handler_data,
730                        int64_t seconds) {
731   upb_json_printer *p = closure;
732   p->seconds = seconds;
733   UPB_UNUSED(handler_data);
734   return true;
735 }
736
737 static bool putnanos(void *closure, const void *handler_data,
738                      int32_t nanos) {
739   upb_json_printer *p = closure;
740   p->nanos = nanos;
741   UPB_UNUSED(handler_data);
742   return true;
743 }
744
745 static void *scalar_startstr_nokey(void *closure, const void *handler_data,
746                                    size_t size_hint) {
747   upb_json_printer *p = closure;
748   UPB_UNUSED(handler_data);
749   UPB_UNUSED(size_hint);
750   print_data(p, "\"", 1);
751   return p;
752 }
753
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);
759   UPB_UNUSED(handle);
760   print_data(p, "\"", 1);
761   putstring(p, str, len);
762   print_data(p, "\"", 1);
763   return len + 2;
764 }
765
766 static void *startseq_nokey(void *closure, const void *handler_data) {
767   upb_json_printer *p = closure;
768   UPB_UNUSED(handler_data);
769   p->depth_++;
770   p->first_elem_[p->depth_] = true;
771   print_data(p, "[", 1);
772   return closure;
773 }
774
775 static void *startseq_fieldmask(void *closure, const void *handler_data) {
776   upb_json_printer *p = closure;
777   UPB_UNUSED(handler_data);
778   p->depth_++;
779   p->first_elem_[p->depth_] = true;
780   return closure;
781 }
782
783 static bool endseq_fieldmask(void *closure, const void *handler_data) {
784   upb_json_printer *p = closure;
785   UPB_UNUSED(handler_data);
786   p->depth_--;
787   return true;
788 }
789
790 static void *repeated_startstr_fieldmask(
791     void *closure, const void *handler_data,
792     size_t size_hint) {
793   upb_json_printer *p = closure;
794   UPB_UNUSED(handler_data);
795   UPB_UNUSED(size_hint);
796   print_comma(p);
797   return p;
798 }
799
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;
805   bool upper = false;
806   size_t result_len = 0;
807   for (; str < limit; str++) {
808     if (*str == '_') {
809       upper = true;
810       continue;
811     }
812     if (upper && *str >= 'a' && *str <= 'z') {
813       char upper_char = toupper(*str);
814       CHK(putstr(closure, handler_data, &upper_char, 1, handle));
815     } else {
816       CHK(putstr(closure, handler_data, str, 1, handle));
817     }
818     upper = false;
819     result_len++;
820   }
821   return result_len;
822 }
823
824 static void *startmap_nokey(void *closure, const void *handler_data) {
825   upb_json_printer *p = closure;
826   UPB_UNUSED(handler_data);
827   p->depth_++;
828   p->first_elem_[p->depth_] = true;
829   print_data(p, "{", 1);
830   return closure;
831 }
832
833 static bool putnull(void *closure, const void *handler_data,
834                     int32_t null) {
835   upb_json_printer *p = closure;
836   print_data(p, "null", 4);
837   UPB_UNUSED(handler_data);
838   UPB_UNUSED(null);
839   return true;
840 }
841
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_);
847   }
848   return true;
849 }
850
851 #define UPB_DURATION_MAX_JSON_LEN 23
852 #define UPB_DURATION_MAX_NANO_LEN 9
853
854 static bool printer_enddurationmsg(void *closure, const void *handler_data,
855                                    upb_status *s) {
856   upb_json_printer *p = closure;
857   char buffer[UPB_DURATION_MAX_JSON_LEN];
858   size_t base_len;
859   size_t curr;
860   size_t i;
861
862   memset(buffer, 0, UPB_DURATION_MAX_JSON_LEN);
863
864   if (p->seconds < -315576000000) {
865     upb_status_seterrf(s, "error parsing duration: "
866                           "minimum acceptable value is "
867                           "-315576000000");
868     return false;
869   }
870
871   if (p->seconds > 315576000000) {
872     upb_status_seterrf(s, "error serializing duration: "
873                           "maximum acceptable value is "
874                           "315576000000");
875     return false;
876   }
877
878   _upb_snprintf(buffer, sizeof(buffer), "%ld", (long)p->seconds);
879   base_len = strlen(buffer);
880
881   if (p->nanos != 0) {
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--) {
888       nanos_buffer[i] = 0;
889     }
890     strcpy(buffer + base_len, nanos_buffer + 1);
891   }
892
893   curr = strlen(buffer);
894   strcpy(buffer + curr, "s");
895
896   p->seconds = 0;
897   p->nanos = 0;
898
899   print_data(p, "\"", 1);
900   print_data(p, buffer, strlen(buffer));
901   print_data(p, "\"", 1);
902
903   if (p->depth_ == 0) {
904     upb_bytessink_end(p->output_);
905   }
906
907   UPB_UNUSED(handler_data);
908   return true;
909 }
910
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_);
916   }
917   return true;
918 }
919
920 #define UPB_TIMESTAMP_MAX_JSON_LEN 31
921 #define UPB_TIMESTAMP_BEFORE_NANO_LEN 19
922 #define UPB_TIMESTAMP_MAX_NANO_LEN 9
923
924 static bool printer_endtimestampmsg(void *closure, const void *handler_data,
925                                     upb_status *s) {
926   upb_json_printer *p = closure;
927   char buffer[UPB_TIMESTAMP_MAX_JSON_LEN];
928   time_t time = p->seconds;
929   size_t curr;
930   size_t i;
931   size_t year_length =
932       strftime(buffer, UPB_TIMESTAMP_MAX_JSON_LEN, "%Y", gmtime(&time));
933
934   if (p->seconds < -62135596800) {
935     upb_status_seterrf(s, "error parsing timestamp: "
936                           "minimum acceptable value is "
937                           "0001-01-01T00:00:00Z");
938     return false;
939   }
940
941   if (p->seconds > 253402300799) {
942     upb_status_seterrf(s, "error parsing timestamp: "
943                           "maximum acceptable value is "
944                           "9999-12-31T23:59:59Z");
945     return false;
946   }
947
948   /* strftime doesn't guarantee 4 digits for year. Prepend 0 by ourselves. */
949   for (i = 0; i < 4 - year_length; i++) {
950     buffer[i] = '0';
951   }
952
953   strftime(buffer + (4 - year_length), UPB_TIMESTAMP_MAX_JSON_LEN,
954            "%Y-%m-%dT%H:%M:%S", gmtime(&time));
955   if (p->nanos != 0) {
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--) {
962       nanos_buffer[i] = 0;
963     }
964     strcpy(buffer + UPB_TIMESTAMP_BEFORE_NANO_LEN, nanos_buffer + 1);
965   }
966
967   curr = strlen(buffer);
968   strcpy(buffer + curr, "Z");
969
970   p->seconds = 0;
971   p->nanos = 0;
972
973   print_data(p, "\"", 1);
974   print_data(p, buffer, strlen(buffer));
975   print_data(p, "\"", 1);
976
977   if (p->depth_ == 0) {
978     upb_bytessink_end(p->output_);
979   }
980
981   UPB_UNUSED(handler_data);
982   UPB_UNUSED(s);
983   return true;
984 }
985
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_);
991   }
992   return true;
993 }
994
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);
999   UPB_UNUSED(s);
1000   if (p->depth_ == 0) {
1001     upb_bytessink_end(p->output_);
1002   }
1003   return true;
1004 }
1005
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_);
1012   }
1013   print_data(p, "\"", 1);
1014   return true;
1015 }
1016
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);
1021   UPB_UNUSED(s);
1022   print_data(p, "\"", 1);
1023   if (p->depth_ == 0) {
1024     upb_bytessink_end(p->output_);
1025   }
1026   return true;
1027 }
1028
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));
1034   return p;
1035 }
1036
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);
1040
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);
1043
1044   upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
1045
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");
1051
1052   type_name_attr.handler_data = type_url_json_name;
1053   value_name_attr.handler_data = value_json_name;
1054
1055   /* Set up handlers. */
1056   upb_handlers_setstartmsg(h, printer_startmsg, &empty_attr);
1057   upb_handlers_setendmsg(h, printer_endmsg, &empty_attr);
1058
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);
1062
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.
1065    */
1066   upb_handlers_setstartstr(h, value_field, scalar_startstr_onlykey,
1067                            &value_name_attr);
1068
1069   UPB_UNUSED(closure);
1070 }
1071
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);
1076
1077   upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
1078
1079   upb_handlers_setstartseq(h, f, startseq_fieldmask, &empty_attr);
1080   upb_handlers_setendseq(h, f, endseq_fieldmask, &empty_attr);
1081
1082   upb_handlers_setstartmsg(h, printer_startmsg_fieldmask, &empty_attr);
1083   upb_handlers_setendmsg(h, printer_endmsg_fieldmask, &empty_attr);
1084
1085   upb_handlers_setstartstr(h, f, repeated_startstr_fieldmask, &empty_attr);
1086   upb_handlers_setstring(h, f, repeated_str_fieldmask, &empty_attr);
1087
1088   UPB_UNUSED(closure);
1089 }
1090
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);
1094
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);
1099
1100   upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
1101
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);
1106
1107   UPB_UNUSED(closure);
1108 }
1109
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);
1114
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);
1119
1120   upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
1121
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);
1126
1127   UPB_UNUSED(closure);
1128 }
1129
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;
1133
1134   upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
1135
1136   upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr);
1137   upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr);
1138
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);
1142
1143     switch (upb_fielddef_type(f)) {
1144       case UPB_TYPE_ENUM:
1145         upb_handlers_setint32(h, f, putnull, &empty_attr);
1146         break;
1147       case UPB_TYPE_DOUBLE:
1148         upb_handlers_setdouble(h, f, putdouble, &empty_attr);
1149         break;
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);
1154         break;
1155       case UPB_TYPE_BOOL:
1156         upb_handlers_setbool(h, f, putbool, &empty_attr);
1157         break;
1158       case UPB_TYPE_MESSAGE:
1159         break;
1160       default:
1161         UPB_ASSERT(false);
1162         break;
1163     }
1164   }
1165
1166   UPB_UNUSED(closure);
1167 }
1168
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);                                                     \
1178 }
1179
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)
1189
1190 #undef WRAPPER_SETHANDLERS
1191
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);
1195
1196   upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
1197
1198   upb_handlers_setstartseq(h, f, startseq_nokey, &empty_attr);
1199   upb_handlers_setendseq(h, f, endseq, &empty_attr);
1200
1201   upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr);
1202   upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr);
1203
1204   upb_handlers_setstartsubmsg(h, f, repeated_startsubmsg, &empty_attr);
1205
1206   UPB_UNUSED(closure);
1207 }
1208
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);
1212
1213   upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
1214
1215   upb_handlers_setstartseq(h, f, startmap_nokey, &empty_attr);
1216   upb_handlers_setendseq(h, f, endmap, &empty_attr);
1217
1218   upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr);
1219   upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr);
1220
1221   upb_handlers_setstartsubmsg(h, f, repeated_startsubmsg, &empty_attr);
1222
1223   UPB_UNUSED(closure);
1224 }
1225
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;
1233
1234   if (is_mapentry) {
1235     /* mapentry messages are sufficiently different that we handle them
1236      * separately. */
1237     printer_sethandlers_mapentry(closure, preserve_fieldnames, h);
1238     return;
1239   }
1240
1241   switch (upb_msgdef_wellknowntype(md)) {
1242     case UPB_WELLKNOWN_UNSPECIFIED:
1243       break;
1244     case UPB_WELLKNOWN_ANY:
1245       printer_sethandlers_any(closure, h);
1246       return;
1247     case UPB_WELLKNOWN_FIELDMASK:
1248       printer_sethandlers_fieldmask(closure, h);
1249       return;
1250     case UPB_WELLKNOWN_DURATION:
1251       printer_sethandlers_duration(closure, h);
1252       return;
1253     case UPB_WELLKNOWN_TIMESTAMP:
1254       printer_sethandlers_timestamp(closure, h);
1255       return;
1256     case UPB_WELLKNOWN_VALUE:
1257       printer_sethandlers_value(closure, h);
1258       return;
1259     case UPB_WELLKNOWN_LISTVALUE:
1260       printer_sethandlers_listvalue(closure, h);
1261       return;
1262     case UPB_WELLKNOWN_STRUCT:
1263       printer_sethandlers_structvalue(closure, h);
1264       return;
1265 #define WRAPPER(wellknowntype, name)        \
1266   case wellknowntype:                       \
1267     printer_sethandlers_##name(closure, h); \
1268     return;                                 \
1269
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);
1279
1280 #undef WRAPPER
1281   }
1282
1283   upb_handlers_setstartmsg(h, printer_startmsg, &empty_attr);
1284   upb_handlers_setendmsg(h, printer_endmsg, &empty_attr);
1285
1286 #define TYPE(type, name, ctype)                                               \
1287   case type:                                                                  \
1288     if (upb_fielddef_isseq(f)) {                                              \
1289       upb_handlers_set##name(h, f, repeated_##ctype, &empty_attr);            \
1290     } else {                                                                  \
1291       upb_handlers_set##name(h, f, scalar_##ctype, &name_attr);               \
1292     }                                                                         \
1293     break;
1294
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);
1298
1299     upb_handlerattr name_attr = UPB_HANDLERATTR_INIT;
1300     name_attr.handler_data = newstrpc(h, f, preserve_fieldnames);
1301
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);
1308     }
1309
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
1321          * need first. */
1322         upb_handlerattr enum_attr = UPB_HANDLERATTR_INIT;
1323         set_enum_hd(h, f, preserve_fieldnames, &enum_attr);
1324
1325         if (upb_fielddef_isseq(f)) {
1326           upb_handlers_setint32(h, f, repeated_enum, &enum_attr);
1327         } else {
1328           upb_handlers_setint32(h, f, scalar_enum, &enum_attr);
1329         }
1330
1331         break;
1332       }
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);
1338         } else {
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);
1342         }
1343         break;
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);
1349         } else {
1350           upb_handlers_setstring(h, f, scalar_bytes, &name_attr);
1351         }
1352         break;
1353       case UPB_TYPE_MESSAGE:
1354         if (upb_fielddef_isseq(f)) {
1355           upb_handlers_setstartsubmsg(h, f, repeated_startsubmsg, &name_attr);
1356         } else {
1357           upb_handlers_setstartsubmsg(h, f, scalar_startsubmsg, &name_attr);
1358         }
1359         break;
1360     }
1361   }
1362
1363 #undef TYPE
1364 }
1365
1366 static void json_printer_reset(upb_json_printer *p) {
1367   p->depth_ = 0;
1368 }
1369
1370
1371 /* Public API *****************************************************************/
1372
1373 upb_json_printer *upb_json_printer_create(upb_arena *a, const upb_handlers *h,
1374                                           upb_bytessink output) {
1375 #ifndef NDEBUG
1376   size_t size_before = upb_arena_bytesallocated(a);
1377 #endif
1378
1379   upb_json_printer *p = upb_arena_malloc(a, sizeof(upb_json_printer));
1380   if (!p) return NULL;
1381
1382   p->output_ = output;
1383   json_printer_reset(p);
1384   upb_sink_reset(&p->input_, h, p);
1385   p->seconds = 0;
1386   p->nanos = 0;
1387
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);
1391   return p;
1392 }
1393
1394 upb_sink upb_json_printer_input(upb_json_printer *p) {
1395   return p->input_;
1396 }
1397
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);
1401
1402   cache->preserve_fieldnames = preserve_proto_fieldnames;
1403   upb_handlercache_addcleanup(ret, cache, upb_gfree);
1404
1405   return ret;
1406 }