ladybird/Userland/Libraries/LibJS/Runtime/JSONObject.h
Linus Groh ecd163bdf1 LibJS+LibWeb: Replace GlobalObject with Realm in object constructors
No functional changes - we can still very easily get to the global
object via `Realm::global_object()`. This is in preparation of moving
the intrinsics to the realm and no longer having to pass a global
object when allocating any object.
In a few (now, and many more in subsequent commits) places we get a
realm using `GlobalObject::associated_realm()`, this is intended to be
temporary. For example, create() functions will later receive the same
treatment and are passed a realm instead of a global object.
2022-08-23 13:58:30 +01:00

51 lines
1.7 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);
public:
explicit JSONObject(Realm&);
virtual void initialize(GlobalObject&) 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<String> stringify_impl(GlobalObject&, Value value, Value replacer, Value space);
static Value parse_json_value(GlobalObject&, JsonValue const&);
private:
struct StringifyState {
FunctionObject* replacer_function { nullptr };
HashTable<Object*> seen_objects;
String indent { String::empty() };
String gap;
Optional<Vector<String>> property_list;
};
// Stringify helpers
static ThrowCompletionOr<String> serialize_json_property(GlobalObject&, StringifyState&, PropertyKey const& key, Object* holder);
static ThrowCompletionOr<String> serialize_json_object(GlobalObject&, StringifyState&, Object&);
static ThrowCompletionOr<String> serialize_json_array(GlobalObject&, StringifyState&, Object&);
static String quote_json_string(String);
// Parse helpers
static Object* parse_json_object(GlobalObject&, JsonObject const&);
static Array* parse_json_array(GlobalObject&, JsonArray const&);
static ThrowCompletionOr<Value> internalize_json_property(GlobalObject&, Object* holder, PropertyKey const& name, FunctionObject& reviver);
JS_DECLARE_NATIVE_FUNCTION(stringify);
JS_DECLARE_NATIVE_FUNCTION(parse);
};
}