summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime/JSONObject.h
blob: 591d9bc8015e9133e0e56717a674c251752556e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
 * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <LibJS/Runtime/Object.h>

namespace JS {

class JSONObject final : public Object {
    JS_OBJECT(JSONObject, Object);

public:
    explicit JSONObject(GlobalObject&);
    virtual void initialize(GlobalObject&) override;
    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);

    static Value parse_json_value(GlobalObject&, const JsonValue&);

private:
    struct StringifyState {
        FunctionObject* replacer_function { nullptr };
        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&);
    static String quote_json_string(String);

    // Parse helpers
    static Object* parse_json_object(GlobalObject&, const JsonObject&);
    static Array* parse_json_array(GlobalObject&, const JsonArray&);
    static Value internalize_json_property(GlobalObject&, Object* holder, PropertyName const& name, FunctionObject& reviver);

    JS_DECLARE_NATIVE_FUNCTION(stringify);
    JS_DECLARE_NATIVE_FUNCTION(parse);
};

}