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