Fix JSON.stringify for non-serializable properties (#3083)

Fixes #3082.

JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai@inf.u-szeged.hu
This commit is contained in:
Dániel Bátyai
2019-09-08 23:49:27 +02:00
committed by GitHub
parent 2933947534
commit 115ad9a41c
2 changed files with 57 additions and 9 deletions
@@ -950,8 +950,8 @@ ecma_builtin_json_serialize_object (ecma_json_stringify_context_t *context_p, /*
/* ecma_builtin_json_serialize_property already appended the result. */
JERRY_ASSERT (ecma_is_value_empty (result));
last_prop = ecma_stringbuilder_get_size (&context_p->result_builder);
ecma_stringbuilder_append_byte (&context_p->result_builder, LIT_CHAR_COMMA);
last_prop = ecma_stringbuilder_get_size (&context_p->result_builder);
}
else
{
@@ -962,15 +962,18 @@ ecma_builtin_json_serialize_object (ecma_json_stringify_context_t *context_p, /*
ecma_value_p = ecma_collection_iterator_next (ecma_value_p);
}
/* Remove the last comma. */
ecma_stringbuilder_revert (&context_p->result_builder, last_prop);
if (last_prop != left_brace && has_gap)
if (last_prop != left_brace)
{
/* We appended at least one element, and have a separator, so must append the stepback. */
ecma_stringbuilder_append_raw (&context_p->result_builder,
ecma_stringbuilder_get_data (&context_p->indent_builder),
stepback_size);
/* Remove the last comma. */
ecma_stringbuilder_revert (&context_p->result_builder, last_prop - 1);
if (has_gap)
{
/* We appended at least one element, and have a separator, so must append the stepback. */
ecma_stringbuilder_append_raw (&context_p->result_builder,
ecma_stringbuilder_get_data (&context_p->indent_builder),
stepback_size);
}
}
ecma_stringbuilder_append_byte (&context_p->result_builder, LIT_CHAR_RIGHT_BRACE);
+45
View File
@@ -0,0 +1,45 @@
// Copyright JS Foundation and other contributors, http://js.foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
var obj = {
a: "str",
b: function() {},
c: true
}
assert (JSON.stringify (obj) === "{\"a\":\"str\",\"c\":true}");
assert (JSON.stringify (obj, null, 2) === "{\n \"a\": \"str\",\n \"c\": true\n}");
var obj = {
f: function() {}
}
assert (JSON.stringify (obj) === "{}");
assert (JSON.stringify (obj, null, 2) === "{}");
var obj = {
f: function() {},
a: null
}
assert (JSON.stringify (obj) === "{\"a\":null}");
assert (JSON.stringify (obj, null, 2) === "{\n \"a\": null\n}");
var obj = {
a: false,
f: function() {}
}
assert (JSON.stringify (obj) === "{\"a\":false}");
assert (JSON.stringify (obj, null, 2) === "{\n \"a\": false\n}");