From: Dmitry Volyntsev Date: Tue, 11 Jun 2019 14:34:59 +0000 (+0300) Subject: Fixed truth value of JSON numbers in JSON.parse(). X-Git-Tag: 0.3.3~12 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=5eaace2e3309ecdc88a445a92cf357d731847b76;p=njs.git Fixed truth value of JSON numbers in JSON.parse(). This closes #180 issue on Github. --- diff --git a/njs/njs_json.c b/njs/njs_json.c index 216c0923..be7569dc 100644 --- a/njs/njs_json.c +++ b/njs/njs_json.c @@ -876,8 +876,9 @@ njs_json_parse_number(njs_json_parse_ctx_t *ctx, njs_value_t *value, start = p; num = njs_number_dec_parse(&p, ctx->end); if (p != start) { - *value = njs_value_zero; value->data.u.number = sign * num; + value->type = NJS_NUMBER; + value->data.truth = njs_is_number_true(num); return p; } diff --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c index 327dac0d..d9712144 100644 --- a/njs/test/njs_unit_test.c +++ b/njs/test/njs_unit_test.c @@ -11724,6 +11724,30 @@ static njs_unit_test_t njs_test[] = "o.b = 3; o.b"), nxt_string("3") }, + { nxt_string("JSON.parse('2') || 10"), + nxt_string("2") }, + + { nxt_string("JSON.parse('0') || 10"), + nxt_string("10") }, + + { nxt_string("JSON.parse('-0') || 10"), + nxt_string("10") }, + + { nxt_string("JSON.parse('\"a\"') || 10"), + nxt_string("a") }, + + { nxt_string("JSON.parse('\"\"') || 10"), + nxt_string("10") }, + + { nxt_string("JSON.parse('true') || 10"), + nxt_string("true") }, + + { nxt_string("JSON.parse('false') || 10"), + nxt_string("10") }, + + { nxt_string("JSON.parse('null') || 10"), + nxt_string("10") }, + { nxt_string("var o = JSON.parse('{}', function(k, v) {return v;}); o"), nxt_string("[object Object]") },