From d04af15acb274025dd403ce42f96bf159a6855c2 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Thu, 8 Jun 2017 16:41:47 -0700 Subject: Some fixes to make JSON properly recognize numbers in quotes. --- upb/json/printer.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'upb/json/printer.c') diff --git a/upb/json/printer.c b/upb/json/printer.c index 0f9ca0e..f97fc55 100644 --- a/upb/json/printer.c +++ b/upb/json/printer.c @@ -154,10 +154,23 @@ static void putstring(upb_json_printer *p, const char *buf, unsigned int len) { * Right now we use %.8g and %.17g for float/double, respectively, to match * proto2::util::JsonFormat's defaults. May want to change this later. */ +const char neginf[] = "\"-Infinity\""; +const char inf[] = "\"Infinity\""; + static size_t fmt_double(double val, char* buf, size_t length) { - size_t n = _upb_snprintf(buf, length, "%.17g", val); - CHKLENGTH(n > 0 && n < length); - return n; + if (val == (1.0 / 0.0)) { + CHKLENGTH(length >= strlen(inf)); + strcpy(buf, inf); + return strlen(inf); + } else if (val == (-1.0 / 0.0)) { + CHKLENGTH(length >= strlen(neginf)); + strcpy(buf, neginf); + return strlen(neginf); + } else { + size_t n = _upb_snprintf(buf, length, "%.17g", val); + CHKLENGTH(n > 0 && n < length); + return n; + } } static size_t fmt_float(float val, char* buf, size_t length) { -- cgit v1.2.3