ladybird/Libraries/LibJS/Runtime/JSONObject.h
aplefull 223c9c91e6
Some checks are pending
CI / Lagom (arm64, Sanitizer_CI, false, macos-15, macOS, Clang) (push) Waiting to run
CI / Lagom (x86_64, Fuzzers_CI, false, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, false, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, true, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (arm64, macos-15, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (x86_64, ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run
LibJS: Implement rawJSON and isRawJSON functions
2025-04-24 09:33:49 -04:00

55 lines
1.8 KiB
C++

/*
* Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Runtime/Object.h>
namespace JS {
class JSONObject final : public Object {
JS_OBJECT(JSONObject, Object);
GC_DECLARE_ALLOCATOR(JSONObject);
public:
virtual void initialize(Realm&) override;
virtual ~JSONObject() override = default;
// The base implementation of stringify is exposed because it is used by
// test-js to communicate between the JS tests and the C++ test runner.
static ThrowCompletionOr<Optional<String>> stringify_impl(VM&, Value value, Value replacer, Value space);
static Value parse_json_value(VM&, JsonValue const&);
private:
explicit JSONObject(Realm&);
struct StringifyState {
GC::Ptr<FunctionObject> replacer_function;
HashTable<GC::Ptr<Object>> seen_objects;
String indent;
String gap;
Optional<Vector<String>> property_list;
};
// Stringify helpers
static ThrowCompletionOr<Optional<String>> serialize_json_property(VM&, StringifyState&, PropertyKey const& key, Object* holder);
static ThrowCompletionOr<String> serialize_json_object(VM&, StringifyState&, Object&);
static ThrowCompletionOr<String> serialize_json_array(VM&, StringifyState&, Object&);
static String quote_json_string(String);
// Parse helpers
static Object* parse_json_object(VM&, JsonObject const&);
static Array* parse_json_array(VM&, JsonArray const&);
static ThrowCompletionOr<Value> internalize_json_property(VM&, Object* holder, PropertyKey const& name, FunctionObject& reviver);
JS_DECLARE_NATIVE_FUNCTION(stringify);
JS_DECLARE_NATIVE_FUNCTION(parse);
JS_DECLARE_NATIVE_FUNCTION(raw_json);
JS_DECLARE_NATIVE_FUNCTION(is_raw_json);
};
}