Skip to content

Commit

Permalink
Reformat json_object_double_to_json_string_format() to have consisten…
Browse files Browse the repository at this point in the history
…t spacing.
  • Loading branch information
hawicz committed Jul 9, 2017
1 parent 5e33dab commit 5a99e52
Showing 1 changed file with 34 additions and 30 deletions.
64 changes: 34 additions & 30 deletions json_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -742,20 +742,24 @@ static int json_object_double_to_json_string_format(struct json_object* jso,
int flags,
const char *format)
{
char buf[128], *p, *q;
int size;
double dummy; /* needed for modf() */
/* 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, sizeof(buf), "NaN");
else if(isinf(jso->o.c_double))
if(jso->o.c_double > 0)
size = snprintf(buf, sizeof(buf), "Infinity");
else
size = snprintf(buf, sizeof(buf), "-Infinity");
char buf[128], *p, *q;
int size;
double dummy; /* needed for modf() */
/* 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, sizeof(buf), "NaN");
}
else if (isinf(jso->o.c_double))
{
if(jso->o.c_double > 0)
size = snprintf(buf, sizeof(buf), "Infinity");
else
size = snprintf(buf, sizeof(buf), "-Infinity");
}
else
{
const char *std_format = "%.17g";
Expand All @@ -782,22 +786,22 @@ static int json_object_double_to_json_string_format(struct json_object* jso,
if (size < 0)
return -1;

p = strchr(buf, ',');
if (p) {
*p = '.';
} else {
p = strchr(buf, '.');
}
if (p && (flags & JSON_C_TO_STRING_NOZERO)) {
/* last useful digit, always keep 1 zero */
p++;
for (q=p ; *q ; q++) {
if (*q!='0') p=q;
}
/* drop trailing zeroes */
*(++p) = 0;
size = p-buf;
}
p = strchr(buf, ',');
if (p)
*p = '.';
else
p = strchr(buf, '.');
if (p && (flags & JSON_C_TO_STRING_NOZERO))
{
/* last useful digit, always keep 1 zero */
p++;
for (q=p ; *q ; q++) {
if (*q!='0') p=q;
}
/* drop trailing zeroes */
*(++p) = 0;
size = p-buf;
}

if (size >= (int)sizeof(buf))
// The standard formats are guaranteed not to overrun the buffer,
Expand Down

0 comments on commit 5a99e52

Please sign in to comment.