[njs] Pass unprintable values to JSON.stringify() replacer function.

Dmitry Volyntsev xeioex at nginx.com
Sat Nov 23 11:29:18 UTC 2019


details:   https://hg.nginx.org/njs/rev/cb81e4469b2c
branches:  
changeset: 1254:cb81e4469b2c
user:      Artem S. Povalyukhin <artem.povaluhin at gmail.com>
date:      Sat Nov 23 12:52:06 2019 +0300
description:
Pass unprintable values to JSON.stringify() replacer function.

This closes #257 issue on Github.

diffstat:

 src/njs_json.c           |  14 +++++---------
 src/test/njs_unit_test.c |   4 ++++
 2 files changed, 9 insertions(+), 9 deletions(-)

diffs (45 lines):

diff -r 4367a0615234 -r cb81e4469b2c src/njs_json.c
--- a/src/njs_json.c	Sat Nov 23 01:02:04 2019 +0300
+++ b/src/njs_json.c	Sat Nov 23 12:52:06 2019 +0300
@@ -1211,14 +1211,6 @@ njs_json_stringify_iterator(njs_vm_t *vm
                 return ret;
             }
 
-            if (njs_is_undefined(value)
-                || njs_is_symbol(value)
-                || njs_is_function(value)
-                || !njs_is_valid(value))
-            {
-                break;
-            }
-
             ret = njs_json_stringify_to_json(stringify, state, key, value);
             if (njs_slow_path(ret != NJS_OK)) {
                 return ret;
@@ -1229,7 +1221,11 @@ njs_json_stringify_iterator(njs_vm_t *vm
                 return ret;
             }
 
-            if (njs_is_undefined(value)) {
+            if (njs_is_undefined(value)
+                || njs_is_symbol(value)
+                || njs_is_function(value)
+                || !njs_is_valid(value))
+            {
                 break;
             }
 
diff -r 4367a0615234 -r cb81e4469b2c src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Sat Nov 23 01:02:04 2019 +0300
+++ b/src/test/njs_unit_test.c	Sat Nov 23 12:52:06 2019 +0300
@@ -14562,6 +14562,10 @@ static njs_unit_test_t  njs_test[] =
                  "JSON.stringify(objs)"),
       njs_str("[{\"\":{\"a\":1}},{\"a\":1}]") },
 
+    { njs_str("JSON.stringify({a: () => 1, b: Symbol(), c: undefined},"
+                             "(k, v) => k.length ? String(v) : v)"),
+      njs_str("{\"a\":\"[object Function]\",\"b\":\"Symbol()\",\"c\":\"undefined\"}") },
+
     { njs_str("var a = []; a[0] = a; JSON.stringify(a)"),
       njs_str("TypeError: Nested too deep or a cyclic structure") },
 


More information about the nginx-devel mailing list