2020-06-10 15:01:00 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Matthew Olsson <matthewcolsson@gmail.com>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-06-10 15:01:00 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2020-06-20 11:56:01 -03:00
|
|
|
#include <LibJS/Runtime/Object.h>
|
|
|
|
|
|
2020-06-10 15:01:00 -03:00
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
|
|
class JSONObject final : public Object {
|
2020-06-21 10:14:02 -03:00
|
|
|
JS_OBJECT(JSONObject, Object);
|
|
|
|
|
|
2020-06-10 15:01:00 -03:00
|
|
|
public:
|
2020-06-20 12:11:11 -03:00
|
|
|
explicit JSONObject(GlobalObject&);
|
2020-07-22 12:50:18 -03:00
|
|
|
virtual void initialize(GlobalObject&) override;
|
2020-06-10 15:01:00 -03:00
|
|
|
virtual ~JSONObject() override;
|
|
|
|
|
|
2020-07-04 15:37:50 -03: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 13:52:42 -03:00
|
|
|
static String stringify_impl(GlobalObject&, Value value, Value replacer, Value space);
|
2020-07-04 15:37:50 -03:00
|
|
|
|
2020-06-10 15:01:00 -03:00
|
|
|
private:
|
|
|
|
|
struct StringifyState {
|
|
|
|
|
Function* replacer_function { nullptr };
|
|
|
|
|
HashTable<Object*> seen_objects;
|
|
|
|
|
String indent { String::empty() };
|
|
|
|
|
String gap;
|
|
|
|
|
Optional<Vector<String>> property_list;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Stringify helpers
|
2020-09-27 13:52:42 -03: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 15:01:00 -03:00
|
|
|
static String quote_json_string(String);
|
|
|
|
|
|
2020-06-11 03:30:36 -03:00
|
|
|
// Parse helpers
|
2020-09-27 13:52:42 -03: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&);
|
|
|
|
|
static Value internalize_json_property(GlobalObject&, Object* holder, const PropertyName& name, Function& reviver);
|
2020-06-11 03:30:36 -03:00
|
|
|
|
2020-06-20 08:55:34 -03:00
|
|
|
JS_DECLARE_NATIVE_FUNCTION(stringify);
|
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(parse);
|
2020-06-10 15:01:00 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|