1 
2 #include "upb/json_encode.h"
3 
4 #include <ctype.h>
5 #include <float.h>
6 #include <inttypes.h>
7 #include <math.h>
8 #include <setjmp.h>
9 #include <stdarg.h>
10 #include <stdio.h>
11 #include <string.h>
12 
13 #include "upb/decode.h"
14 #include "upb/reflection.h"
15 
16 /* Must be last. */
17 #include "upb/port_def.inc"
18 
19 typedef struct {
20   char *buf, *ptr, *end;
21   size_t overflow;
22   int indent_depth;
23   int options;
24   const upb_symtab *ext_pool;
25   jmp_buf err;
26   upb_status *status;
27   upb_arena *arena;
28 } jsonenc;
29 
30 static void jsonenc_msg(jsonenc *e, const upb_msg *msg, const upb_msgdef *m);
31 static void jsonenc_scalar(jsonenc *e, upb_msgval val, const upb_fielddef *f);
32 static void jsonenc_msgfield(jsonenc *e, const upb_msg *msg,
33                              const upb_msgdef *m);
34 static void jsonenc_msgfields(jsonenc *e, const upb_msg *msg,
35                               const upb_msgdef *m);
36 static void jsonenc_value(jsonenc *e, const upb_msg *msg, const upb_msgdef *m);
37 
jsonenc_err(jsonenc * e,const char * msg)38 UPB_NORETURN static void jsonenc_err(jsonenc *e, const char *msg) {
39   upb_status_seterrmsg(e->status, msg);
40   longjmp(e->err, 1);
41 }
42 
jsonenc_errf(jsonenc * e,const char * fmt,...)43 UPB_NORETURN static void jsonenc_errf(jsonenc *e, const char *fmt, ...) {
44   va_list argp;
45   va_start(argp, fmt);
46   upb_status_vseterrf(e->status, fmt, argp);
47   va_end(argp);
48   longjmp(e->err, 1);
49 }
50 
jsonenc_arena(jsonenc * e)51 static upb_arena *jsonenc_arena(jsonenc *e) {
52   /* Create lazily, since it's only needed for Any */
53   if (!e->arena) {
54     e->arena = upb_arena_new();
55   }
56   return e->arena;
57 }
58 
jsonenc_putbytes(jsonenc * e,const void * data,size_t len)59 static void jsonenc_putbytes(jsonenc *e, const void *data, size_t len) {
60   size_t have = e->end - e->ptr;
61   if (UPB_LIKELY(have >= len)) {
62     memcpy(e->ptr, data, len);
63     e->ptr += len;
64   } else {
65     if (have) memcpy(e->ptr, data, have);
66     e->ptr += have;
67     e->overflow += (len - have);
68   }
69 }
70 
jsonenc_putstr(jsonenc * e,const char * str)71 static void jsonenc_putstr(jsonenc *e, const char *str) {
72   jsonenc_putbytes(e, str, strlen(str));
73 }
74 
jsonenc_printf(jsonenc * e,const char * fmt,...)75 static void jsonenc_printf(jsonenc *e, const char *fmt, ...) {
76   size_t n;
77   size_t have = e->end - e->ptr;
78   va_list args;
79 
80   va_start(args, fmt);
81   n = vsnprintf(e->ptr, have, fmt, args);
82   va_end(args);
83 
84   if (UPB_LIKELY(have > n)) {
85     e->ptr += n;
86   } else {
87     e->ptr += have;
88     e->overflow += (n - have);
89   }
90 }
91 
jsonenc_nanos(jsonenc * e,int32_t nanos)92 static void jsonenc_nanos(jsonenc *e, int32_t nanos) {
93   int digits = 9;
94 
95   if (nanos == 0) return;
96   if (nanos < 0 || nanos >= 1000000000) {
97     jsonenc_err(e, "error formatting timestamp as JSON: invalid nanos");
98   }
99 
100   while (nanos % 1000 == 0) {
101     nanos /= 1000;
102     digits -= 3;
103   }
104 
105   jsonenc_printf(e, ".%0.*" PRId32, digits, nanos);
106 }
107 
jsonenc_timestamp(jsonenc * e,const upb_msg * msg,const upb_msgdef * m)108 static void jsonenc_timestamp(jsonenc *e, const upb_msg *msg,
109                               const upb_msgdef *m) {
110   const upb_fielddef *seconds_f = upb_msgdef_itof(m, 1);
111   const upb_fielddef *nanos_f = upb_msgdef_itof(m, 2);
112   int64_t seconds = upb_msg_get(msg, seconds_f).int64_val;
113   int32_t nanos = upb_msg_get(msg, nanos_f).int32_val;
114   int L, N, I, J, K, hour, min, sec;
115 
116   if (seconds < -62135596800) {
117     jsonenc_err(e,
118                 "error formatting timestamp as JSON: minimum acceptable value "
119                 "is 0001-01-01T00:00:00Z");
120   } else if (seconds > 253402300799) {
121     jsonenc_err(e,
122                 "error formatting timestamp as JSON: maximum acceptable value "
123                 "is 9999-12-31T23:59:59Z");
124   }
125 
126   /* Julian Day -> Y/M/D, Algorithm from:
127    * Fliegel, H. F., and Van Flandern, T. C., "A Machine Algorithm for
128    *   Processing Calendar Dates," Communications of the Association of
129    *   Computing Machines, vol. 11 (1968), p. 657.  */
130   L = (int)(seconds / 86400) + 68569 + 2440588;
131   N = 4 * L / 146097;
132   L = L - (146097 * N + 3) / 4;
133   I = 4000 * (L + 1) / 1461001;
134   L = L - 1461 * I / 4 + 31;
135   J = 80 * L / 2447;
136   K = L - 2447 * J / 80;
137   L = J / 11;
138   J = J + 2 - 12 * L;
139   I = 100 * (N - 49) + I + L;
140 
141   sec = seconds % 60;
142   min = (seconds / 60) % 60;
143   hour = (seconds / 3600) % 24;
144 
145   jsonenc_printf(e, "\"%04d-%02d-%02dT%02d:%02d:%02d", I, J, K, hour, min, sec);
146   jsonenc_nanos(e, nanos);
147   jsonenc_putstr(e, "Z\"");
148 }
149 
jsonenc_duration(jsonenc * e,const upb_msg * msg,const upb_msgdef * m)150 static void jsonenc_duration(jsonenc *e, const upb_msg *msg, const upb_msgdef *m) {
151   const upb_fielddef *seconds_f = upb_msgdef_itof(m, 1);
152   const upb_fielddef *nanos_f = upb_msgdef_itof(m, 2);
153   int64_t seconds = upb_msg_get(msg, seconds_f).int64_val;
154   int32_t nanos = upb_msg_get(msg, nanos_f).int32_val;
155 
156   if (seconds > 315576000000 || seconds < -315576000000 ||
157       (seconds < 0) != (nanos < 0)) {
158     jsonenc_err(e, "bad duration");
159   }
160 
161   if (nanos < 0) {
162     nanos = -nanos;
163   }
164 
165   jsonenc_printf(e, "\"%" PRId64, seconds);
166   jsonenc_nanos(e, nanos);
167   jsonenc_putstr(e, "s\"");
168 }
169 
jsonenc_enum(int32_t val,const upb_fielddef * f,jsonenc * e)170 static void jsonenc_enum(int32_t val, const upb_fielddef *f, jsonenc *e) {
171   const upb_enumdef *e_def = upb_fielddef_enumsubdef(f);
172 
173   if (strcmp(upb_enumdef_fullname(e_def), "google.protobuf.NullValue") == 0) {
174     jsonenc_putstr(e, "null");
175   } else {
176     const char *name = upb_enumdef_iton(e_def, val);
177 
178     if (name) {
179       jsonenc_printf(e, "\"%s\"", name);
180     } else {
181       jsonenc_printf(e, "%" PRId32, val);
182     }
183   }
184 }
185 
jsonenc_bytes(jsonenc * e,upb_strview str)186 static void jsonenc_bytes(jsonenc *e, upb_strview str) {
187   /* This is the regular base64, not the "web-safe" version. */
188   static const char base64[] =
189       "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
190   const unsigned char *ptr = (unsigned char*)str.data;
191   const unsigned char *end = ptr + str.size;
192   char buf[4];
193 
194   jsonenc_putstr(e, "\"");
195 
196   while (end - ptr >= 3) {
197     buf[0] = base64[ptr[0] >> 2];
198     buf[1] = base64[((ptr[0] & 0x3) << 4) | (ptr[1] >> 4)];
199     buf[2] = base64[((ptr[1] & 0xf) << 2) | (ptr[2] >> 6)];
200     buf[3] = base64[ptr[2] & 0x3f];
201     jsonenc_putbytes(e, buf, 4);
202     ptr += 3;
203   }
204 
205   switch (end - ptr) {
206     case 2:
207       buf[0] = base64[ptr[0] >> 2];
208       buf[1] = base64[((ptr[0] & 0x3) << 4) | (ptr[1] >> 4)];
209       buf[2] = base64[(ptr[1] & 0xf) << 2];
210       buf[3] = '=';
211       jsonenc_putbytes(e, buf, 4);
212       break;
213     case 1:
214       buf[0] = base64[ptr[0] >> 2];
215       buf[1] = base64[((ptr[0] & 0x3) << 4)];
216       buf[2] = '=';
217       buf[3] = '=';
218       jsonenc_putbytes(e, buf, 4);
219       break;
220   }
221 
222   jsonenc_putstr(e, "\"");
223 }
224 
jsonenc_stringbody(jsonenc * e,upb_strview str)225 static void jsonenc_stringbody(jsonenc *e, upb_strview str) {
226   const char *ptr = str.data;
227   const char *end = ptr + str.size;
228 
229   while (ptr < end) {
230     switch (*ptr) {
231       case '\n':
232         jsonenc_putstr(e, "\\n");
233         break;
234       case '\r':
235         jsonenc_putstr(e, "\\r");
236         break;
237       case '\t':
238         jsonenc_putstr(e, "\\t");
239         break;
240       case '\"':
241         jsonenc_putstr(e, "\\\"");
242         break;
243       case '\f':
244         jsonenc_putstr(e, "\\f");
245         break;
246       case '\b':
247         jsonenc_putstr(e, "\\b");
248         break;
249       case '\\':
250         jsonenc_putstr(e, "\\\\");
251         break;
252       default:
253         if ((uint8_t)*ptr < 0x20) {
254           jsonenc_printf(e, "\\u%04x", (int)(uint8_t)*ptr);
255         } else {
256           /* This could be a non-ASCII byte.  We rely on the string being valid
257            * UTF-8. */
258           jsonenc_putbytes(e, ptr, 1);
259         }
260         break;
261     }
262     ptr++;
263   }
264 }
265 
jsonenc_string(jsonenc * e,upb_strview str)266 static void jsonenc_string(jsonenc *e, upb_strview str) {
267   jsonenc_putstr(e, "\"");
268   jsonenc_stringbody(e, str);
269   jsonenc_putstr(e, "\"");
270 }
271 
jsonenc_double(jsonenc * e,const char * fmt,double val)272 static void jsonenc_double(jsonenc *e, const char *fmt, double val) {
273   if (val == INFINITY) {
274     jsonenc_putstr(e, "\"Infinity\"");
275   } else if (val == -INFINITY) {
276     jsonenc_putstr(e, "\"-Infinity\"");
277   } else if (val != val) {
278     jsonenc_putstr(e, "\"NaN\"");
279   } else {
280     jsonenc_printf(e, fmt, val);
281   }
282 }
283 
jsonenc_wrapper(jsonenc * e,const upb_msg * msg,const upb_msgdef * m)284 static void jsonenc_wrapper(jsonenc *e, const upb_msg *msg,
285                             const upb_msgdef *m) {
286   const upb_fielddef *val_f = upb_msgdef_itof(m, 1);
287   upb_msgval val = upb_msg_get(msg, val_f);
288   jsonenc_scalar(e, val, val_f);
289 }
290 
jsonenc_getanymsg(jsonenc * e,upb_strview type_url)291 static const upb_msgdef *jsonenc_getanymsg(jsonenc *e, upb_strview type_url) {
292   /* Find last '/', if any. */
293   const char *end = type_url.data + type_url.size;
294   const char *ptr = end;
295   const upb_msgdef *ret;
296 
297   if (!e->ext_pool) {
298     jsonenc_err(e, "Tried to encode Any, but no symtab was provided");
299   }
300 
301   if (type_url.size == 0) goto badurl;
302 
303   while (true) {
304     if (--ptr == type_url.data) {
305       /* Type URL must contain at least one '/', with host before. */
306       goto badurl;
307     }
308     if (*ptr == '/') {
309       ptr++;
310       break;
311     }
312   }
313 
314   ret = upb_symtab_lookupmsg2(e->ext_pool, ptr, end - ptr);
315 
316   if (!ret) {
317     jsonenc_errf(e, "Couldn't find Any type: %.*s", (int)(end - ptr), ptr);
318   }
319 
320   return ret;
321 
322 badurl:
323   jsonenc_errf(
324       e, "Bad type URL: " UPB_STRVIEW_FORMAT, UPB_STRVIEW_ARGS(type_url));
325 }
326 
jsonenc_any(jsonenc * e,const upb_msg * msg,const upb_msgdef * m)327 static void jsonenc_any(jsonenc *e, const upb_msg *msg, const upb_msgdef *m) {
328   const upb_fielddef *type_url_f = upb_msgdef_itof(m, 1);
329   const upb_fielddef *value_f = upb_msgdef_itof(m, 2);
330   upb_strview type_url = upb_msg_get(msg, type_url_f).str_val;
331   upb_strview value = upb_msg_get(msg, value_f).str_val;
332   const upb_msgdef *any_m = jsonenc_getanymsg(e, type_url);
333   const upb_msglayout *any_layout = upb_msgdef_layout(any_m);
334   upb_arena *arena = jsonenc_arena(e);
335   upb_msg *any = upb_msg_new(any_m, arena);
336 
337   if (!upb_decode(value.data, value.size, any, any_layout, arena)) {
338     jsonenc_err(e, "Error decoding message in Any");
339   }
340 
341   jsonenc_putstr(e, "{\"@type\":");
342   jsonenc_string(e, type_url);
343   jsonenc_putstr(e, ",");
344 
345   if (upb_msgdef_wellknowntype(any_m) == UPB_WELLKNOWN_UNSPECIFIED) {
346     /* Regular messages: {"@type": "...","foo": 1, "bar": 2} */
347     jsonenc_msgfields(e, any, any_m);
348   } else {
349     /* Well-known type: {"@type": "...","value": <well-known encoding>} */
350     jsonenc_putstr(e, "\"value\":");
351     jsonenc_msgfield(e, any, any_m);
352   }
353 
354   jsonenc_putstr(e, "}");
355 }
356 
jsonenc_putsep(jsonenc * e,const char * str,bool * first)357 static void jsonenc_putsep(jsonenc *e, const char *str, bool *first) {
358   if (*first) {
359     *first = false;
360   } else {
361     jsonenc_putstr(e, str);
362   }
363 }
364 
jsonenc_fieldpath(jsonenc * e,upb_strview path)365 static void jsonenc_fieldpath(jsonenc *e, upb_strview path) {
366   const char *ptr = path.data;
367   const char *end = ptr + path.size;
368 
369   while (ptr < end) {
370     char ch = *ptr;
371 
372     if (ch >= 'A' && ch <= 'Z') {
373       jsonenc_err(e, "Field mask element may not have upper-case letter.");
374     } else if (ch == '_') {
375       if (ptr == end - 1 || *(ptr + 1) < 'a' || *(ptr + 1) > 'z') {
376         jsonenc_err(e, "Underscore must be followed by a lowercase letter.");
377       }
378       ch = *++ptr - 32;
379     }
380 
381     jsonenc_putbytes(e, &ch, 1);
382     ptr++;
383   }
384 }
385 
jsonenc_fieldmask(jsonenc * e,const upb_msg * msg,const upb_msgdef * m)386 static void jsonenc_fieldmask(jsonenc *e, const upb_msg *msg,
387                               const upb_msgdef *m) {
388   const upb_fielddef *paths_f = upb_msgdef_itof(m, 1);
389   const upb_array *paths = upb_msg_get(msg, paths_f).array_val;
390   bool first = true;
391   size_t i, n = 0;
392 
393   if (paths) n = upb_array_size(paths);
394 
395   jsonenc_putstr(e, "\"");
396 
397   for (i = 0; i < n; i++) {
398     jsonenc_putsep(e, ",", &first);
399     jsonenc_fieldpath(e, upb_array_get(paths, i).str_val);
400   }
401 
402   jsonenc_putstr(e, "\"");
403 }
404 
jsonenc_struct(jsonenc * e,const upb_msg * msg,const upb_msgdef * m)405 static void jsonenc_struct(jsonenc *e, const upb_msg *msg,
406                            const upb_msgdef *m) {
407   const upb_fielddef *fields_f = upb_msgdef_itof(m, 1);
408   const upb_map *fields = upb_msg_get(msg, fields_f).map_val;
409   const upb_msgdef *entry_m = upb_fielddef_msgsubdef(fields_f);
410   const upb_fielddef *value_f = upb_msgdef_itof(entry_m, 2);
411   size_t iter = UPB_MAP_BEGIN;
412   bool first = true;
413 
414   jsonenc_putstr(e, "{");
415 
416   if (fields) {
417     while (upb_mapiter_next(fields, &iter)) {
418       upb_msgval key = upb_mapiter_key(fields, iter);
419       upb_msgval val = upb_mapiter_value(fields, iter);
420 
421       jsonenc_putsep(e, ",", &first);
422       jsonenc_string(e, key.str_val);
423       jsonenc_putstr(e, ":");
424       jsonenc_value(e, val.msg_val, upb_fielddef_msgsubdef(value_f));
425     }
426   }
427 
428   jsonenc_putstr(e, "}");
429 }
430 
jsonenc_listvalue(jsonenc * e,const upb_msg * msg,const upb_msgdef * m)431 static void jsonenc_listvalue(jsonenc *e, const upb_msg *msg,
432                               const upb_msgdef *m) {
433   const upb_fielddef *values_f = upb_msgdef_itof(m, 1);
434   const upb_msgdef *values_m = upb_fielddef_msgsubdef(values_f);
435   const upb_array *values = upb_msg_get(msg, values_f).array_val;
436   size_t i;
437   bool first = true;
438 
439   jsonenc_putstr(e, "[");
440 
441   if (values) {
442     const size_t size = upb_array_size(values);
443     for (i = 0; i < size; i++) {
444       upb_msgval elem = upb_array_get(values, i);
445 
446       jsonenc_putsep(e, ",", &first);
447       jsonenc_value(e, elem.msg_val, values_m);
448     }
449   }
450 
451   jsonenc_putstr(e, "]");
452 }
453 
jsonenc_value(jsonenc * e,const upb_msg * msg,const upb_msgdef * m)454 static void jsonenc_value(jsonenc *e, const upb_msg *msg, const upb_msgdef *m) {
455   /* TODO(haberman): do we want a reflection method to get oneof case? */
456   size_t iter = UPB_MSG_BEGIN;
457   const upb_fielddef *f;
458   upb_msgval val;
459 
460   if (!upb_msg_next(msg, m, NULL,  &f, &val, &iter)) {
461     jsonenc_err(e, "No value set in Value proto");
462   }
463 
464   switch (upb_fielddef_number(f)) {
465     case 1:
466       jsonenc_putstr(e, "null");
467       break;
468     case 2:
469       jsonenc_double(e, "%.17g", val.double_val);
470       break;
471     case 3:
472       jsonenc_string(e, val.str_val);
473       break;
474     case 4:
475       jsonenc_putstr(e, val.bool_val ? "true" : "false");
476       break;
477     case 5:
478       jsonenc_struct(e, val.msg_val, upb_fielddef_msgsubdef(f));
479       break;
480     case 6:
481       jsonenc_listvalue(e, val.msg_val, upb_fielddef_msgsubdef(f));
482       break;
483   }
484 }
485 
jsonenc_msgfield(jsonenc * e,const upb_msg * msg,const upb_msgdef * m)486 static void jsonenc_msgfield(jsonenc *e, const upb_msg *msg,
487                              const upb_msgdef *m) {
488   switch (upb_msgdef_wellknowntype(m)) {
489     case UPB_WELLKNOWN_UNSPECIFIED:
490       jsonenc_msg(e, msg, m);
491       break;
492     case UPB_WELLKNOWN_ANY:
493       jsonenc_any(e, msg, m);
494       break;
495     case UPB_WELLKNOWN_FIELDMASK:
496       jsonenc_fieldmask(e, msg, m);
497       break;
498     case UPB_WELLKNOWN_DURATION:
499       jsonenc_duration(e, msg, m);
500       break;
501     case UPB_WELLKNOWN_TIMESTAMP:
502       jsonenc_timestamp(e, msg, m);
503       break;
504     case UPB_WELLKNOWN_DOUBLEVALUE:
505     case UPB_WELLKNOWN_FLOATVALUE:
506     case UPB_WELLKNOWN_INT64VALUE:
507     case UPB_WELLKNOWN_UINT64VALUE:
508     case UPB_WELLKNOWN_INT32VALUE:
509     case UPB_WELLKNOWN_UINT32VALUE:
510     case UPB_WELLKNOWN_STRINGVALUE:
511     case UPB_WELLKNOWN_BYTESVALUE:
512     case UPB_WELLKNOWN_BOOLVALUE:
513       jsonenc_wrapper(e, msg, m);
514       break;
515     case UPB_WELLKNOWN_VALUE:
516       jsonenc_value(e, msg, m);
517       break;
518     case UPB_WELLKNOWN_LISTVALUE:
519       jsonenc_listvalue(e, msg, m);
520       break;
521     case UPB_WELLKNOWN_STRUCT:
522       jsonenc_struct(e, msg, m);
523       break;
524   }
525 }
526 
jsonenc_scalar(jsonenc * e,upb_msgval val,const upb_fielddef * f)527 static void jsonenc_scalar(jsonenc *e, upb_msgval val, const upb_fielddef *f) {
528   switch (upb_fielddef_type(f)) {
529     case UPB_TYPE_BOOL:
530       jsonenc_putstr(e, val.bool_val ? "true" : "false");
531       break;
532     case UPB_TYPE_FLOAT:
533       jsonenc_double(e, "%.9g", val.float_val);
534       break;
535     case UPB_TYPE_DOUBLE:
536       jsonenc_double(e, "%.17g", val.double_val);
537       break;
538     case UPB_TYPE_INT32:
539       jsonenc_printf(e, "%" PRId32, val.int32_val);
540       break;
541     case UPB_TYPE_UINT32:
542       jsonenc_printf(e, "%" PRIu32, val.uint32_val);
543       break;
544     case UPB_TYPE_INT64:
545       jsonenc_printf(e, "\"%" PRId64 "\"", val.int64_val);
546       break;
547     case UPB_TYPE_UINT64:
548       jsonenc_printf(e, "\"%" PRIu64 "\"", val.uint64_val);
549       break;
550     case UPB_TYPE_STRING:
551       jsonenc_string(e, val.str_val);
552       break;
553     case UPB_TYPE_BYTES:
554       jsonenc_bytes(e, val.str_val);
555       break;
556     case UPB_TYPE_ENUM:
557       jsonenc_enum(val.int32_val, f, e);
558       break;
559     case UPB_TYPE_MESSAGE:
560       jsonenc_msgfield(e, val.msg_val, upb_fielddef_msgsubdef(f));
561       break;
562   }
563 }
564 
jsonenc_mapkey(jsonenc * e,upb_msgval val,const upb_fielddef * f)565 static void jsonenc_mapkey(jsonenc *e, upb_msgval val, const upb_fielddef *f) {
566   jsonenc_putstr(e, "\"");
567 
568   switch (upb_fielddef_type(f)) {
569     case UPB_TYPE_BOOL:
570       jsonenc_putstr(e, val.bool_val ? "true" : "false");
571       break;
572     case UPB_TYPE_INT32:
573       jsonenc_printf(e, "%" PRId32, val.int32_val);
574       break;
575     case UPB_TYPE_UINT32:
576       jsonenc_printf(e, "%" PRIu32, val.uint32_val);
577       break;
578     case UPB_TYPE_INT64:
579       jsonenc_printf(e, "%" PRId64, val.int64_val);
580       break;
581     case UPB_TYPE_UINT64:
582       jsonenc_printf(e, "%" PRIu64, val.uint64_val);
583       break;
584     case UPB_TYPE_STRING:
585       jsonenc_stringbody(e, val.str_val);
586       break;
587     default:
588       UPB_UNREACHABLE();
589   }
590 
591   jsonenc_putstr(e, "\":");
592 }
593 
jsonenc_array(jsonenc * e,const upb_array * arr,const upb_fielddef * f)594 static void jsonenc_array(jsonenc *e, const upb_array *arr,
595                          const upb_fielddef *f) {
596   size_t i;
597   size_t size = arr ? upb_array_size(arr) : 0;
598   bool first = true;
599 
600   jsonenc_putstr(e, "[");
601 
602   for (i = 0; i < size; i++) {
603     jsonenc_putsep(e, ",", &first);
604     jsonenc_scalar(e, upb_array_get(arr, i), f);
605   }
606 
607   jsonenc_putstr(e, "]");
608 }
609 
jsonenc_map(jsonenc * e,const upb_map * map,const upb_fielddef * f)610 static void jsonenc_map(jsonenc *e, const upb_map *map, const upb_fielddef *f) {
611   const upb_msgdef *entry = upb_fielddef_msgsubdef(f);
612   const upb_fielddef *key_f = upb_msgdef_itof(entry, 1);
613   const upb_fielddef *val_f = upb_msgdef_itof(entry, 2);
614   size_t iter = UPB_MAP_BEGIN;
615   bool first = true;
616 
617   jsonenc_putstr(e, "{");
618 
619   if (map) {
620     while (upb_mapiter_next(map, &iter)) {
621       jsonenc_putsep(e, ",", &first);
622       jsonenc_mapkey(e, upb_mapiter_key(map, iter), key_f);
623       jsonenc_scalar(e, upb_mapiter_value(map, iter), val_f);
624     }
625   }
626 
627   jsonenc_putstr(e, "}");
628 }
629 
jsonenc_fieldval(jsonenc * e,const upb_fielddef * f,upb_msgval val,bool * first)630 static void jsonenc_fieldval(jsonenc *e, const upb_fielddef *f,
631                              upb_msgval val, bool *first) {
632   const char *name;
633 
634   if (e->options & UPB_JSONENC_PROTONAMES) {
635     name = upb_fielddef_name(f);
636   } else {
637     name = upb_fielddef_jsonname(f);
638   }
639 
640   jsonenc_putsep(e, ",", first);
641   jsonenc_printf(e, "\"%s\":", name);
642 
643   if (upb_fielddef_ismap(f)) {
644     jsonenc_map(e, val.map_val, f);
645   } else if (upb_fielddef_isseq(f)) {
646     jsonenc_array(e, val.array_val, f);
647   } else {
648     jsonenc_scalar(e, val, f);
649   }
650 }
651 
jsonenc_msgfields(jsonenc * e,const upb_msg * msg,const upb_msgdef * m)652 static void jsonenc_msgfields(jsonenc *e, const upb_msg *msg,
653                               const upb_msgdef *m) {
654   upb_msgval val;
655   const upb_fielddef *f;
656   bool first = true;
657 
658   if (e->options & UPB_JSONENC_EMITDEFAULTS) {
659     /* Iterate over all fields. */
660     int i = 0;
661     int n = upb_msgdef_fieldcount(m);
662     for (i = 0; i < n; i++) {
663       f = upb_msgdef_field(m, i);
664       if (!upb_fielddef_haspresence(f) || upb_msg_has(msg, f)) {
665         jsonenc_fieldval(e, f, upb_msg_get(msg, f), &first);
666       }
667     }
668   } else {
669     /* Iterate over non-empty fields. */
670     size_t iter = UPB_MSG_BEGIN;
671     while (upb_msg_next(msg, m, e->ext_pool, &f, &val, &iter)) {
672       jsonenc_fieldval(e, f, val, &first);
673     }
674   }
675 }
676 
jsonenc_msg(jsonenc * e,const upb_msg * msg,const upb_msgdef * m)677 static void jsonenc_msg(jsonenc *e, const upb_msg *msg, const upb_msgdef *m) {
678   jsonenc_putstr(e, "{");
679   jsonenc_msgfields(e, msg, m);
680   jsonenc_putstr(e, "}");
681 }
682 
jsonenc_nullz(jsonenc * e,size_t size)683 static size_t jsonenc_nullz(jsonenc *e, size_t size) {
684   size_t ret = e->ptr - e->buf + e->overflow;
685 
686   if (size > 0) {
687     if (e->ptr == e->end) e->ptr--;
688     *e->ptr = '\0';
689   }
690 
691   return ret;
692 }
693 
upb_json_encode(const upb_msg * msg,const upb_msgdef * m,const upb_symtab * ext_pool,int options,char * buf,size_t size,upb_status * status)694 size_t upb_json_encode(const upb_msg *msg, const upb_msgdef *m,
695                        const upb_symtab *ext_pool, int options, char *buf,
696                        size_t size, upb_status *status) {
697   jsonenc e;
698 
699   e.buf = buf;
700   e.ptr = buf;
701   e.end = buf + size;
702   e.overflow = 0;
703   e.options = options;
704   e.ext_pool = ext_pool;
705   e.status = status;
706   e.arena = NULL;
707 
708   if (setjmp(e.err)) return -1;
709 
710   jsonenc_msgfield(&e, msg, m);
711   if (e.arena) upb_arena_free(e.arena);
712   return jsonenc_nullz(&e, size);
713 }
714