]> git.kaiwu.me - njs.git/commitdiff
Pass unprintable values to JSON.stringify() replacer function.
authorArtem S. Povalyukhin <artem.povaluhin@gmail.com>
Sat, 23 Nov 2019 09:52:06 +0000 (12:52 +0300)
committerArtem S. Povalyukhin <artem.povaluhin@gmail.com>
Sat, 23 Nov 2019 09:52:06 +0000 (12:52 +0300)
This closes #257 issue on Github.

src/njs_json.c
src/test/njs_unit_test.c

index 97886f8320884bf85fcb8e38994b23d60fcbc7be..78ed12b4a8c6dd254d83a866d659bf8f44d903b7 100644 (file)
@@ -1211,14 +1211,6 @@ njs_json_stringify_iterator(njs_vm_t *vm, njs_json_stringify_t *stringify,
                 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, njs_json_stringify_t *stringify,
                 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;
             }
 
index c1e843e7fc8b4f65731b80fab79577ebba9e63a0..32c8c45f1e7d251f103f7d6c475b133d1c1a6c12 100644 (file)
@@ -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") },