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 {
|
|
|
|
|
|
|
|
class JSONObject final : public Object {
|
2020-06-21 15:14:02 +02:00
|
|
|
JS_OBJECT(JSONObject, Object);
|
|
|
|
|
2020-06-10 11:01:00 -07:00
|
|
|
public:
|
2020-06-20 17:11:11 +02:00
|
|
|
explicit JSONObject(GlobalObject&);
|
2020-07-22 17:50:18 +02:00
|
|
|
virtual void initialize(GlobalObject&) override;
|
2020-06-10 11:01:00 -07:00
|
|
|
virtual ~JSONObject() override;
|
|
|
|
|
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.
|
2020-09-27 18:52:42 +02:00
|
|
|
static String stringify_impl(GlobalObject&, Value value, Value replacer, Value space);
|
2020-07-04 11:37:50 -07:00
|
|
|
|
2020-06-10 11:01:00 -07:00
|
|
|
private:
|
|
|
|
struct StringifyState {
|
2021-06-27 21:48:34 +02:00
|
|
|
FunctionObject* replacer_function { nullptr };
|
2020-06-10 11:01:00 -07:00
|
|
|
HashTable<Object*> seen_objects;
|
|
|
|
String indent { String::empty() };
|
|
|
|
String gap;
|
|
|
|
Optional<Vector<String>> property_list;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Stringify helpers
|
2020-09-27 18:52:42 +02:00
|
|
|
static String serialize_json_property(GlobalObject&, StringifyState&, const PropertyName& key, Object* holder);
|
|
|
|
static String serialize_json_object(GlobalObject&, StringifyState&, Object&);
|
|
|
|
static String serialize_json_array(GlobalObject&, StringifyState&, Object&);
|
2020-06-10 11:01:00 -07:00
|
|
|
static String quote_json_string(String);
|
|
|
|
|
2020-06-10 23:30:36 -07:00
|
|
|
// Parse helpers
|
2020-09-27 18:52:42 +02:00
|
|
|
static Object* parse_json_object(GlobalObject&, const JsonObject&);
|
|
|
|
static Array* parse_json_array(GlobalObject&, const JsonArray&);
|
|
|
|
static Value parse_json_value(GlobalObject&, const JsonValue&);
|
2021-06-27 21:48:34 +02:00
|
|
|
static Value internalize_json_property(GlobalObject&, Object* holder, PropertyName const& name, FunctionObject& reviver);
|
2020-06-10 23:30:36 -07:00
|
|
|
|
2020-06-20 13:55:34 +02:00
|
|
|
JS_DECLARE_NATIVE_FUNCTION(stringify);
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(parse);
|
2020-06-10 11:01:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|