From 587567aa1a255360796572fdaca602dc7a2b2fb3 Mon Sep 17 00:00:00 2001 From: Jordan Lee Date: Mon, 17 Dec 2012 02:43:17 +0000 Subject: [PATCH] (trunk libT) when seraializing to JSON, set the LC_NUMERIC locale once for the entire serialization pass, instead of N times --- libtransmission/variant-json.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/libtransmission/variant-json.c b/libtransmission/variant-json.c index a1810ea15..d6f90f5cf 100644 --- a/libtransmission/variant-json.c +++ b/libtransmission/variant-json.c @@ -491,20 +491,11 @@ jsonRealFunc (const tr_variant * val, void * vdata) { struct jsonWalk * data = vdata; - char locale[128]; if (fabs (val->val.d - (int)val->val.d) < 0.00001) - { - evbuffer_add_printf (data->out, "%d", (int)val->val.d); - } + evbuffer_add_printf (data->out, "%d", (int)val->val.d); else - { - /* json requires a '.' decimal point regardless of locale */ - tr_strlcpy (locale, setlocale (LC_NUMERIC, NULL), sizeof (locale)); - setlocale (LC_NUMERIC, "POSIX"); - evbuffer_add_printf (data->out, "%.4f", tr_truncd (val->val.d, 4)); - setlocale (LC_NUMERIC, locale); - } + evbuffer_add_printf (data->out, "%.4f", tr_truncd (val->val.d, 4)); jsonChildFunc (data); } @@ -629,11 +620,19 @@ static const struct VariantWalkFuncs walk_funcs = { jsonIntFunc, void tr_variantToBufJson (const tr_variant * top, struct evbuffer * buf, bool lean) { + char lc_numeric[128]; struct jsonWalk data; + data.doIndent = !lean; data.out = buf; data.parents = NULL; + + /* json requires a '.' decimal point regardless of locale */ + tr_strlcpy (lc_numeric, setlocale (LC_NUMERIC, NULL), sizeof (lc_numeric)); + setlocale (LC_NUMERIC, "POSIX"); tr_variantWalk (top, &walk_funcs, &data, true); + setlocale (LC_NUMERIC, lc_numeric); + if (evbuffer_get_length (buf)) evbuffer_add_printf (buf, "\n"); }