diff options
Diffstat (limited to 'src/jv_print.c')
-rw-r--r-- | src/jv_print.c | 37 |
1 files changed, 21 insertions, 16 deletions
diff --git a/src/jv_print.c b/src/jv_print.c index 5ebc01e6..6c848035 100644 --- a/src/jv_print.c +++ b/src/jv_print.c @@ -11,8 +11,10 @@ #include "jv.h" #include "jv_dtoa.h" +#include "jv_dtoa_tsd.h" #include "jv_unicode.h" #include "jv_alloc.h" +#include "jv_type_private.h" #ifndef MAX_PRINT_DEPTH #define MAX_PRINT_DEPTH (256) @@ -229,15 +231,24 @@ static void jv_dump_term(struct dtoa_context* C, jv x, int flags, int indent, FI put_str("true", F, S, flags & JV_PRINT_ISATTY); break; case JV_KIND_NUMBER: { - double d = jv_number_value(x); - if (d != d) { - // JSON doesn't have NaN, so we'll render it as "null" - put_str("null", F, S, flags & JV_PRINT_ISATTY); + if (jvp_number_is_nan(x)) { + jv_dump_term(C, jv_null(), flags, indent, F, S); } else { - // Normalise infinities to something we can print in valid JSON - if (d > DBL_MAX) d = DBL_MAX; - if (d < -DBL_MAX) d = -DBL_MAX; - put_str(jvp_dtoa_fmt(C, buf, d), F, S, flags & JV_PRINT_ISATTY); + const char * literal_data = jv_number_get_literal(x); + if (literal_data) { + put_str(literal_data, F, S, flags & JV_PRINT_ISATTY); + } else { + double d = jv_number_value(x); + if (d != d) { + // JSON doesn't have NaN, so we'll render it as "null" + put_str("null", F, S, flags & JV_PRINT_ISATTY); + } else { + // Normalise infinities to something we can print in valid JSON + if (d > DBL_MAX) d = DBL_MAX; + if (d < -DBL_MAX) d = -DBL_MAX; + put_str(jvp_dtoa_fmt(C, buf, d), F, S, flags & JV_PRINT_ISATTY); + } + } } break; } @@ -357,10 +368,7 @@ static void jv_dump_term(struct dtoa_context* C, jv x, int flags, int indent, FI } void jv_dumpf(jv x, FILE *f, int flags) { - struct dtoa_context C; - jvp_dtoa_context_init(&C); - jv_dump_term(&C, x, flags, 0, f, 0); - jvp_dtoa_context_free(&C); + jv_dump_term(tsd_dtoa_context_get(), x, flags, 0, f, 0); } void jv_dump(jv x, int flags) { @@ -376,11 +384,8 @@ void jv_show(jv x, int flags) { } jv jv_dump_string(jv x, int flags) { - struct dtoa_context C; - jvp_dtoa_context_init(&C); jv s = jv_string(""); - jv_dump_term(&C, x, flags, 0, 0, &s); - jvp_dtoa_context_free(&C); + jv_dump_term(tsd_dtoa_context_get(), x, flags, 0, 0, &s); return s; } |