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 <inttypes.h>
10 #include <math.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <time.h>
15
16 #include "upb/port_def.inc"
17
18 struct upb_json_printer {
19 upb_sink input_;
20 /* BytesSink closure. */
21 void *subc_;
22 upb_bytessink output_;
23
24 /* We track the depth so that we know when to emit startstr/endstr on the
25 * output. */
26 int depth_;
27
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
30 * frame depth.
31 *
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];
37
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. */
41 int64_t seconds;
42 int32_t nanos;
43 };
44
45 /* StringPiece; a pointer plus a length. */
46 typedef struct {
47 char *ptr;
48 size_t len;
49 } strpc;
50
freestrpc(void * ptr)51 void freestrpc(void *ptr) {
52 strpc *pc = ptr;
53 upb_gfree(pc->ptr);
54 upb_gfree(pc);
55 }
56
57 typedef struct {
58 bool preserve_fieldnames;
59 } upb_json_printercache;
60
61 /* Convert fielddef name to JSON name and return as a string piece. */
newstrpc(upb_handlers * h,const upb_fielddef * f,bool preserve_fieldnames)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);
69 } else {
70 ret->ptr = upb_gstrdup(upb_fielddef_jsonname(f));
71 ret->len = strlen(ret->ptr);
72 }
73
74 upb_handlers_addcleanup(h, ret, freestrpc);
75 return ret;
76 }
77
78 /* Convert a null-terminated const char* to a string piece. */
newstrpc_str(upb_handlers * h,const char * str)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);
84 return ret;
85 }
86
87 /* ------------ JSON string printing: values, maps, arrays ------------------ */
88
print_data(upb_json_printer * p,const char * buf,size_t len)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);
93 UPB_ASSERT(n == len);
94 }
95
print_comma(upb_json_printer * p)96 static void print_comma(upb_json_printer *p) {
97 if (!p->first_elem_[p->depth_]) {
98 print_data(p, ",", 1);
99 }
100 p->first_elem_[p->depth_] = false;
101 }
102
103 /* Helpers that print properly formatted elements to the JSON output stream. */
104
105 /* Used for escaping control chars in strings. */
106 static const char kControlCharLimit = 0x20;
107
is_json_escaped(char c)108 UPB_INLINE bool is_json_escaped(char c) {
109 /* See RFC 4627. */
110 unsigned char uc = (unsigned char)c;
111 return uc < kControlCharLimit || uc == '"' || uc == '\\';
112 }
113
json_nice_escape(char c)114 UPB_INLINE const char* json_nice_escape(char c) {
115 switch (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;
124 }
125 }
126
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. */
putstring(upb_json_printer * p,const char * buf,size_t len)130 static void putstring(upb_json_printer *p, const char *buf, size_t len) {
131 const char* unescaped_run = NULL;
132 unsigned int i;
133 for (i = 0; i < len; i++) {
134 char c = buf[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
140 * escape. */
141 char escape_buf[8];
142 if (!escape) {
143 unsigned char byte = (unsigned char)c;
144 snprintf(escape_buf, sizeof(escape_buf), "\\u%04x", (int)byte);
145 escape = escape_buf;
146 }
147
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. */
151
152 /* If there's a current run of unescaped chars, print that run first. */
153 if (unescaped_run) {
154 print_data(p, unescaped_run, &buf[i] - unescaped_run);
155 unescaped_run = NULL;
156 }
157 /* Then print the escape code. */
158 print_data(p, escape, strlen(escape));
159 } else {
160 /* Add to the current unescaped run of characters. */
161 if (unescaped_run == NULL) {
162 unescaped_run = &buf[i];
163 }
164 }
165 }
166
167 /* If the string ended in a run of unescaped characters, print that last run. */
168 if (unescaped_run) {
169 print_data(p, unescaped_run, &buf[len] - unescaped_run);
170 }
171 }
172
173 #define CHKLENGTH(x) if (!(x)) return -1;
174
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. */
178
179 const char neginf[] = "\"-Infinity\"";
180 const char inf[] = "\"Infinity\"";
181
fmt_double(double val,char * buf,size_t length)182 static size_t fmt_double(double val, char* buf, size_t length) {
183 if (val == INFINITY) {
184 CHKLENGTH(length >= strlen(inf));
185 strcpy(buf, inf);
186 return strlen(inf);
187 } else if (val == -INFINITY) {
188 CHKLENGTH(length >= strlen(neginf));
189 strcpy(buf, neginf);
190 return strlen(neginf);
191 } else {
192 size_t n = snprintf(buf, length, "%.17g", val);
193 CHKLENGTH(n > 0 && n < length);
194 return n;
195 }
196 }
197
fmt_float(float val,char * buf,size_t 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);
201 return n;
202 }
203
fmt_bool(bool val,char * buf,size_t 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);
207 return n;
208 }
209
fmt_int64_as_number(int64_t val,char * buf,size_t 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);
213 return n;
214 }
215
fmt_uint64_as_number(uint64_t val,char * buf,size_t 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);
219 return n;
220 }
221
fmt_int64_as_string(int64_t val,char * buf,size_t 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);
225 return n;
226 }
227
fmt_uint64_as_string(uint64_t val,char * buf,size_t 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);
231 return n;
232 }
233
234 /* Print a map key given a field name. Called by scalar field handlers and by
235 * startseq for repeated fields. */
putkey(void * closure,const void * handler_data)236 static bool putkey(void *closure, const void *handler_data) {
237 upb_json_printer *p = closure;
238 const strpc *key = handler_data;
239 print_comma(p);
240 print_data(p, "\"", 1);
241 putstring(p, key->ptr, key->len);
242 print_data(p, "\":", 2);
243 return true;
244 }
245
246 #define CHKFMT(val) if ((val) == (size_t)-1) return false;
247 #define CHK(val) if (!(val)) return false;
248
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; \
252 char data[64]; \
253 size_t length = fmt_func(val, data, sizeof(data)); \
254 UPB_UNUSED(handler_data); \
255 CHKFMT(length); \
256 print_data(p, data, length); \
257 return true; \
258 } \
259 static bool scalar_##type(void *closure, const void *handler_data, \
260 type val) { \
261 CHK(putkey(closure, handler_data)); \
262 CHK(put##type(closure, handler_data, val)); \
263 return true; \
264 } \
265 static bool repeated_##type(void *closure, const void *handler_data, \
266 type val) { \
267 upb_json_printer *p = closure; \
268 print_comma(p); \
269 CHK(put##type(closure, handler_data, val)); \
270 return true; \
271 }
272
273 #define TYPE_HANDLERS_MAPKEY(type, fmt_func) \
274 static bool putmapkey_##type(void *closure, const void *handler_data, \
275 type val) { \
276 upb_json_printer *p = closure; \
277 char data[64]; \
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); \
283 return true; \
284 }
285
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)
293
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)
300
301 #undef TYPE_HANDLERS
302 #undef TYPE_HANDLERS_MAPKEY
303
304 typedef struct {
305 void *keyname;
306 const upb_enumdef *enumdef;
307 } EnumHandlerData;
308
scalar_enum(void * closure,const void * handler_data,int32_t val)309 static bool scalar_enum(void *closure, const void *handler_data,
310 int32_t val) {
311 const EnumHandlerData *hd = handler_data;
312 upb_json_printer *p = closure;
313 const char *symbolic_name;
314
315 CHK(putkey(closure, hd->keyname));
316
317 symbolic_name = upb_enumdef_iton(hd->enumdef, val);
318 if (symbolic_name) {
319 print_data(p, "\"", 1);
320 putstring(p, symbolic_name, strlen(symbolic_name));
321 print_data(p, "\"", 1);
322 } else {
323 putint32_t(closure, NULL, val);
324 }
325
326 return true;
327 }
328
print_enum_symbolic_name(upb_json_printer * p,const upb_enumdef * def,int32_t val)329 static void print_enum_symbolic_name(upb_json_printer *p,
330 const upb_enumdef *def,
331 int32_t val) {
332 const char *symbolic_name = upb_enumdef_iton(def, val);
333 if (symbolic_name) {
334 print_data(p, "\"", 1);
335 putstring(p, symbolic_name, strlen(symbolic_name));
336 print_data(p, "\"", 1);
337 } else {
338 putint32_t(p, NULL, val);
339 }
340 }
341
repeated_enum(void * closure,const void * handler_data,int32_t val)342 static bool repeated_enum(void *closure, const void *handler_data,
343 int32_t val) {
344 const EnumHandlerData *hd = handler_data;
345 upb_json_printer *p = closure;
346 print_comma(p);
347
348 print_enum_symbolic_name(p, hd->enumdef, val);
349
350 return true;
351 }
352
mapvalue_enum(void * closure,const void * handler_data,int32_t val)353 static bool mapvalue_enum(void *closure, const void *handler_data,
354 int32_t val) {
355 const EnumHandlerData *hd = handler_data;
356 upb_json_printer *p = closure;
357
358 print_enum_symbolic_name(p, hd->enumdef, val);
359
360 return true;
361 }
362
scalar_startsubmsg(void * closure,const void * handler_data)363 static void *scalar_startsubmsg(void *closure, const void *handler_data) {
364 return putkey(closure, handler_data) ? closure : UPB_BREAK;
365 }
366
repeated_startsubmsg(void * closure,const void * handler_data)367 static void *repeated_startsubmsg(void *closure, const void *handler_data) {
368 upb_json_printer *p = closure;
369 UPB_UNUSED(handler_data);
370 print_comma(p);
371 return closure;
372 }
373
start_frame(upb_json_printer * p)374 static void start_frame(upb_json_printer *p) {
375 p->depth_++;
376 p->first_elem_[p->depth_] = true;
377 print_data(p, "{", 1);
378 }
379
end_frame(upb_json_printer * p)380 static void end_frame(upb_json_printer *p) {
381 print_data(p, "}", 1);
382 p->depth_--;
383 }
384
printer_startmsg(void * closure,const void * handler_data)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_);
390 }
391 start_frame(p);
392 return true;
393 }
394
printer_endmsg(void * closure,const void * handler_data,upb_status * s)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);
398 UPB_UNUSED(s);
399 end_frame(p);
400 if (p->depth_ == 0) {
401 upb_bytessink_end(p->output_);
402 }
403 return true;
404 }
405
startseq(void * closure,const void * handler_data)406 static void *startseq(void *closure, const void *handler_data) {
407 upb_json_printer *p = closure;
408 CHK(putkey(closure, handler_data));
409 p->depth_++;
410 p->first_elem_[p->depth_] = true;
411 print_data(p, "[", 1);
412 return closure;
413 }
414
endseq(void * closure,const void * handler_data)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);
419 p->depth_--;
420 return true;
421 }
422
startmap(void * closure,const void * handler_data)423 static void *startmap(void *closure, const void *handler_data) {
424 upb_json_printer *p = closure;
425 CHK(putkey(closure, handler_data));
426 p->depth_++;
427 p->first_elem_[p->depth_] = true;
428 print_data(p, "{", 1);
429 return closure;
430 }
431
endmap(void * closure,const void * handler_data)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);
436 p->depth_--;
437 return true;
438 }
439
putstr(void * closure,const void * handler_data,const char * str,size_t len,const upb_bufhandle * handle)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);
444 UPB_UNUSED(handle);
445 putstring(p, str, len);
446 return len;
447 }
448
449 /* This has to Base64 encode the bytes, because JSON has no "bytes" type. */
putbytes(void * closure,const void * handler_data,const char * str,size_t len,const upb_bufhandle * handle)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;
453
454 /* This is the regular base64, not the "web-safe" version. */
455 static const char base64[] =
456 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
457
458 /* Base64-encode. */
459 char data[16000];
460 const char *limit = data + sizeof(data);
461 const unsigned char *from = (const unsigned char*)str;
462 char *to = data;
463 size_t remaining = len;
464 size_t bytes;
465
466 UPB_UNUSED(handler_data);
467 UPB_UNUSED(handle);
468
469 print_data(p, "\"", 1);
470
471 while (remaining > 2) {
472 if (limit - to < 4) {
473 bytes = to - data;
474 putstring(p, data, bytes);
475 to = data;
476 }
477
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];
482
483 remaining -= 3;
484 to += 4;
485 from += 3;
486 }
487
488 switch (remaining) {
489 case 2:
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];
493 to[3] = '=';
494 to += 4;
495 from += 2;
496 break;
497 case 1:
498 to[0] = base64[from[0] >> 2];
499 to[1] = base64[((from[0] & 0x3) << 4)];
500 to[2] = '=';
501 to[3] = '=';
502 to += 4;
503 from += 1;
504 break;
505 }
506
507 bytes = to - data;
508 putstring(p, data, bytes);
509 print_data(p, "\"", 1);
510 return len;
511 }
512
scalar_startstr(void * closure,const void * handler_data,size_t size_hint)513 static void *scalar_startstr(void *closure, const void *handler_data,
514 size_t size_hint) {
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);
520 return p;
521 }
522
scalar_str(void * closure,const void * handler_data,const char * str,size_t len,const upb_bufhandle * handle)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));
527 return len;
528 }
529
scalar_endstr(void * closure,const void * handler_data)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);
534 return true;
535 }
536
repeated_startstr(void * closure,const void * handler_data,size_t size_hint)537 static void *repeated_startstr(void *closure, const void *handler_data,
538 size_t size_hint) {
539 upb_json_printer *p = closure;
540 UPB_UNUSED(handler_data);
541 UPB_UNUSED(size_hint);
542 print_comma(p);
543 print_data(p, "\"", 1);
544 return p;
545 }
546
repeated_str(void * closure,const void * handler_data,const char * str,size_t len,const upb_bufhandle * handle)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));
551 return len;
552 }
553
repeated_endstr(void * closure,const void * handler_data)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);
558 return true;
559 }
560
mapkeyval_startstr(void * closure,const void * handler_data,size_t size_hint)561 static void *mapkeyval_startstr(void *closure, const void *handler_data,
562 size_t size_hint) {
563 upb_json_printer *p = closure;
564 UPB_UNUSED(handler_data);
565 UPB_UNUSED(size_hint);
566 print_data(p, "\"", 1);
567 return p;
568 }
569
mapkey_str(void * closure,const void * handler_data,const char * str,size_t len,const upb_bufhandle * handle)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));
574 return len;
575 }
576
mapkey_endstr(void * closure,const void * handler_data)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);
581 return true;
582 }
583
mapvalue_endstr(void * closure,const void * handler_data)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);
588 return true;
589 }
590
scalar_bytes(void * closure,const void * handler_data,const char * str,size_t len,const upb_bufhandle * handle)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));
596 return len;
597 }
598
repeated_bytes(void * closure,const void * handler_data,const char * str,size_t len,const upb_bufhandle * 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;
603 print_comma(p);
604 CHK(putbytes(closure, handler_data, str, len, handle));
605 return len;
606 }
607
mapkey_bytes(void * closure,const void * handler_data,const char * str,size_t len,const upb_bufhandle * 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);
614 return len;
615 }
616
set_enum_hd(upb_handlers * h,const upb_fielddef * f,bool preserve_fieldnames,upb_handlerattr * attr)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;
626 }
627
628 /* Set up handlers for a mapentry submessage (i.e., an individual key/value pair
629 * in a map).
630 *
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
639 * moment. */
printer_sethandlers_mapentry(const void * closure,bool preserve_fieldnames,upb_handlers * h)640 void printer_sethandlers_mapentry(const void *closure, bool preserve_fieldnames,
641 upb_handlers *h) {
642 const upb_msgdef *md = upb_handlers_msgdef(h);
643
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);
649
650 upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
651
652 UPB_UNUSED(closure);
653
654 switch (upb_fielddef_type(key_field)) {
655 case UPB_TYPE_INT32:
656 upb_handlers_setint32(h, key_field, putmapkey_int32_t, &empty_attr);
657 break;
658 case UPB_TYPE_INT64:
659 upb_handlers_setint64(h, key_field, putmapkey_int64_t, &empty_attr);
660 break;
661 case UPB_TYPE_UINT32:
662 upb_handlers_setuint32(h, key_field, putmapkey_uint32_t, &empty_attr);
663 break;
664 case UPB_TYPE_UINT64:
665 upb_handlers_setuint64(h, key_field, putmapkey_uint64_t, &empty_attr);
666 break;
667 case UPB_TYPE_BOOL:
668 upb_handlers_setbool(h, key_field, putmapkey_bool, &empty_attr);
669 break;
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);
674 break;
675 case UPB_TYPE_BYTES:
676 upb_handlers_setstring(h, key_field, mapkey_bytes, &empty_attr);
677 break;
678 default:
679 UPB_ASSERT(false);
680 break;
681 }
682
683 switch (upb_fielddef_type(value_field)) {
684 case UPB_TYPE_INT32:
685 upb_handlers_setint32(h, value_field, putint32_t, &empty_attr);
686 break;
687 case UPB_TYPE_INT64:
688 upb_handlers_setint64(h, value_field, putint64_t, &empty_attr);
689 break;
690 case UPB_TYPE_UINT32:
691 upb_handlers_setuint32(h, value_field, putuint32_t, &empty_attr);
692 break;
693 case UPB_TYPE_UINT64:
694 upb_handlers_setuint64(h, value_field, putuint64_t, &empty_attr);
695 break;
696 case UPB_TYPE_BOOL:
697 upb_handlers_setbool(h, value_field, putbool, &empty_attr);
698 break;
699 case UPB_TYPE_FLOAT:
700 upb_handlers_setfloat(h, value_field, putfloat, &empty_attr);
701 break;
702 case UPB_TYPE_DOUBLE:
703 upb_handlers_setdouble(h, value_field, putdouble, &empty_attr);
704 break;
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);
709 break;
710 case UPB_TYPE_BYTES:
711 upb_handlers_setstring(h, value_field, putbytes, &empty_attr);
712 break;
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);
717 break;
718 }
719 case UPB_TYPE_MESSAGE:
720 /* No handler necessary -- the submsg handlers will print the message
721 * as appropriate. */
722 break;
723 }
724 }
725
putseconds(void * closure,const void * handler_data,int64_t seconds)726 static bool putseconds(void *closure, const void *handler_data,
727 int64_t seconds) {
728 upb_json_printer *p = closure;
729 p->seconds = seconds;
730 UPB_UNUSED(handler_data);
731 return true;
732 }
733
putnanos(void * closure,const void * handler_data,int32_t nanos)734 static bool putnanos(void *closure, const void *handler_data,
735 int32_t nanos) {
736 upb_json_printer *p = closure;
737 p->nanos = nanos;
738 UPB_UNUSED(handler_data);
739 return true;
740 }
741
scalar_startstr_nokey(void * closure,const void * handler_data,size_t size_hint)742 static void *scalar_startstr_nokey(void *closure, const void *handler_data,
743 size_t size_hint) {
744 upb_json_printer *p = closure;
745 UPB_UNUSED(handler_data);
746 UPB_UNUSED(size_hint);
747 print_data(p, "\"", 1);
748 return p;
749 }
750
putstr_nokey(void * closure,const void * handler_data,const char * str,size_t len,const upb_bufhandle * handle)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);
756 UPB_UNUSED(handle);
757 print_data(p, "\"", 1);
758 putstring(p, str, len);
759 print_data(p, "\"", 1);
760 return len + 2;
761 }
762
startseq_nokey(void * closure,const void * handler_data)763 static void *startseq_nokey(void *closure, const void *handler_data) {
764 upb_json_printer *p = closure;
765 UPB_UNUSED(handler_data);
766 p->depth_++;
767 p->first_elem_[p->depth_] = true;
768 print_data(p, "[", 1);
769 return closure;
770 }
771
startseq_fieldmask(void * closure,const void * handler_data)772 static void *startseq_fieldmask(void *closure, const void *handler_data) {
773 upb_json_printer *p = closure;
774 UPB_UNUSED(handler_data);
775 p->depth_++;
776 p->first_elem_[p->depth_] = true;
777 return closure;
778 }
779
endseq_fieldmask(void * closure,const void * handler_data)780 static bool endseq_fieldmask(void *closure, const void *handler_data) {
781 upb_json_printer *p = closure;
782 UPB_UNUSED(handler_data);
783 p->depth_--;
784 return true;
785 }
786
repeated_startstr_fieldmask(void * closure,const void * handler_data,size_t size_hint)787 static void *repeated_startstr_fieldmask(
788 void *closure, const void *handler_data,
789 size_t size_hint) {
790 upb_json_printer *p = closure;
791 UPB_UNUSED(handler_data);
792 UPB_UNUSED(size_hint);
793 print_comma(p);
794 return p;
795 }
796
repeated_str_fieldmask(void * closure,const void * handler_data,const char * str,size_t len,const upb_bufhandle * handle)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;
802 bool upper = false;
803 size_t result_len = 0;
804 for (; str < limit; str++) {
805 if (*str == '_') {
806 upper = true;
807 continue;
808 }
809 if (upper && *str >= 'a' && *str <= 'z') {
810 char upper_char = toupper(*str);
811 CHK(putstr(closure, handler_data, &upper_char, 1, handle));
812 } else {
813 CHK(putstr(closure, handler_data, str, 1, handle));
814 }
815 upper = false;
816 result_len++;
817 }
818 return result_len;
819 }
820
startmap_nokey(void * closure,const void * handler_data)821 static void *startmap_nokey(void *closure, const void *handler_data) {
822 upb_json_printer *p = closure;
823 UPB_UNUSED(handler_data);
824 p->depth_++;
825 p->first_elem_[p->depth_] = true;
826 print_data(p, "{", 1);
827 return closure;
828 }
829
putnull(void * closure,const void * handler_data,int32_t null)830 static bool putnull(void *closure, const void *handler_data,
831 int32_t null) {
832 upb_json_printer *p = closure;
833 print_data(p, "null", 4);
834 UPB_UNUSED(handler_data);
835 UPB_UNUSED(null);
836 return true;
837 }
838
printer_startdurationmsg(void * closure,const void * 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_);
844 }
845 return true;
846 }
847
848 #define UPB_DURATION_MAX_JSON_LEN 23
849 #define UPB_DURATION_MAX_NANO_LEN 9
850
printer_enddurationmsg(void * closure,const void * handler_data,upb_status * s)851 static bool printer_enddurationmsg(void *closure, const void *handler_data,
852 upb_status *s) {
853 upb_json_printer *p = closure;
854 char buffer[UPB_DURATION_MAX_JSON_LEN];
855 size_t base_len;
856 size_t curr;
857 size_t i;
858
859 memset(buffer, 0, UPB_DURATION_MAX_JSON_LEN);
860
861 if (p->seconds < -315576000000) {
862 upb_status_seterrf(s, "error parsing duration: "
863 "minimum acceptable value is "
864 "-315576000000");
865 return false;
866 }
867
868 if (p->seconds > 315576000000) {
869 upb_status_seterrf(s, "error serializing duration: "
870 "maximum acceptable value is "
871 "315576000000");
872 return false;
873 }
874
875 snprintf(buffer, sizeof(buffer), "%ld", (long)p->seconds);
876 base_len = strlen(buffer);
877
878 if (p->nanos != 0) {
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--) {
885 nanos_buffer[i] = 0;
886 }
887 strcpy(buffer + base_len, nanos_buffer + 1);
888 }
889
890 curr = strlen(buffer);
891 strcpy(buffer + curr, "s");
892
893 p->seconds = 0;
894 p->nanos = 0;
895
896 print_data(p, "\"", 1);
897 print_data(p, buffer, strlen(buffer));
898 print_data(p, "\"", 1);
899
900 if (p->depth_ == 0) {
901 upb_bytessink_end(p->output_);
902 }
903
904 UPB_UNUSED(handler_data);
905 return true;
906 }
907
printer_starttimestampmsg(void * closure,const void * 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_);
913 }
914 return true;
915 }
916
917 #define UPB_TIMESTAMP_MAX_JSON_LEN 31
918 #define UPB_TIMESTAMP_BEFORE_NANO_LEN 19
919 #define UPB_TIMESTAMP_MAX_NANO_LEN 9
920
printer_endtimestampmsg(void * closure,const void * handler_data,upb_status * s)921 static bool printer_endtimestampmsg(void *closure, const void *handler_data,
922 upb_status *s) {
923 upb_json_printer *p = closure;
924 char buffer[UPB_TIMESTAMP_MAX_JSON_LEN];
925 time_t time = p->seconds;
926 size_t curr;
927 size_t i;
928 size_t year_length =
929 strftime(buffer, UPB_TIMESTAMP_MAX_JSON_LEN, "%Y", gmtime(&time));
930
931 if (p->seconds < -62135596800) {
932 upb_status_seterrf(s, "error parsing timestamp: "
933 "minimum acceptable value is "
934 "0001-01-01T00:00:00Z");
935 return false;
936 }
937
938 if (p->seconds > 253402300799) {
939 upb_status_seterrf(s, "error parsing timestamp: "
940 "maximum acceptable value is "
941 "9999-12-31T23:59:59Z");
942 return false;
943 }
944
945 /* strftime doesn't guarantee 4 digits for year. Prepend 0 by ourselves. */
946 for (i = 0; i < 4 - year_length; i++) {
947 buffer[i] = '0';
948 }
949
950 strftime(buffer + (4 - year_length), UPB_TIMESTAMP_MAX_JSON_LEN,
951 "%Y-%m-%dT%H:%M:%S", gmtime(&time));
952 if (p->nanos != 0) {
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--) {
959 nanos_buffer[i] = 0;
960 }
961 strcpy(buffer + UPB_TIMESTAMP_BEFORE_NANO_LEN, nanos_buffer + 1);
962 }
963
964 curr = strlen(buffer);
965 strcpy(buffer + curr, "Z");
966
967 p->seconds = 0;
968 p->nanos = 0;
969
970 print_data(p, "\"", 1);
971 print_data(p, buffer, strlen(buffer));
972 print_data(p, "\"", 1);
973
974 if (p->depth_ == 0) {
975 upb_bytessink_end(p->output_);
976 }
977
978 UPB_UNUSED(handler_data);
979 UPB_UNUSED(s);
980 return true;
981 }
982
printer_startmsg_noframe(void * closure,const void * 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_);
988 }
989 return true;
990 }
991
printer_endmsg_noframe(void * closure,const void * handler_data,upb_status * s)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);
996 UPB_UNUSED(s);
997 if (p->depth_ == 0) {
998 upb_bytessink_end(p->output_);
999 }
1000 return true;
1001 }
1002
printer_startmsg_fieldmask(void * closure,const void * handler_data)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_);
1009 }
1010 print_data(p, "\"", 1);
1011 return true;
1012 }
1013
printer_endmsg_fieldmask(void * closure,const void * handler_data,upb_status * s)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);
1018 UPB_UNUSED(s);
1019 print_data(p, "\"", 1);
1020 if (p->depth_ == 0) {
1021 upb_bytessink_end(p->output_);
1022 }
1023 return true;
1024 }
1025
scalar_startstr_onlykey(void * closure,const void * handler_data,size_t size_hint)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));
1031 return p;
1032 }
1033
1034 /* Set up handlers for an Any submessage. */
printer_sethandlers_any(const void * closure,upb_handlers * h)1035 void printer_sethandlers_any(const void *closure, upb_handlers *h) {
1036 const upb_msgdef *md = upb_handlers_msgdef(h);
1037
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);
1040
1041 upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
1042
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");
1048
1049 type_name_attr.handler_data = type_url_json_name;
1050 value_name_attr.handler_data = value_json_name;
1051
1052 /* Set up handlers. */
1053 upb_handlers_setstartmsg(h, printer_startmsg, &empty_attr);
1054 upb_handlers_setendmsg(h, printer_endmsg, &empty_attr);
1055
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);
1059
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.
1062 */
1063 upb_handlers_setstartstr(h, value_field, scalar_startstr_onlykey,
1064 &value_name_attr);
1065
1066 UPB_UNUSED(closure);
1067 }
1068
1069 /* Set up handlers for a fieldmask submessage. */
printer_sethandlers_fieldmask(const void * closure,upb_handlers * h)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);
1073
1074 upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
1075
1076 upb_handlers_setstartseq(h, f, startseq_fieldmask, &empty_attr);
1077 upb_handlers_setendseq(h, f, endseq_fieldmask, &empty_attr);
1078
1079 upb_handlers_setstartmsg(h, printer_startmsg_fieldmask, &empty_attr);
1080 upb_handlers_setendmsg(h, printer_endmsg_fieldmask, &empty_attr);
1081
1082 upb_handlers_setstartstr(h, f, repeated_startstr_fieldmask, &empty_attr);
1083 upb_handlers_setstring(h, f, repeated_str_fieldmask, &empty_attr);
1084
1085 UPB_UNUSED(closure);
1086 }
1087
1088 /* Set up handlers for a duration submessage. */
printer_sethandlers_duration(const void * closure,upb_handlers * h)1089 void printer_sethandlers_duration(const void *closure, upb_handlers *h) {
1090 const upb_msgdef *md = upb_handlers_msgdef(h);
1091
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);
1096
1097 upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
1098
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);
1103
1104 UPB_UNUSED(closure);
1105 }
1106
1107 /* Set up handlers for a timestamp submessage. Instead of printing fields
1108 * separately, the json representation of timestamp follows RFC 3339 */
printer_sethandlers_timestamp(const void * closure,upb_handlers * h)1109 void printer_sethandlers_timestamp(const void *closure, upb_handlers *h) {
1110 const upb_msgdef *md = upb_handlers_msgdef(h);
1111
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);
1116
1117 upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
1118
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);
1123
1124 UPB_UNUSED(closure);
1125 }
1126
printer_sethandlers_value(const void * closure,upb_handlers * h)1127 void printer_sethandlers_value(const void *closure, upb_handlers *h) {
1128 const upb_msgdef *md = upb_handlers_msgdef(h);
1129 int i, n;
1130
1131 upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
1132
1133 upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr);
1134 upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr);
1135
1136 n = upb_msgdef_fieldcount(md);
1137 for (i = 0; i < n; i++) {
1138 const upb_fielddef *f = upb_msgdef_field(md, i);
1139
1140 switch (upb_fielddef_type(f)) {
1141 case UPB_TYPE_ENUM:
1142 upb_handlers_setint32(h, f, putnull, &empty_attr);
1143 break;
1144 case UPB_TYPE_DOUBLE:
1145 upb_handlers_setdouble(h, f, putdouble, &empty_attr);
1146 break;
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);
1151 break;
1152 case UPB_TYPE_BOOL:
1153 upb_handlers_setbool(h, f, putbool, &empty_attr);
1154 break;
1155 case UPB_TYPE_MESSAGE:
1156 break;
1157 default:
1158 UPB_ASSERT(false);
1159 break;
1160 }
1161 }
1162
1163 UPB_UNUSED(closure);
1164 }
1165
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); \
1175 }
1176
WRAPPER_SETHANDLERS(doublevalue,double,putdouble)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)
1186
1187 #undef WRAPPER_SETHANDLERS
1188
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);
1192
1193 upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
1194
1195 upb_handlers_setstartseq(h, f, startseq_nokey, &empty_attr);
1196 upb_handlers_setendseq(h, f, endseq, &empty_attr);
1197
1198 upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr);
1199 upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr);
1200
1201 upb_handlers_setstartsubmsg(h, f, repeated_startsubmsg, &empty_attr);
1202
1203 UPB_UNUSED(closure);
1204 }
1205
printer_sethandlers_structvalue(const void * closure,upb_handlers * h)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);
1209
1210 upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT;
1211
1212 upb_handlers_setstartseq(h, f, startmap_nokey, &empty_attr);
1213 upb_handlers_setendseq(h, f, endmap, &empty_attr);
1214
1215 upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr);
1216 upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr);
1217
1218 upb_handlers_setstartsubmsg(h, f, repeated_startsubmsg, &empty_attr);
1219
1220 UPB_UNUSED(closure);
1221 }
1222
printer_sethandlers(const void * closure,upb_handlers * h)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;
1227 int i, n;
1228 const upb_json_printercache *cache = closure;
1229 const bool preserve_fieldnames = cache->preserve_fieldnames;
1230
1231 if (is_mapentry) {
1232 /* mapentry messages are sufficiently different that we handle them
1233 * separately. */
1234 printer_sethandlers_mapentry(closure, preserve_fieldnames, h);
1235 return;
1236 }
1237
1238 switch (upb_msgdef_wellknowntype(md)) {
1239 case UPB_WELLKNOWN_UNSPECIFIED:
1240 break;
1241 case UPB_WELLKNOWN_ANY:
1242 printer_sethandlers_any(closure, h);
1243 return;
1244 case UPB_WELLKNOWN_FIELDMASK:
1245 printer_sethandlers_fieldmask(closure, h);
1246 return;
1247 case UPB_WELLKNOWN_DURATION:
1248 printer_sethandlers_duration(closure, h);
1249 return;
1250 case UPB_WELLKNOWN_TIMESTAMP:
1251 printer_sethandlers_timestamp(closure, h);
1252 return;
1253 case UPB_WELLKNOWN_VALUE:
1254 printer_sethandlers_value(closure, h);
1255 return;
1256 case UPB_WELLKNOWN_LISTVALUE:
1257 printer_sethandlers_listvalue(closure, h);
1258 return;
1259 case UPB_WELLKNOWN_STRUCT:
1260 printer_sethandlers_structvalue(closure, h);
1261 return;
1262 #define WRAPPER(wellknowntype, name) \
1263 case wellknowntype: \
1264 printer_sethandlers_##name(closure, h); \
1265 return; \
1266
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);
1276
1277 #undef WRAPPER
1278 }
1279
1280 upb_handlers_setstartmsg(h, printer_startmsg, &empty_attr);
1281 upb_handlers_setendmsg(h, printer_endmsg, &empty_attr);
1282
1283 #define TYPE(type, name, ctype) \
1284 case type: \
1285 if (upb_fielddef_isseq(f)) { \
1286 upb_handlers_set##name(h, f, repeated_##ctype, &empty_attr); \
1287 } else { \
1288 upb_handlers_set##name(h, f, scalar_##ctype, &name_attr); \
1289 } \
1290 break;
1291
1292 n = upb_msgdef_fieldcount(md);
1293 for (i = 0; i < n; i++) {
1294 const upb_fielddef *f = upb_msgdef_field(md, i);
1295
1296 upb_handlerattr name_attr = UPB_HANDLERATTR_INIT;
1297 name_attr.handler_data = newstrpc(h, f, preserve_fieldnames);
1298
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);
1305 }
1306
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
1318 * need first. */
1319 upb_handlerattr enum_attr = UPB_HANDLERATTR_INIT;
1320 set_enum_hd(h, f, preserve_fieldnames, &enum_attr);
1321
1322 if (upb_fielddef_isseq(f)) {
1323 upb_handlers_setint32(h, f, repeated_enum, &enum_attr);
1324 } else {
1325 upb_handlers_setint32(h, f, scalar_enum, &enum_attr);
1326 }
1327
1328 break;
1329 }
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);
1335 } else {
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);
1339 }
1340 break;
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);
1346 } else {
1347 upb_handlers_setstring(h, f, scalar_bytes, &name_attr);
1348 }
1349 break;
1350 case UPB_TYPE_MESSAGE:
1351 if (upb_fielddef_isseq(f)) {
1352 upb_handlers_setstartsubmsg(h, f, repeated_startsubmsg, &name_attr);
1353 } else {
1354 upb_handlers_setstartsubmsg(h, f, scalar_startsubmsg, &name_attr);
1355 }
1356 break;
1357 }
1358 }
1359
1360 #undef TYPE
1361 }
1362
json_printer_reset(upb_json_printer * p)1363 static void json_printer_reset(upb_json_printer *p) {
1364 p->depth_ = 0;
1365 }
1366
1367
1368 /* Public API *****************************************************************/
1369
upb_json_printer_create(upb_arena * a,const upb_handlers * h,upb_bytessink output)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;
1374
1375 p->output_ = output;
1376 json_printer_reset(p);
1377 upb_sink_reset(&p->input_, h, p);
1378 p->seconds = 0;
1379 p->nanos = 0;
1380
1381 return p;
1382 }
1383
upb_json_printer_input(upb_json_printer * p)1384 upb_sink upb_json_printer_input(upb_json_printer *p) {
1385 return p->input_;
1386 }
1387
upb_json_printer_newcache(bool preserve_proto_fieldnames)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);
1391
1392 cache->preserve_fieldnames = preserve_proto_fieldnames;
1393 upb_handlercache_addcleanup(ret, cache, upb_gfree);
1394
1395 return ret;
1396 }
1397