mirror of
https://github.com/json-c/json-c.git
synced 2026-04-08 23:09:07 +08:00
Issue #195: Actually call uselocale() in the new locale handling code in json_tokener.
Also, be sure the right locale_t is freed if we fail on the second uselocale. Finally, fix test_locale so it *doesn't* use json_object_to_json_string as that will simple re-emit the original parsed string values.
This commit is contained in:
@@ -262,12 +262,16 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
|
|||||||
// XXX at least Debian 8.4 has a bug in newlocale where it doesn't
|
// XXX at least Debian 8.4 has a bug in newlocale where it doesn't
|
||||||
// change the decimal separator unless you set LC_TIME!
|
// change the decimal separator unless you set LC_TIME!
|
||||||
if (newloc)
|
if (newloc)
|
||||||
newloc = newlocale(LC_TIME, "C", newloc);
|
{
|
||||||
|
duploc = newloc; // original duploc has been freed by newlocale()
|
||||||
|
newloc = newlocale(LC_TIME, "C", duploc);
|
||||||
|
}
|
||||||
if (newloc == NULL)
|
if (newloc == NULL)
|
||||||
{
|
{
|
||||||
freelocale(duploc);
|
freelocale(duploc);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
uselocale(newloc);
|
||||||
}
|
}
|
||||||
#elif defined(HAVE_SETLOCALE)
|
#elif defined(HAVE_SETLOCALE)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -30,13 +30,29 @@ int main(int argc, char **argv)
|
|||||||
MC_SET_DEBUG(1);
|
MC_SET_DEBUG(1);
|
||||||
|
|
||||||
new_obj = json_tokener_parse("[1.2,3.4,123456.78,5.0,2.3e10]");
|
new_obj = json_tokener_parse("[1.2,3.4,123456.78,5.0,2.3e10]");
|
||||||
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
|
|
||||||
printf("new_obj.to_string()=%s\n", json_object_to_json_string_ext(new_obj,JSON_C_TO_STRING_NOZERO));
|
|
||||||
json_object_put(new_obj);
|
|
||||||
|
|
||||||
(void)snprintf(buf2, sizeof(buf2), "%f", 0.1);
|
(void)snprintf(buf2, sizeof(buf2), "%f", 0.1);
|
||||||
if (strcmp(buf1, buf2) != 0)
|
if (strcmp(buf1, buf2) != 0)
|
||||||
printf("ERROR: Original locale not restored \"%s\" != \"%s\"",
|
printf("ERROR: Original locale not restored \"%s\" != \"%s\"",
|
||||||
buf1, buf2);
|
buf1, buf2);
|
||||||
|
|
||||||
|
#ifdef HAVE_SETLOCALE
|
||||||
|
setlocale(LC_NUMERIC, "C");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Explicitly print each value, to avoid having the "special"
|
||||||
|
// serialization done for parsed doubles simply re-emit the original
|
||||||
|
// string that was parsed. (see json_object_new_double_s())
|
||||||
|
printf("new_obj.to_string()=[");
|
||||||
|
unsigned int ii;
|
||||||
|
for (ii = 0 ; ii < json_object_array_length(new_obj); ii++)
|
||||||
|
{
|
||||||
|
json_object *val = json_object_array_get_idx(new_obj, ii);
|
||||||
|
printf("%s%.2lf", (ii > 0) ? "," : "", json_object_get_double(val));
|
||||||
|
}
|
||||||
|
printf("]\n");
|
||||||
|
|
||||||
|
printf("new_obj.to_string()=%s\n", json_object_to_json_string_ext(new_obj,JSON_C_TO_STRING_NOZERO));
|
||||||
|
json_object_put(new_obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
new_obj.to_string()=[ 1.2, 3.4, 123456.78, 5.0, 2.3e10 ]
|
new_obj.to_string()=[1.20,3.40,123456.78,5.00,23000000000.00]
|
||||||
new_obj.to_string()=[1.2,3.4,123456.78,5.0,2.3e10]
|
new_obj.to_string()=[1.2,3.4,123456.78,5.0,2.3e10]
|
||||||
|
|||||||
Reference in New Issue
Block a user