diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index fed29efe8e..be29c7b82a 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -270,6 +270,7 @@ set(SOURCES HighResolutionTime/CoarsenTime.cpp HighResolutionTime/Performance.cpp Infra/ByteSequences.cpp + Infra/JSON.cpp IntersectionObserver/IntersectionObserver.cpp Layout/BlockContainer.cpp Layout/BlockFormattingContext.cpp diff --git a/Userland/Libraries/LibWeb/Infra/JSON.cpp b/Userland/Libraries/LibWeb/Infra/JSON.cpp new file mode 100644 index 0000000000..279e667162 --- /dev/null +++ b/Userland/Libraries/LibWeb/Infra/JSON.cpp @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2022, Linus Groh + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include + +namespace Web::Infra { + +// https://infra.spec.whatwg.org/#parse-a-json-string-to-a-javascript-value +WebIDL::ExceptionOr parse_json_string_to_javascript_value(JS::VM& vm, StringView string) +{ + auto& realm = *vm.current_realm(); + + // 1. Return ? Call(%JSON.parse%, undefined, « string »). + return TRY(JS::call(vm, realm.intrinsics().json_parse_function(), JS::js_undefined(), JS::js_string(vm, string))); +} + +// https://infra.spec.whatwg.org/#parse-json-bytes-to-a-javascript-value +WebIDL::ExceptionOr parse_json_bytes_to_javascript_value(JS::VM& vm, ReadonlyBytes bytes) +{ + // 1. Let string be the result of running UTF-8 decode on bytes. [ENCODING] + auto string = StringView { bytes }; + + // 2. Return the result of parsing a JSON string to an Infra value given string. + return parse_json_string_to_javascript_value(vm, string); +} + +// https://infra.spec.whatwg.org/#serialize-a-javascript-value-to-a-json-string +WebIDL::ExceptionOr serialize_javascript_value_to_json_string(JS::VM& vm, JS::Value value) +{ + auto& realm = *vm.current_realm(); + + // 1. Let result be ? Call(%JSON.stringify%, undefined, « value »). + auto result = TRY(JS::call(vm, realm.intrinsics().json_stringify_function(), JS::js_undefined(), value)); + + // 2. If result is undefined, then throw a TypeError. + if (result.is_undefined()) + return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Result of stringifying value must not be undefined"sv }; + + // 3. Assert: result is a string. + VERIFY(result.is_string()); + + // 4. Return result. + return result.as_string().string(); +} + +// https://infra.spec.whatwg.org/#serialize-a-javascript-value-to-json-bytes +WebIDL::ExceptionOr serialize_javascript_value_to_json_bytes(JS::VM& vm, JS::Value value) +{ + auto& realm = *vm.current_realm(); + auto& global_object = realm.global_object(); + + // 1. Let string be the result of serializing a JavaScript value to a JSON string given value. + auto string = TRY(serialize_javascript_value_to_json_string(vm, value)); + + // 2. Return the result of running UTF-8 encode on string. [ENCODING] + return TRY_OR_RETURN_OOM(global_object, ByteBuffer::copy(string.bytes())); +} + +} diff --git a/Userland/Libraries/LibWeb/Infra/JSON.h b/Userland/Libraries/LibWeb/Infra/JSON.h new file mode 100644 index 0000000000..a2d128238a --- /dev/null +++ b/Userland/Libraries/LibWeb/Infra/JSON.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2022, Linus Groh + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include + +namespace Web::Infra { + +WebIDL::ExceptionOr parse_json_string_to_javascript_value(JS::VM&, StringView); +WebIDL::ExceptionOr parse_json_bytes_to_javascript_value(JS::VM&, ReadonlyBytes); +WebIDL::ExceptionOr serialize_javascript_value_to_json_string(JS::VM&, JS::Value); +WebIDL::ExceptionOr serialize_javascript_value_to_json_bytes(JS::VM&, JS::Value); + +}