summaryrefslogtreecommitdiff
path: root/AK/JsonValue.h
blob: 59415fdac8bf0cbd4ecf7b9e672da6f21dd81110 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#pragma once

#include <AK/AKString.h>
#include <AK/IPv4Address.h>
#include <AK/Optional.h>
#include <AK/StringBuilder.h>

namespace AK {

class JsonArray;
class JsonObject;
class StringBuilder;

class JsonValue {
public:
    enum class Type {
        Undefined,
        Null,
        Int,
        UnsignedInt,
#ifndef KERNEL
        Double,
#endif
        Bool,
        String,
        Array,
        Object,
    };

    static JsonValue from_string(const StringView&);

    explicit JsonValue(Type = Type::Null);
    ~JsonValue() { clear(); }

    JsonValue(const JsonValue&);
    JsonValue(JsonValue&&);

    JsonValue& operator=(const JsonValue&);
    JsonValue& operator=(JsonValue&&);

    JsonValue(int);
    JsonValue(unsigned);
    JsonValue(long unsigned);
#ifndef KERNEL
    JsonValue(double);
#endif
    JsonValue(bool);
    JsonValue(const char*);
    JsonValue(const String&);
    JsonValue(const IPv4Address&);
    JsonValue(const JsonArray&);
    JsonValue(const JsonObject&);

    JsonValue(JsonArray&&);
    JsonValue(JsonObject&&);

    // FIXME: Implement these
    JsonValue& operator=(JsonArray&&) = delete;
    JsonValue& operator=(JsonObject&&) = delete;

    template<typename Builder>
    typename Builder::OutputType serialized() const;
    template<typename Builder>
    void serialize(Builder&) const;

    String as_string_or(const String& alternative)
    {
        if (is_string())
            return as_string();
        return alternative;
    }

    String to_string() const
    {
        if (is_string())
            return as_string();
        return serialized<StringBuilder>();
    }

    Optional<IPv4Address> to_ipv4_address() const
    {
        if (!is_string())
            return {};
        return IPv4Address::from_string(as_string());
    }

    int to_int(int default_value = 0) const
    {
        if (!is_number())
            return default_value;
#ifndef KERNEL
        if (is_double())
            return (int)as_double();
#endif
        if (is_uint())
            return (int)as_uint();
        return as_int();
    }

    unsigned to_uint(unsigned default_value = 0) const
    {
        if (!is_number())
            return default_value;
#ifndef KERNEL
        if (is_double())
            return (unsigned)as_double();
#endif
        if (is_int())
            return (unsigned)as_int();
        return as_uint();
    }

    bool to_bool(bool default_value = false) const
    {
        if (!is_bool())
            return default_value;
        return as_bool();
    }

    int as_int() const
    {
        ASSERT(is_int());
        return m_value.as_int;
    }

    int as_uint() const
    {
        ASSERT(is_uint());
        return m_value.as_uint;
    }

    int as_bool() const
    {
        ASSERT(is_bool());
        return m_value.as_bool;
    }

    String as_string() const
    {
        ASSERT(is_string());
        return *m_value.as_string;
    }

    const JsonObject& as_object() const
    {
        ASSERT(is_object());
        return *m_value.as_object;
    }

    const JsonArray& as_array() const
    {
        ASSERT(is_array());
        return *m_value.as_array;
    }

#ifndef KERNEL
    double as_double() const
    {
        ASSERT(is_double());
        return m_value.as_double;
    }
#endif

    Type type() const
    {
        return m_type;
    }

    bool is_null() const { return m_type == Type::Null; }
    bool is_undefined() const { return m_type == Type::Undefined; }
    bool is_bool() const { return m_type == Type::Bool; }
    bool is_string() const { return m_type == Type::String; }
    bool is_int() const { return m_type == Type::Int; }
    bool is_uint() const { return m_type == Type::UnsignedInt; }
#ifndef KERNEL
    bool is_double() const
    {
        return m_type == Type::Double;
    }
#endif
    bool is_array() const
    {
        return m_type == Type::Array;
    }
    bool is_object() const { return m_type == Type::Object; }
    bool is_number() const
    {
        if (m_type == Type::Int || m_type == Type::UnsignedInt)
            return true;
#ifdef KERNEL
        return false;
#else
        return m_type == Type::Double;
#endif
    }

    u32 to_u32(u32 default_value = 0) const
    {
        if (!is_number())
            return default_value;
#ifdef KERNEL
        return (u32)m_value.as_int;
#else
        if (type() == Type::Int)
            return (u32)m_value.as_int;
        if (type() == Type::UnsignedInt)
            return m_value.as_uint;
        return (u32)m_value.as_double;
#endif
    }

private:
    void clear();
    void copy_from(const JsonValue&);

    Type m_type { Type::Undefined };

    union {
        StringImpl* as_string { nullptr };
        JsonArray* as_array;
        JsonObject* as_object;
#ifndef KERNEL
        double as_double;
#endif
        int as_int;
        unsigned int as_uint;
        bool as_bool;
    } m_value;
};

}

using AK::JsonValue;