Skip to content

Commit 4885ca3

Browse files
author
James McLaughlin
committed
Append .0 to integers so they will be re-parsed as doubles
1 parent 51f3ab4 commit 4885ca3

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

json-builder.c

+15-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <string.h>
3434
#include <assert.h>
3535
#include <stdio.h>
36+
#include <math.h>
3637

3738
#ifdef _MSC_VER
3839
#define snprintf _snprintf
@@ -561,6 +562,10 @@ size_t json_measure_ex (json_value * value, json_serialize_opts opts)
561562
case json_double:
562563

563564
total += snprintf (NULL, 0, "%g", value->u.dbl);
565+
566+
if (value->u.dbl - floor (value->u.dbl) < 0.001)
567+
total += 2;
568+
564569
break;
565570

566571
case json_boolean:
@@ -623,7 +628,7 @@ void json_serialize_ex (json_char * buf, json_value * value, json_serialize_opts
623628
{
624629
json_int_t integer, orig_integer;
625630
json_object_entry * entry;
626-
json_char * ptr;
631+
json_char * ptr, * dot;
627632
int indent = 0;
628633
char indent_char;
629634
int i;
@@ -773,8 +778,15 @@ void json_serialize_ex (json_char * buf, json_value * value, json_serialize_opts
773778

774779
buf += sprintf (buf, "%g", value->u.dbl);
775780

776-
if ((ptr = strchr (ptr, ',')))
777-
*ptr = '.';
781+
if ((dot = strchr (ptr, ',')))
782+
{
783+
*dot = '.';
784+
}
785+
else if (!strchr (ptr, '.'))
786+
{
787+
*buf ++ = '.';
788+
*buf ++ = '0';
789+
}
778790

779791
break;
780792

0 commit comments

Comments
 (0)