From 223472c57ff5c736c59a5591708da3fc22529adc Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Fri, 16 Apr 2021 13:02:51 +0300 Subject: [PATCH] LibJS: Dont try to serialize symbol-keyed properties As per the specification: "All Symbol-keyed properties will be completely ignored, even when using the replacer function." --- Userland/Libraries/LibJS/Runtime/JSONObject.cpp | 2 ++ .../Libraries/LibJS/Tests/builtins/JSON/JSON.stringify.js | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/Userland/Libraries/LibJS/Runtime/JSONObject.cpp b/Userland/Libraries/LibJS/Runtime/JSONObject.cpp index fa7d04a53c..e6dbed1b0f 100644 --- a/Userland/Libraries/LibJS/Runtime/JSONObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/JSONObject.cpp @@ -215,6 +215,8 @@ String JSONObject::serialize_json_object(GlobalObject& global_object, StringifyS Vector property_strings; auto process_property = [&](const PropertyName& key) { + if (key.is_symbol()) + return; auto serialized_property_string = serialize_json_property(global_object, state, key, &object); if (vm.exception()) return; diff --git a/Userland/Libraries/LibJS/Tests/builtins/JSON/JSON.stringify.js b/Userland/Libraries/LibJS/Tests/builtins/JSON/JSON.stringify.js index 1e5c174312..ae4398d9ad 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/JSON/JSON.stringify.js +++ b/Userland/Libraries/LibJS/Tests/builtins/JSON/JSON.stringify.js @@ -43,6 +43,13 @@ describe("correct behavior", () => { Object.defineProperty(o, "baz", { value: "qux", enumerable: false }); expect(JSON.stringify(o)).toBe('{"foo":"bar"}'); }); + + test("ignores symbol properties", () => { + let o = { foo: "bar" }; + let sym = Symbol("baz"); + o[sym] = "qux"; + expect(JSON.stringify(o)).toBe('{"foo":"bar"}'); + }); }); describe("errors", () => {