Merge commit '7eafc04dde203ad17cb454c64dccbe1468f68cb2' into upb_upgrade

pull/23140/head
Mark D. Roth 5 years ago
commit 7f0b8e4b4e
  1. 51
      third_party/upb/BUILD
  2. 14
      third_party/upb/CMakeLists.txt
  3. 32
      third_party/upb/tests/conformance_upb.c
  4. 2
      third_party/upb/tests/empty.proto
  5. 655
      third_party/upb/upb/json_encode.c
  6. 36
      third_party/upb/upb/json_encode.h
  7. 7
      third_party/upb/upb/reflection.c
  8. 1
      third_party/upb/upb/text_encode.c
  9. 18
      third_party/upb/upbc/generator.cc

@ -110,6 +110,7 @@ cc_library(
}),
visibility = ["//visibility:public"],
deps = [
":table",
":upb",
],
)
@ -124,6 +125,7 @@ cc_library(
name = "reflection",
srcs = [
"upb/def.c",
"upb/msg.h",
"upb/reflection.c",
],
hdrs = [
@ -153,7 +155,23 @@ cc_library(
],
visibility = ["//visibility:public"],
deps = [
":port",
":reflection",
],
)
cc_library(
name = "json",
srcs = [
"upb/json_encode.c",
],
hdrs = [
"upb/json_encode.h",
],
deps = [
":port",
":reflection",
":upb",
],
)
@ -372,12 +390,24 @@ cc_test(
srcs = ["tests/test_generated_code.c"],
deps = [
":test_messages_proto3_proto_upb",
":empty_upbdefs_proto",
":test_upbproto",
":upb_test",
],
)
proto_library(
name = "empty_proto",
srcs = ["tests/empty.proto"],
)
upb_proto_reflection_library(
name = "empty_upbdefs_proto",
testonly = 1,
deps = [":empty_proto"],
)
upb_proto_library(
name = "test_messages_proto3_proto_upb",
testonly = 1,
deps = ["@com_google_protobuf//:test_messages_proto3_proto"],
@ -581,9 +611,10 @@ cc_binary(
deps = [
":conformance_proto_upb",
":conformance_proto_upbdefs",
":json",
":reflection",
":test_messages_proto2_upbdefs",
":test_messages_proto3_upbdefs",
":reflection",
":textformat",
":upb",
],
@ -665,23 +696,23 @@ cc_library(
cc_test(
name = "test_lua",
linkstatic = 1,
srcs = ["tests/bindings/lua/main.c"],
data = [
"@com_google_protobuf//:conformance_proto",
"@com_google_protobuf//:descriptor_proto",
":descriptor_proto_lua",
":test_messages_proto3_proto_lua",
":test_proto_lua",
"tests/bindings/lua/test_upb.lua",
"third_party/lunit/console.lua",
"third_party/lunit/lunit.lua",
"upb/bindings/lua/upb.lua",
":descriptor_proto_lua",
":test_messages_proto3_proto_lua",
":test_proto_lua",
"@com_google_protobuf//:conformance_proto",
"@com_google_protobuf//:descriptor_proto",
],
linkstatic = 1,
deps = [
":lupb",
"@lua//:liblua",
]
],
)
cc_binary(
@ -689,12 +720,12 @@ cc_binary(
srcs = ["upb/bindings/lua/upbc.cc"],
copts = select({
":windows": [],
"//conditions:default": CPPOPTS
"//conditions:default": CPPOPTS,
}),
visibility = ["//visibility:public"],
deps = [
"@com_google_absl//absl/strings",
"@com_google_protobuf//:protoc_lib"
"@com_google_protobuf//:protoc_lib",
],
)

@ -77,9 +77,11 @@ target_link_libraries(upb
port)
add_library(generated_code_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me INTERFACE)
target_link_libraries(generated_code_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me INTERFACE
table
upb)
add_library(reflection
upb/def.c
upb/msg.h
upb/reflection.c
upb/def.h
upb/reflection.h)
@ -92,7 +94,15 @@ add_library(textformat
upb/text_encode.c
upb/text_encode.h)
target_link_libraries(textformat
port
reflection)
add_library(json
upb/json_encode.c
upb/json_encode.h)
target_link_libraries(json
port
reflection
upb)
add_library(table INTERFACE)
target_link_libraries(table INTERFACE
port
@ -104,8 +114,8 @@ add_library(handlers
upb/handlers.h
upb/sink.h)
target_link_libraries(handlers
reflection
port
reflection
table
upb)
add_library(upb_pb
@ -122,8 +132,8 @@ add_library(upb_pb
target_link_libraries(upb_pb
descriptor_upbproto
handlers
reflection
port
reflection
table
upb)
add_library(upb_json

@ -15,6 +15,7 @@
#include "upb/decode.h"
#include "upb/encode.h"
#include "upb/reflection.h"
#include "upb/json_encode.h"
#include "upb/text_encode.h"
int test_count = 0;
@ -95,6 +96,34 @@ void serialize_text(const upb_msg *msg, const upb_msgdef *m, const ctx *c) {
c->response, upb_strview_make(data, len));
}
void serialize_json(const upb_msg *msg, const upb_msgdef *m, const ctx *c) {
size_t len;
size_t len2;
int opts = 0;
char *data;
upb_status status;
upb_status_clear(&status);
if (!conformance_ConformanceRequest_print_unknown_fields(c->request)) {
opts |= UPB_TXTENC_SKIPUNKNOWN;
}
len = upb_json_encode(msg, m, c->symtab, opts, NULL, 0, &status);
if (len == -1) {
static const char msg[] = "Error serializing.";
conformance_ConformanceResponse_set_serialize_error(
c->response, upb_strview_make(msg, strlen(msg)));
return;
}
data = upb_arena_malloc(c->arena, len + 1);
len2 = upb_json_encode(msg, m, c->symtab, opts, data, len + 1, &status);
assert(len == len2);
conformance_ConformanceResponse_set_json_payload(
c->response, upb_strview_make(data, len));
}
bool parse_input(upb_msg *msg, const upb_msgdef *m, const ctx* c) {
switch (conformance_ConformanceRequest_payload_case(c->request)) {
case conformance_ConformanceRequest_payload_protobuf_payload:
@ -122,6 +151,9 @@ void write_output(const upb_msg *msg, const upb_msgdef *m, const ctx* c) {
case conformance_TEXT_FORMAT:
serialize_text(msg, m, c);
break;
case conformance_JSON:
serialize_json(msg, m, c);
break;
default: {
static const char msg[] = "Unsupported output format.";
conformance_ConformanceResponse_set_skipped(

@ -0,0 +1,2 @@
syntax = "proto2";

@ -0,0 +1,655 @@
#include "upb/json_encode.h"
#include <ctype.h>
#include <float.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <setjmp.h>
#include "upb/decode.h"
#include "upb/reflection.h"
#include "upb/port_def.inc"
typedef struct {
char *buf, *ptr, *end;
size_t overflow;
int indent_depth;
int options;
const upb_symtab *ext_pool;
jmp_buf err;
upb_status *status;
upb_arena *arena;
} jsonenc;
static void jsonenc_msg(jsonenc *e, const upb_msg *msg, const upb_msgdef *m);
static void jsonenc_scalar(jsonenc *e, upb_msgval val, const upb_fielddef *f);
static void jsonenc_msgfield(jsonenc *e, const upb_msg *msg,
const upb_msgdef *m);
static void jsonenc_value(jsonenc *e, const upb_msg *msg, const upb_msgdef *m);
static void jsonenc_err(jsonenc *e, const char *msg) {
upb_status_seterrmsg(e->status, msg);
longjmp(e->err, 1);
}
static void jsonenc_putbytes(jsonenc *e, const void *data, size_t len) {
size_t have = e->end - e->ptr;
if (UPB_LIKELY(have >= len)) {
memcpy(e->ptr, data, len);
e->ptr += len;
} else {
memcpy(e->ptr, data, have);
e->ptr += have;
e->overflow += (len - have);
}
}
static void jsonenc_putstr(jsonenc *e, const char *str) {
jsonenc_putbytes(e, str, strlen(str));
}
static void jsonenc_printf(jsonenc *e, const char *fmt, ...) {
size_t n;
size_t have = e->end - e->ptr;
va_list args;
va_start(args, fmt);
n = _upb_vsnprintf(e->ptr, have, fmt, args);
va_end(args);
if (UPB_LIKELY(have > n)) {
e->ptr += n;
} else {
e->ptr += have;
e->overflow += (n - have);
}
}
static void jsonenc_nanos(jsonenc *e, int32_t nanos) {
const char zeros[3] = "000";
if (nanos == 0) return;
if (nanos < 0 || nanos >= 1000000000) {
jsonenc_err(e, "error formatting timestamp as JSON: invalid nanos");
}
jsonenc_printf(e, "%09" PRId32, nanos);
/* Remove trailing zeros, 3 at a time. */
while ((e->ptr - e->buf) >= 3 && memcmp(e->ptr, zeros, 3) == 0) {
e->ptr -= 3;
}
}
static void jsonenc_timestamp(jsonenc *e, const upb_msg *msg,
const upb_msgdef *m) {
const upb_fielddef *seconds_f = upb_msgdef_itof(m, 1);
const upb_fielddef *nanos_f = upb_msgdef_itof(m, 2);
int64_t seconds = upb_msg_get(msg, seconds_f).int64_val;
int32_t nanos = upb_msg_get(msg, nanos_f).int32_val;
int L, N, I, J, K, hour, min, sec;
if (seconds < -62135596800) {
jsonenc_err(e,
"error formatting timestamp as JSON: minimum acceptable value "
"is 0001-01-01T00:00:00Z");
} else if (seconds > 253402300799) {
jsonenc_err(e,
"error formatting timestamp as JSON: maximum acceptable value "
"is 9999-12-31T23:59:59Z");
}
/* Julian Day -> Y/M/D, Algorithm from:
* Fliegel, H. F., and Van Flandern, T. C., "A Machine Algorithm for
* Processing Calendar Dates," Communications of the Association of
* Computing Machines, vol. 11 (1968), p. 657. */
L = (seconds / 86400) + 2440588;
N = 4 * L / 146097;
L = L - (146097 * N + 3) / 4;
I = 4000 * (L + 1) / 1461001;
L = L - 1461 * I / 4 + 31;
J = 80 * L / 2447;
K = L - 2447 * J / 80;
L = J / 11;
J = J + 2 - 12 * L;
I = 100 * (N - 49) + I + L;
sec = seconds % 60;
min = (seconds / 60) % 60;
hour = (seconds / 3600) % 24;
jsonenc_printf(e, "\"%04d-%02d-%02dT%02d:%02d:%02d", I, J, K, hour, min, sec);
jsonenc_nanos(e, nanos);
jsonenc_putstr(e, "Z\"");
}
static void jsonenc_duration(jsonenc *e, const upb_msg *msg, const upb_msgdef *m) {
const upb_fielddef *seconds_f = upb_msgdef_itof(m, 1);
const upb_fielddef *nanos_f = upb_msgdef_itof(m, 2);
int64_t seconds = upb_msg_get(msg, seconds_f).int64_val;
int32_t nanos = upb_msg_get(msg, nanos_f).int32_val;
if (seconds > 315576000000 || seconds < -315576000000 ||
(seconds < 0) != (nanos < 0)) {
jsonenc_err(e, "bad duration");
}
jsonenc_printf(e, "\"%" PRId64, seconds);
jsonenc_nanos(e, nanos);
jsonenc_putstr(e, "s\"");
}
static void jsonenc_enum(int32_t val, const upb_fielddef *f, jsonenc *e) {
const upb_enumdef *e_def = upb_fielddef_enumsubdef(f);
const char *name = upb_enumdef_iton(e_def, val);
if (name) {
jsonenc_printf(e, "\"%s\"", name);
} else {
jsonenc_printf(e, "%" PRId32, val);
}
}
static void jsonenc_bytes(jsonenc *e, upb_strview str) {
/* This is the regular base64, not the "web-safe" version. */
static const char base64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const char *ptr = str.data;
const char *end = ptr + str.size;
char buf[4];
jsonenc_putstr(e, "\"");
while (end - ptr >= 3) {
buf[0] = base64[ptr[0] >> 2];
buf[1] = base64[((ptr[0] & 0x3) << 4) | (ptr[1] >> 4)];
buf[2] = base64[((ptr[1] & 0xf) << 2) | (ptr[2] >> 6)];
buf[3] = base64[ptr[2] & 0x3f];
jsonenc_putbytes(e, buf, 4);
ptr += 3;
}
switch (end - ptr) {
case 2:
buf[0] = base64[ptr[0] >> 2];
buf[1] = base64[((ptr[0] & 0x3) << 4) | (ptr[1] >> 4)];
buf[2] = base64[(ptr[1] & 0xf) << 2];
buf[3] = '=';
jsonenc_putbytes(e, buf, 4);
break;
case 1:
buf[0] = base64[ptr[0] >> 2];
buf[1] = base64[((ptr[0] & 0x3) << 4)];
buf[2] = '=';
buf[3] = '=';
jsonenc_putbytes(e, buf, 4);
break;
}
jsonenc_putstr(e, "\"");
}
static void jsonenc_stringbody(jsonenc *e, upb_strview str) {
const char *ptr = str.data;
const char *end = ptr + str.size;
while (ptr < end) {
switch (*ptr) {
case '\n':
jsonenc_putstr(e, "\\n");
break;
case '\r':
jsonenc_putstr(e, "\\r");
break;
case '\t':
jsonenc_putstr(e, "\\t");
break;
case '\"':
jsonenc_putstr(e, "\\\"");
break;
case '\f':
jsonenc_putstr(e, "\f'");
break;
case '\b':
jsonenc_putstr(e, "\b'");
break;
case '\\':
jsonenc_putstr(e, "\\\\");
break;
default:
if ((uint8_t)*ptr < 0x20) {
jsonenc_printf(e, "\\u%04x", (int)(uint8_t)*ptr);
} else {
/* This could be a non-ASCII byte. We rely on the string being valid
* UTF-8. */
jsonenc_putbytes(e, ptr, 1);
}
break;
}
ptr++;
}
}
static void jsonenc_string(jsonenc *e, upb_strview str) {
jsonenc_putstr(e, "\"");
jsonenc_stringbody(e, str);
jsonenc_putstr(e, "\"");
}
static void jsonenc_double(jsonenc *e, const char *fmt, double val) {
if (val == UPB_INFINITY) {
jsonenc_putstr(e, "\"Infinity\"");
} else if (val == -UPB_INFINITY) {
jsonenc_putstr(e, "\"-Infinity\"");
} else if (val != val) {
jsonenc_putstr(e, "\"NaN\"");
} else {
jsonenc_printf(e, fmt, val);
}
}
static void jsonenc_wrapper(jsonenc *e, const upb_msg *msg,
const upb_msgdef *m) {
const upb_fielddef *val_f = upb_msgdef_itof(m, 1);
upb_msgval val = upb_msg_get(m, val_f);
jsonenc_scalar(e, val, val_f);
}
const upb_msgdef *jsonenc_getanymsg(jsonenc *e, upb_strview type_url) {
/* Find last '/', if any. */
const char *end = type_url.data + type_url.size;
const char *ptr = end;
if (!e->ext_pool || type_url.size == 0) return NULL;
while (true) {
if (--ptr == type_url.data) {
/* Type URL must contain at least one '/', with host before. */
return NULL;
}
if (*ptr == '/') {
ptr++;
break;
}
}
return upb_symtab_lookupmsg2(e->ext_pool, ptr, end - ptr);
}
static void jsonenc_any(jsonenc *e, const upb_msg *msg, const upb_msgdef *m) {
const upb_fielddef *type_url_f = upb_msgdef_itof(m, 1);
const upb_fielddef *value_f = upb_msgdef_itof(m, 1);
upb_strview type_url = upb_msg_get(msg, type_url_f).str_val;
upb_strview value = upb_msg_get(msg, value_f).str_val;
const upb_msgdef *any_m = jsonenc_getanymsg(e, type_url);
const upb_msglayout *any_layout = upb_msgdef_layout(any_m);
upb_msg *any = upb_msg_new(any_m, e->arena);
if (!upb_decode(value.data, value.size, any, any_layout, e->arena)) {
jsonenc_err(e, "Error decoding message in Any");
}
jsonenc_putstr(e, "{\"@type\": ");
jsonenc_string(e, type_url);
jsonenc_putstr(e, ", ");
if (upb_msgdef_wellknowntype(m) == UPB_WELLKNOWN_UNSPECIFIED) {
/* Regular messages: {"@type": "...", "foo": 1, "bar": 2} */
jsonenc_msg(e, any, any_m);
} else {
/* Well-known type: {"@type": "...", "value": <well-known encoding>} */
jsonenc_putstr(e, "value: ");
jsonenc_msgfield(e, any, any_m);
}
jsonenc_putstr(e, "}");
}
static void jsonenc_putsep(jsonenc *e, const char *str, bool *first) {
if (*first) {
*first = false;
} else {
jsonenc_putstr(e, str);
}
}
static void jsonenc_fieldpath(jsonenc *e, upb_strview path) {
const char *ptr = path.data;
const char *end = ptr + path.size;
while (ptr < end) {
char ch = *ptr;
if (ch >= 'A' && ch <= 'Z') {
jsonenc_err(e, "Field mask element may not have upper-case letter.");
} else if (ch == '_') {
if (ptr == end - 1 || *(ptr + 1) < 'a' || *(ptr + 1) > 'z') {
jsonenc_err(e, "Underscore must be followed by a lowercase letter.");
}
} else {
jsonenc_putbytes(e, &ch, 1);
}
ptr++;
}
}
static void jsonenc_fieldmask(jsonenc *e, const upb_msg *msg,
const upb_msgdef *m) {
const upb_fielddef *paths_f = upb_msgdef_itof(m, 1);
const upb_array *paths = upb_msg_get(msg, paths_f).array_val;
bool first = true;
size_t i, n = 0;
if (paths) n = upb_array_size(paths);
jsonenc_putstr(e, "\"");
for (i = 0; i < n; i++) {
jsonenc_putsep(e, ",", &first);
jsonenc_fieldpath(e, upb_array_get(paths, i).str_val);
}
jsonenc_putstr(e, "\"");
}
static void jsonenc_struct(jsonenc *e, const upb_msg *msg,
const upb_msgdef *m) {
const upb_fielddef *fields_f = upb_msgdef_itof(m, 1);
const upb_map *fields = upb_msg_get(msg, fields_f).map_val;
const upb_msgdef *entry_m = upb_fielddef_msgsubdef(fields_f);
const upb_fielddef *value_f = upb_msgdef_itof(entry_m, 2);
size_t iter = UPB_MAP_BEGIN;
bool first = true;
jsonenc_putstr(e, "{");
while (upb_mapiter_next(fields, &iter)) {
upb_msgval key = upb_mapiter_key(fields, iter);
upb_msgval val = upb_mapiter_value(fields, iter);
jsonenc_putsep(e, ", ", &first);
jsonenc_string(e, key.str_val);
jsonenc_putstr(e, ": ");
jsonenc_value(e, val.msg_val, upb_fielddef_msgsubdef(value_f));
}
jsonenc_putstr(e, "}");
}
static void jsonenc_listvalue(jsonenc *e, const upb_msg *msg,
const upb_msgdef *m) {
const upb_fielddef *values_f = upb_msgdef_itof(m, 1);
const upb_msgdef *values_m = upb_fielddef_msgsubdef(values_f);
const upb_array *values = upb_msg_get(msg, values_f).array_val;
const size_t size = upb_array_size(values);
size_t i;
bool first = true;
jsonenc_putstr(e, "[");
for (i = 0; i < size; i++) {
upb_msgval elem = upb_array_get(values, i);
jsonenc_putsep(e, ", ", &first);
jsonenc_value(e, elem.msg_val, values_m);
}
jsonenc_putstr(e, "]");
}
static void jsonenc_value(jsonenc *e, const upb_msg *msg, const upb_msgdef *m) {
/* TODO(haberman): do we want a reflection method to get oneof case? */
size_t iter = UPB_MSG_BEGIN;
const upb_fielddef *f;
upb_msgval val;
if (!upb_msg_next(msg, m, NULL, &f, &val, &iter)) {
jsonenc_err(e, "No value set in Value proto");
}
switch (upb_fielddef_number(f)) {
case 1:
jsonenc_putstr(e, "null");
break;
case 2:
jsonenc_double(e, "%.17g", val.double_val);
break;
case 3:
jsonenc_string(e, val.str_val);
break;
case 4:
jsonenc_putstr(e, val.bool_val ? "true" : "false");
break;
case 5:
jsonenc_struct(e, val.msg_val, upb_fielddef_msgsubdef(f));
break;
case 6:
jsonenc_listvalue(e, val.msg_val, upb_fielddef_msgsubdef(f));
break;
}
}
static void jsonenc_msgfield(jsonenc *e, const upb_msg *msg,
const upb_msgdef *m) {
switch (upb_msgdef_wellknowntype(m)) {
case UPB_WELLKNOWN_UNSPECIFIED:
jsonenc_msg(e, msg, m);
break;
case UPB_WELLKNOWN_ANY:
jsonenc_any(e, msg, m);
break;
case UPB_WELLKNOWN_FIELDMASK:
jsonenc_fieldmask(e, msg, m);
break;
case UPB_WELLKNOWN_DURATION:
jsonenc_duration(e, msg, m);
break;
case UPB_WELLKNOWN_TIMESTAMP:
jsonenc_timestamp(e, msg, m);
break;
case UPB_WELLKNOWN_DOUBLEVALUE:
case UPB_WELLKNOWN_FLOATVALUE:
case UPB_WELLKNOWN_INT64VALUE:
case UPB_WELLKNOWN_UINT64VALUE:
case UPB_WELLKNOWN_INT32VALUE:
case UPB_WELLKNOWN_UINT32VALUE:
case UPB_WELLKNOWN_STRINGVALUE:
case UPB_WELLKNOWN_BYTESVALUE:
case UPB_WELLKNOWN_BOOLVALUE:
jsonenc_wrapper(e, msg, m);
break;
case UPB_WELLKNOWN_VALUE:
jsonenc_value(e, msg, m);
break;
case UPB_WELLKNOWN_LISTVALUE:
jsonenc_listvalue(e, msg, m);
break;
case UPB_WELLKNOWN_STRUCT:
jsonenc_listvalue(e, msg, m);
break;
}
}
static void jsonenc_scalar(jsonenc *e, upb_msgval val, const upb_fielddef *f) {
switch (upb_fielddef_type(f)) {
case UPB_TYPE_BOOL:
jsonenc_putstr(e, val.bool_val ? "true" : "false");
break;
case UPB_TYPE_FLOAT:
jsonenc_double(e, "%.9g", val.float_val);
break;
case UPB_TYPE_DOUBLE:
jsonenc_double(e, "%.17g", val.double_val);
break;
case UPB_TYPE_INT32:
jsonenc_printf(e, "%" PRId32, val.int32_val);
break;
case UPB_TYPE_UINT32:
jsonenc_printf(e, "%" PRIu32, val.uint32_val);
break;
case UPB_TYPE_INT64:
jsonenc_printf(e, "\"%" PRId64 "\"", val.int64_val);
break;
case UPB_TYPE_UINT64:
jsonenc_printf(e, "\"%" PRIu64 "\"", val.uint64_val);
break;
case UPB_TYPE_STRING:
jsonenc_string(e, val.str_val);
break;
case UPB_TYPE_BYTES:
jsonenc_bytes(e, val.str_val);
break;
case UPB_TYPE_ENUM:
jsonenc_enum(val.int32_val, f, e);
break;
case UPB_TYPE_MESSAGE:
jsonenc_msgfield(e, val.msg_val, upb_fielddef_msgsubdef(f));
break;
}
}
static void jsonenc_mapkey(jsonenc *e, upb_msgval val, const upb_fielddef *f) {
jsonenc_putstr(e, "\"");
switch (upb_fielddef_type(f)) {
case UPB_TYPE_BOOL:
jsonenc_putstr(e, val.bool_val ? "true" : "false");
break;
case UPB_TYPE_INT32:
jsonenc_printf(e, "%" PRId32, val.int32_val);
break;
case UPB_TYPE_UINT32:
jsonenc_printf(e, "%" PRIu32, val.uint32_val);
break;
case UPB_TYPE_INT64:
jsonenc_printf(e, "%" PRId64, val.int64_val);
break;
case UPB_TYPE_UINT64:
jsonenc_printf(e, "%" PRIu64, val.uint64_val);
break;
case UPB_TYPE_STRING:
jsonenc_stringbody(e, val.str_val);
default:
UPB_UNREACHABLE();
}
jsonenc_putstr(e, "\": ");
}
static void jsonenc_array(jsonenc *e, const upb_array *arr,
const upb_fielddef *f) {
size_t i;
size_t size = upb_array_size(arr);
bool first = true;
jsonenc_putstr(e, "[");
for (i = 0; i < size; i++) {
jsonenc_putsep(e, ", ", &first);
jsonenc_scalar(e, upb_array_get(arr, i), f);
}
jsonenc_putstr(e, "]");
}
static void jsonenc_map(jsonenc *e, const upb_map *map, const upb_fielddef *f) {
const upb_msgdef *entry = upb_fielddef_msgsubdef(f);
const upb_fielddef *key_f = upb_msgdef_itof(entry, 1);
const upb_fielddef *val_f = upb_msgdef_itof(entry, 2);
size_t iter = UPB_MAP_BEGIN;
bool first = true;
jsonenc_putstr(e, "{");
while (upb_mapiter_next(map, &iter)) {
jsonenc_putsep(e, ", ", &first);
jsonenc_mapkey(e, upb_mapiter_key(map, iter), key_f);
jsonenc_scalar(e, upb_mapiter_value(map, iter), val_f);
}
jsonenc_putstr(e, "}");
}
static void jsonenc_fieldval(jsonenc *e, const upb_fielddef *f,
upb_msgval val, bool *first) {
char buf[128];
const char *name;
if (e->options & UPB_JSONENC_PROTONAMES) {
name = upb_fielddef_name(f);
} else {
/* TODO(haberman): we need a better JSON name API. */
upb_fielddef_getjsonname(f, buf, sizeof(buf));
name = buf;
}
jsonenc_putsep(e, ", ", first);
jsonenc_printf(e, "\"%s\": ", name);
if (upb_fielddef_ismap(f)) {
jsonenc_map(e, val.map_val, f);
} else if (upb_fielddef_isseq(f)) {
jsonenc_array(e, val.array_val, f);
} else {
jsonenc_scalar(e, val, f);
}
}
static void jsonenc_msg(jsonenc *e, const upb_msg *msg, const upb_msgdef *m) {
upb_msgval val;
const upb_fielddef *f;
bool first = true;
jsonenc_putstr(e, "{");
if (e->options & UPB_JSONENC_EMITDEFAULTS) {
/* Iterate over all fields. */
upb_msg_field_iter i;
for (upb_msg_field_begin(&i, m); !upb_msg_field_done(&i);
upb_msg_field_next(&i)) {
f = upb_msg_iter_field(&i);
jsonenc_fieldval(e, f, upb_msg_get(msg, f), &first);
}
} else {
/* Iterate over non-empty fields. */
size_t iter = UPB_MSG_BEGIN;
while (upb_msg_next(msg, m, e->ext_pool, &f, &val, &iter)) {
jsonenc_fieldval(e, f, val, &first);
}
}
jsonenc_putstr(e, "}");
}
size_t jsonenc_nullz(jsonenc *e, size_t size) {
size_t ret = e->ptr - e->buf + e->overflow;
if (size > 0) {
if (e->ptr == e->end) e->ptr--;
*e->ptr = '\0';
}
return ret;
}
size_t 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) {
jsonenc e;
e.buf = buf;
e.ptr = buf;
e.end = buf + size;
e.overflow = 0;
e.options = options;
e.ext_pool = ext_pool;
e.status = status;
if (setjmp(e.err)) return -1;
jsonenc_msg(&e, msg, m);
return jsonenc_nullz(&e, size);
}

@ -0,0 +1,36 @@
#ifndef UPB_JSONENCODE_H_
#define UPB_JSONENCODE_H_
#include "upb/def.h"
#ifdef __cplusplus
extern "C" {
#endif
enum {
/* When set, emits 0/default values. TOOD(haberman): proto3 only? */
UPB_JSONENC_EMITDEFAULTS = 1,
/* When set, use normal (snake_caes) field names instead of JSON (camelCase)
names. */
UPB_JSONENC_PROTONAMES = 2
};
/* Encodes the given |msg| to JSON format. The message's reflection is given in
* |m|. The symtab in |symtab| is used to find extensions (if NULL, extensions
* will not be printed).
*
* Output is placed in the given buffer, and always NULL-terminated. The output
* size (excluding NULL) is returned. This means that a return value >= |size|
* implies that the output was truncated. (These are the same semantics as
* snprintf()). */
size_t 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);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* UPB_JSONENCODE_H_ */

@ -186,6 +186,13 @@ bool upb_msg_next(const upb_msg *msg, const upb_msgdef *m,
}
/* Continue if NULL or 0. */
if (memcmp(&test, &zero, sizeof(test)) == 0) continue;
/* Continue on empty array or map. */
if (upb_fielddef_ismap(f)) {
if (upb_map_size(test.map_val) == 0) continue;
} else if (upb_fielddef_isseq(f)) {
if (upb_array_size(test.array_val) == 0) continue;
}
}
*out_val = val;

@ -113,6 +113,7 @@ static void txtenc_string(txtenc *e, upb_strview str, bool bytes) {
} else {
txtenc_putbytes(e, ptr, 1);
}
break;
}
ptr++;
}

@ -853,12 +853,14 @@ void WriteDefSource(const protobuf::FileDescriptor* file, Output& output) {
}
output("\n");
output("static const upb_msglayout *layouts[$0] = {\n", file_messages.size());
for (auto message : file_messages) {
output(" &$0,\n", MessageInit(message));
if (!file_messages.empty()) {
output("static const upb_msglayout *layouts[$0] = {\n", file_messages.size());
for (auto message : file_messages) {
output(" &$0,\n", MessageInit(message));
}
output("};\n");
output("\n");
}
output("};\n");
output("\n");
protobuf::FileDescriptorProto file_proto;
file->CopyTo(&file_proto);
@ -905,7 +907,11 @@ void WriteDefSource(const protobuf::FileDescriptor* file, Output& output) {
output("upb_def_init $0 = {\n", DefInitSymbol(file));
output(" deps,\n");
output(" layouts,\n");
if (file_messages.empty()) {
output(" NULL,\n");
} else {
output(" layouts,\n");
}
output(" \"$0\",\n", file->name());
output(" UPB_STRVIEW_INIT(descriptor, $0)\n", file_data.size());
output("};\n");

Loading…
Cancel
Save