read boolean json values

be smarter about reading number values that are integers
This commit is contained in:
Enno Rehling 2015-09-11 09:17:07 +02:00
parent 9bb2a625f9
commit c5e14c85a6
2 changed files with 12 additions and 3 deletions

View File

@ -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->valueint<child->valuedouble) {
_snprintf(value, sizeof(value), "%lf", child->valuedouble);
}
else {
_snprintf(value, sizeof(value), "%d", child->valueint);
}
set_param(&global.parameters, child->string, value);
}
}

View File

@ -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();
}