ladybird/Userland/Libraries/LibJS/Runtime/JSONObject.h

51 lines
1.6 KiB
C
Raw Normal View History

2020-06-10 11:01:00 -07:00
/*
* Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
2020-06-10 11:01:00 -07:00
*
* SPDX-License-Identifier: BSD-2-Clause
2020-06-10 11:01:00 -07:00
*/
#pragma once
#include <LibJS/Runtime/Object.h>
2020-06-10 11:01:00 -07:00
namespace JS {
class JSONObject final : public Object {
JS_OBJECT(JSONObject, Object);
2020-06-10 11:01:00 -07:00
public:
explicit JSONObject(GlobalObject&);
virtual void initialize(GlobalObject&) override;
2020-06-10 11:01:00 -07:00
virtual ~JSONObject() override;
// 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 String stringify_impl(GlobalObject&, Value value, Value replacer, Value space);
2020-06-10 11:01:00 -07:00
private:
struct StringifyState {
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
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
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, PropertyName const& name, FunctionObject& reviver);
2020-06-10 23:30:36 -07:00
JS_DECLARE_NATIVE_FUNCTION(stringify);
JS_DECLARE_NATIVE_FUNCTION(parse);
2020-06-10 11:01:00 -07:00
};
}