diff options
author | Joshua Haberman <jhaberman@gmail.com> | 2017-06-20 18:35:22 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-20 18:35:22 -0700 |
commit | e38098cbfc58c0e2911b2c2b2abff043de42a85b (patch) | |
tree | a17a6b494a777453c05d79cc11d8d63ed19162e4 /upb/json/printer.c | |
parent | 8389aa29772c86cb961a131e0c81d5b46a48e1b9 (diff) | |
parent | cbc2d7af561e149e7c445e63a485793e2669b89c (diff) |
Merge pull request #86 from haberman/json-numbers
Some fixes to make JSON properly recognize numbers in quotes.
Diffstat (limited to 'upb/json/printer.c')
-rw-r--r-- | upb/json/printer.c | 19 |
1 files changed, 16 insertions, 3 deletions
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) { |