]> granicus.if.org Git - json-c/commitdiff
Fixes for Infinity and NaN
authorAdrian Yanes <ayanes@gnu.org>
Fri, 7 Jun 2013 20:14:54 +0000 (13:14 -0700)
committerAdrian Yanes <ayanes@gnu.org>
Thu, 13 Jun 2013 02:48:00 +0000 (19:48 -0700)
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

index f2b5ce04afeb4b842d9e333c77d0999156fc65bf..b955f6ddb7f87dd4236dddf07d9c0b94f8d5aacb 100644 (file)
@@ -16,6 +16,7 @@
 #include <stdlib.h>
 #include <stddef.h>
 #include <string.h>
+#include <math.h>
 
 #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 = '.';