2020-06-10 18:01:00 +00:00
|
|
|
/*
|
2021-04-22 23:53:07 +00:00
|
|
|
* Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
|
2020-06-10 18:01:00 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-06-10 18:01:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-06-20 14:56:01 +00:00
|
|
|
#include <LibJS/Runtime/Object.h>
|
|
|
|
|
2020-06-10 18:01:00 +00:00
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
class JSONObject final : public Object {
|
2020-06-21 13:14:02 +00:00
|
|
|
JS_OBJECT(JSONObject, Object);
|
2024-11-14 15:01:23 +00:00
|
|
|
GC_DECLARE_ALLOCATOR(JSONObject);
|
2020-06-21 13:14:02 +00:00
|
|
|
|
2020-06-10 18:01:00 +00:00
|
|
|
public:
|
2023-08-07 06:41:28 +00:00
|
|
|
virtual void initialize(Realm&) override;
|
2022-03-14 16:25:06 +00:00
|
|
|
virtual ~JSONObject() override = default;
|
2020-06-10 18:01:00 +00:00
|
|
|
|
2020-07-04 18:37:50 +00:00
|
|
|
// 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.
|
2023-12-16 14:19:34 +00:00
|
|
|
static ThrowCompletionOr<Optional<ByteString>> stringify_impl(VM&, Value value, Value replacer, Value space);
|
2020-07-04 18:37:50 +00:00
|
|
|
|
2022-08-21 16:41:49 +00:00
|
|
|
static Value parse_json_value(VM&, JsonValue const&);
|
2021-09-16 19:48:07 +00:00
|
|
|
|
2020-06-10 18:01:00 +00:00
|
|
|
private:
|
2022-08-28 21:51:28 +00:00
|
|
|
explicit JSONObject(Realm&);
|
|
|
|
|
2020-06-10 18:01:00 +00:00
|
|
|
struct StringifyState {
|
2024-11-14 15:01:23 +00:00
|
|
|
GC::Ptr<FunctionObject> replacer_function;
|
|
|
|
HashTable<GC::Ptr<Object>> seen_objects;
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString indent { ByteString::empty() };
|
|
|
|
ByteString gap;
|
|
|
|
Optional<Vector<ByteString>> property_list;
|
2020-06-10 18:01:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Stringify helpers
|
2023-12-16 14:19:34 +00:00
|
|
|
static ThrowCompletionOr<Optional<ByteString>> serialize_json_property(VM&, StringifyState&, PropertyKey const& key, Object* holder);
|
|
|
|
static ThrowCompletionOr<ByteString> serialize_json_object(VM&, StringifyState&, Object&);
|
|
|
|
static ThrowCompletionOr<ByteString> serialize_json_array(VM&, StringifyState&, Object&);
|
|
|
|
static ByteString quote_json_string(ByteString);
|
2020-06-10 18:01:00 +00:00
|
|
|
|
2020-06-11 06:30:36 +00:00
|
|
|
// Parse helpers
|
2022-08-21 16:41:49 +00:00
|
|
|
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);
|
2020-06-11 06:30:36 +00:00
|
|
|
|
2021-10-28 21:27:25 +00:00
|
|
|
JS_DECLARE_NATIVE_FUNCTION(stringify);
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(parse);
|
2020-06-10 18:01:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|