From c5e14c85a635aad1d6bd226a4542ce43f1a70427 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 11 Sep 2015 09:17:07 +0200 Subject: [PATCH] read boolean json values be smarter about reading number values that are integers --- src/kernel/jsonconf.c | 7 ++++++- src/kernel/jsonconf.test.c | 8 ++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/kernel/jsonconf.c b/src/kernel/jsonconf.c index 465478775..7b2297146 100644 --- a/src/kernel/jsonconf.c +++ b/src/kernel/jsonconf.c @@ -650,7 +650,12 @@ static void json_settings(cJSON *json) { } else { char value[32]; - _snprintf(value, sizeof(value), "%lf", child->valuedouble); + if (child->type == cJSON_Number && child->valuedouble && child->valueintvaluedouble) { + _snprintf(value, sizeof(value), "%lf", child->valuedouble); + } + else { + _snprintf(value, sizeof(value), "%d", child->valueint); + } set_param(&global.parameters, child->string, value); } } diff --git a/src/kernel/jsonconf.test.c b/src/kernel/jsonconf.test.c index 38fd41ae9..c317fc15a 100644 --- a/src/kernel/jsonconf.test.c +++ b/src/kernel/jsonconf.test.c @@ -62,14 +62,18 @@ static void test_settings(CuTest * tc) const char * data = "{\"settings\": { " "\"string\" : \"1d4\"," "\"integer\" : 14," - "\"float\" : 0.5 }}"; + "\"true\": true," + "\"false\": false," + "\"float\" : 1.5 }}"; cJSON *json = cJSON_Parse(data); test_cleanup(); json_config(json); + CuAssertStrEquals(tc, "1", get_param(global.parameters, "true")); + CuAssertStrEquals(tc, "0", get_param(global.parameters, "false")); CuAssertStrEquals(tc, "1d4", get_param(global.parameters, "string")); CuAssertIntEquals(tc, 14, get_param_int(global.parameters, "integer", 0)); - CuAssertDblEquals(tc, 0.5f, get_param_flt(global.parameters, "float", 0), 0.01); + CuAssertDblEquals(tc, 1.5f, get_param_flt(global.parameters, "float", 0), 0.01); test_cleanup(); }