Welcome! Log In Create A New Profile

Advanced

[njs] Fixed truth value of JSON numbers in JSON.parse().

Dmitry Volyntsev
June 11, 2019 10:36AM
details: https://hg.nginx.org/njs/rev/028277f5c551
branches:
changeset: 1004:028277f5c551
user: Dmitry Volyntsev <xeioex@nginx.com>
date: Tue Jun 11 17:34:59 2019 +0300
description:
Fixed truth value of JSON numbers in JSON.parse().

This closes #180 issue on Github.

diffstat:

njs/njs_json.c | 3 ++-
njs/test/njs_unit_test.c | 24 ++++++++++++++++++++++++
2 files changed, 26 insertions(+), 1 deletions(-)

diffs (48 lines):

diff -r c46dca06edb1 -r 028277f5c551 njs/njs_json.c
--- a/njs/njs_json.c Mon Jun 10 21:40:11 2019 -0400
+++ b/njs/njs_json.c Tue Jun 11 17:34:59 2019 +0300
@@ -876,8 +876,9 @@ njs_json_parse_number(njs_json_parse_ctx
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 -r c46dca06edb1 -r 028277f5c551 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c Mon Jun 10 21:40:11 2019 -0400
+++ b/njs/test/njs_unit_test.c Tue Jun 11 17:34:59 2019 +0300
@@ -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]") },

_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[njs] Fixed truth value of JSON numbers in JSON.parse().

Dmitry Volyntsev 274 June 11, 2019 10:36AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 264
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready