From c50e849ac256c9f0f1921b626a41769a0d194bfe Mon Sep 17 00:00:00 2001 From: Bo Yang Date: Thu, 13 Jun 2019 00:24:46 +0000 Subject: [PATCH 1/3] Change int64 json encoding to be string --- upb/json/printer.c | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/upb/json/printer.c b/upb/json/printer.c index 37482a8a3b..bf70d04c50 100644 --- a/upb/json/printer.c +++ b/upb/json/printer.c @@ -208,18 +208,30 @@ static size_t fmt_bool(bool val, char* buf, size_t length) { return n; } -static size_t fmt_int64(long val, char* buf, size_t length) { +static size_t fmt_int32(long val, char* buf, size_t length) { size_t n = _upb_snprintf(buf, length, "%ld", val); CHKLENGTH(n > 0 && n < length); return n; } -static size_t fmt_uint64(unsigned long long val, char* buf, size_t length) { +static size_t fmt_uint32(unsigned long long val, char* buf, size_t length) { size_t n = _upb_snprintf(buf, length, "%llu", val); CHKLENGTH(n > 0 && n < length); return n; } +static size_t fmt_int64(long val, char* buf, size_t length) { + size_t n = _upb_snprintf(buf, length, "\"%ld\"", val); + CHKLENGTH(n > 0 && n < length); + return n; +} + +static size_t fmt_uint64(unsigned long long val, char* buf, size_t length) { + size_t n = _upb_snprintf(buf, length, "\"%llu\"", val); + CHKLENGTH(n > 0 && n < length); + return n; +} + /* Print a map key given a field name. Called by scalar field handlers and by * startseq for repeated fields. */ static bool putkey(void *closure, const void *handler_data) { @@ -263,8 +275,11 @@ static bool putkey(void *closure, const void *handler_data) { static bool putmapkey_##type(void *closure, const void *handler_data, \ type val) { \ upb_json_printer *p = closure; \ + char data[64]; \ + size_t length = fmt_func(val, data, sizeof(data)); \ + UPB_UNUSED(handler_data); \ print_data(p, "\"", 1); \ - CHK(put##type(closure, handler_data, val)); \ + print_data(p, data, length); \ print_data(p, "\":", 2); \ return true; \ } @@ -272,17 +287,17 @@ static bool putkey(void *closure, const void *handler_data) { TYPE_HANDLERS(double, fmt_double) TYPE_HANDLERS(float, fmt_float) TYPE_HANDLERS(bool, fmt_bool) -TYPE_HANDLERS(int32_t, fmt_int64) -TYPE_HANDLERS(uint32_t, fmt_int64) +TYPE_HANDLERS(int32_t, fmt_int32) +TYPE_HANDLERS(uint32_t, fmt_int32) TYPE_HANDLERS(int64_t, fmt_int64) TYPE_HANDLERS(uint64_t, fmt_uint64) /* double and float are not allowed to be map keys. */ TYPE_HANDLERS_MAPKEY(bool, fmt_bool) -TYPE_HANDLERS_MAPKEY(int32_t, fmt_int64) -TYPE_HANDLERS_MAPKEY(uint32_t, fmt_int64) -TYPE_HANDLERS_MAPKEY(int64_t, fmt_int64) -TYPE_HANDLERS_MAPKEY(uint64_t, fmt_uint64) +TYPE_HANDLERS_MAPKEY(int32_t, fmt_int32) +TYPE_HANDLERS_MAPKEY(uint32_t, fmt_int32) +TYPE_HANDLERS_MAPKEY(int64_t, fmt_int32) +TYPE_HANDLERS_MAPKEY(uint64_t, fmt_uint32) #undef TYPE_HANDLERS #undef TYPE_HANDLERS_MAPKEY From 8e5f03f719d923331ad87661a139545cb6660fd5 Mon Sep 17 00:00:00 2001 From: Bo Yang Date: Fri, 14 Jun 2019 00:25:46 +0000 Subject: [PATCH 2/3] Fix integer types --- upb/json/printer.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/upb/json/printer.c b/upb/json/printer.c index bf70d04c50..1bc6498759 100644 --- a/upb/json/printer.c +++ b/upb/json/printer.c @@ -208,8 +208,8 @@ static size_t fmt_bool(bool val, char* buf, size_t length) { return n; } -static size_t fmt_int32(long val, char* buf, size_t length) { - size_t n = _upb_snprintf(buf, length, "%ld", val); +static size_t fmt_int32(long long val, char* buf, size_t length) { + size_t n = _upb_snprintf(buf, length, "%lld", val); CHKLENGTH(n > 0 && n < length); return n; } @@ -220,8 +220,8 @@ static size_t fmt_uint32(unsigned long long val, char* buf, size_t length) { return n; } -static size_t fmt_int64(long val, char* buf, size_t length) { - size_t n = _upb_snprintf(buf, length, "\"%ld\"", val); +static size_t fmt_int64(long long val, char* buf, size_t length) { + size_t n = _upb_snprintf(buf, length, "\"%lld\"", val); CHKLENGTH(n > 0 && n < length); return n; } From a6347ec35e95453c72b05624606e252672bea5ee Mon Sep 17 00:00:00 2001 From: Bo Yang Date: Fri, 14 Jun 2019 21:59:49 +0000 Subject: [PATCH 3/3] Change methods name to specify as number/string --- upb/json/printer.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/upb/json/printer.c b/upb/json/printer.c index 1bc6498759..90705a79d4 100644 --- a/upb/json/printer.c +++ b/upb/json/printer.c @@ -208,25 +208,27 @@ static size_t fmt_bool(bool val, char* buf, size_t length) { return n; } -static size_t fmt_int32(long long val, char* buf, size_t length) { +static size_t fmt_int64_as_number(long long val, char* buf, size_t length) { size_t n = _upb_snprintf(buf, length, "%lld", val); CHKLENGTH(n > 0 && n < length); return n; } -static size_t fmt_uint32(unsigned long long val, char* buf, size_t length) { +static size_t fmt_uint64_as_number( + unsigned long long val, char* buf, size_t length) { size_t n = _upb_snprintf(buf, length, "%llu", val); CHKLENGTH(n > 0 && n < length); return n; } -static size_t fmt_int64(long long val, char* buf, size_t length) { +static size_t fmt_int64_as_string(long long val, char* buf, size_t length) { size_t n = _upb_snprintf(buf, length, "\"%lld\"", val); CHKLENGTH(n > 0 && n < length); return n; } -static size_t fmt_uint64(unsigned long long val, char* buf, size_t length) { +static size_t fmt_uint64_as_string( + unsigned long long val, char* buf, size_t length) { size_t n = _upb_snprintf(buf, length, "\"%llu\"", val); CHKLENGTH(n > 0 && n < length); return n; @@ -287,17 +289,17 @@ static bool putkey(void *closure, const void *handler_data) { TYPE_HANDLERS(double, fmt_double) TYPE_HANDLERS(float, fmt_float) TYPE_HANDLERS(bool, fmt_bool) -TYPE_HANDLERS(int32_t, fmt_int32) -TYPE_HANDLERS(uint32_t, fmt_int32) -TYPE_HANDLERS(int64_t, fmt_int64) -TYPE_HANDLERS(uint64_t, fmt_uint64) +TYPE_HANDLERS(int32_t, fmt_int64_as_number) +TYPE_HANDLERS(uint32_t, fmt_int64_as_number) +TYPE_HANDLERS(int64_t, fmt_int64_as_string) +TYPE_HANDLERS(uint64_t, fmt_uint64_as_string) /* double and float are not allowed to be map keys. */ TYPE_HANDLERS_MAPKEY(bool, fmt_bool) -TYPE_HANDLERS_MAPKEY(int32_t, fmt_int32) -TYPE_HANDLERS_MAPKEY(uint32_t, fmt_int32) -TYPE_HANDLERS_MAPKEY(int64_t, fmt_int32) -TYPE_HANDLERS_MAPKEY(uint64_t, fmt_uint32) +TYPE_HANDLERS_MAPKEY(int32_t, fmt_int64_as_number) +TYPE_HANDLERS_MAPKEY(uint32_t, fmt_int64_as_number) +TYPE_HANDLERS_MAPKEY(int64_t, fmt_int64_as_number) +TYPE_HANDLERS_MAPKEY(uint64_t, fmt_uint64_as_number) #undef TYPE_HANDLERS #undef TYPE_HANDLERS_MAPKEY