keep the exponent when trimming zeroes with JSON_C_TO_STRING_NOZERO

This commit is contained in:
Javid Khan authored and Eric Hawicz committed 2026-11-08 21:41:15 -05:00
1 parent cdd8db0530
commit 7f4196213a
3 files changed
+22 -5

No files matched your search

+8 -5
View File
@@ -1328,15 +1328,18 @@ static int json_object_double_to_json_string_format(struct json_object *jso, str
{
/* last useful digit, always keep 1 zero */
p++;
for (q = p; *q; q++)
for (q = p; is_plain_digit(*q); q++)
{
if (*q != '0')
p = q;
}
/* drop trailing zeroes */
if (*p != 0)
*(++p) = 0;
size = p - buf;
/* drop trailing zeroes, keeping any exponent after them */
if (is_plain_digit(*p))
{
p++;
memmove(p, q, strlen(q) + 1);
}
size = (int)strlen(buf);
}
}
// although unlikely, snprintf can fail
+12
View File
@@ -41,6 +41,18 @@ int main(int argc, char **argv)
json_object_set_serializer(obj, json_object_double_to_json_string, data, NULL);
printf("obj.to_string(reset)=%s\n", json_object_to_json_string_ext(obj, 4));
json_object_put(obj);
/* NOZERO must only trim the fraction, never the exponent */
obj = json_object_new_double(1.5e20);
printf("obj(1.5e20).to_string(nozero)=%s\n",
json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOZERO));
json_object_put(obj);
obj = json_object_new_double(1.5e20);
char data_e[] = "%.3e";
json_object_set_serializer(obj, json_object_double_to_json_string, data_e, NULL);
printf("obj(1.5e20).to_string(%%.3e,nozero)=%s\n",
json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOZERO));
json_object_put(obj);
obj = json_object_new_double(0.52381);
+2
View File
@@ -8,6 +8,8 @@ Test reset serializer:
obj.to_string(reset)=0.5
Test no zero reset serializer:
obj.to_string(reset)=3.1415000000000002
obj(1.5e20).to_string(nozero)=1.5e+20
obj(1.5e20).to_string(%.3e,nozero)=1.5e+20
obj.to_string(default format)=0.52381
obj.to_string(with global format)=x0.524y
obj.to_string(with thread format)=T0.52X