summaryrefslogtreecommitdiff
path: root/AK/JsonObject.h
blob: a21e9f03c3c6b6e9d787770764e00aec500f956f (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#pragma once

#include <AK/AKString.h>
#include <AK/HashMap.h>
#include <AK/JsonValue.h>

namespace AK {

class JsonObject {
public:
    JsonObject() { }
    ~JsonObject() {}

    JsonObject(const JsonObject& other)
        : m_members(other.m_members)
    {
    }

    JsonObject(JsonObject&& other)
        : m_members(move(other.m_members))
    {
    }

    JsonObject& operator=(const JsonObject& other)
    {
        if (this != &other)
            m_members = other.m_members;
        return *this;
    }

    JsonObject& operator=(JsonObject&& other)
    {
        if (this != &other)
            m_members = move(other.m_members);
        return *this;
    }

    int size() const { return m_members.size(); }
    bool is_empty() const { return m_members.is_empty(); }

    JsonValue get(const String& key) const
    {
        auto it = m_members.find(key);
        if (it == m_members.end())
            return JsonValue(JsonValue::Type::Undefined);
        return (*it).value;
    }

    void set(const String& key, JsonValue&& value)
    {
        m_members.set(key, move(value));
    }

    void set(const String& key, const JsonValue& value)
    {
        m_members.set(key, JsonValue(value));
    }

    template<typename Callback>
    void for_each_member(Callback callback) const
    {
        for (auto& it : m_members)
            callback(it.key, it.value);
    }

    String serialized() const;
    void serialize(StringBuilder&) const;

private:
    HashMap<String, JsonValue> m_members;
};

}

using AK::JsonObject;