diff options
author | Eric Haszlakiewicz <erh+git@nimenees.com> | 2013-06-18 21:18:27 -0700 |
---|---|---|
committer | Eric Haszlakiewicz <erh+git@nimenees.com> | 2013-06-18 21:18:27 -0700 |
commit | 98a62a765222d31331cae640e20c409f5b3dc7f2 (patch) | |
tree | ae57185bc44d928498f71e6f91935faa2fd08c7a /json_object.c | |
parent | b6539d6e9055a72c8518c7079ba51efee4503d6a (diff) | |
parent | d086e2018c7938b0038db714335213a2ad91fa91 (diff) |
Merge pull request #89 from ayanes/master
Support NaN and Infinity
Diffstat (limited to 'json_object.c')
-rw-r--r-- | json_object.c | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/json_object.c b/json_object.c index facb824..e12c440 100644 --- a/json_object.c +++ b/json_object.c @@ -16,6 +16,7 @@ #include <stdlib.h> #include <stddef.h> #include <string.h> +#include <math.h> #include "debug.h" #include "printbuf.h" @@ -568,8 +569,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 = '.'; |