From d086e2018c7938b0038db714335213a2ad91fa91 Mon Sep 17 00:00:00 2001 From: Adrian Yanes Date: Fri, 7 Jun 2013 13:14:54 -0700 Subject: [PATCH] Fixes for Infinity and NaN Although JSON RFC does not support NaN or Infinity as numeric values ECMA 262 section 9.8.1 defines how to handle these cases as strings --- json_object.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/json_object.c b/json_object.c index f2b5ce0..b955f6d 100644 --- a/json_object.c +++ b/json_object.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "debug.h" #include "printbuf.h" @@ -561,8 +562,19 @@ static int json_object_double_to_json_string(struct json_object* jso, { char buf[128], *p, *q; int size; + /* Although JSON RFC does not support + NaN or Infinity as numeric values + ECMA 262 section 9.8.1 defines + how to handle these cases as strings */ + if(isnan(jso->o.c_double)) + size = snprintf(buf, 128, "NaN"); + else if(isinf(jso->o.c_double) == 1) + size = snprintf(buf, 128, "Infinity"); + else if(isinf(jso->o.c_double) == -1) + size = snprintf(buf, 128, "-Infinity"); + else + size = snprintf(buf, 128, "%f", jso->o.c_double); - size = snprintf(buf, 128, "%f", jso->o.c_double); p = strchr(buf, ','); if (p) { *p = '.'; -- 2.49.0