2019-06-17 17:47:35 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/AKString.h>
|
|
|
|
|
2019-06-17 19:34:12 +00:00
|
|
|
namespace AK {
|
|
|
|
|
2019-06-17 17:47:35 +00:00
|
|
|
class JsonArray;
|
|
|
|
class JsonObject;
|
2019-06-17 19:34:12 +00:00
|
|
|
class StringBuilder;
|
2019-06-17 17:47:35 +00:00
|
|
|
|
|
|
|
class JsonValue {
|
|
|
|
public:
|
|
|
|
enum class Type {
|
|
|
|
Undefined,
|
|
|
|
Null,
|
|
|
|
Int,
|
|
|
|
Double,
|
|
|
|
Bool,
|
|
|
|
String,
|
|
|
|
Array,
|
|
|
|
Object,
|
|
|
|
};
|
|
|
|
|
2019-06-24 09:25:10 +00:00
|
|
|
static JsonValue from_string(const StringView&);
|
|
|
|
|
2019-06-17 17:47:35 +00:00
|
|
|
explicit JsonValue(Type = Type::Null);
|
|
|
|
~JsonValue() { clear(); }
|
|
|
|
|
|
|
|
JsonValue(const JsonValue&);
|
|
|
|
JsonValue(JsonValue&&);
|
|
|
|
|
|
|
|
JsonValue& operator=(const JsonValue&);
|
|
|
|
JsonValue& operator=(JsonValue&&);
|
|
|
|
|
|
|
|
JsonValue(int);
|
2019-06-18 06:55:58 +00:00
|
|
|
JsonValue(unsigned);
|
2019-06-17 17:47:35 +00:00
|
|
|
JsonValue(double);
|
|
|
|
JsonValue(bool);
|
2019-06-18 07:11:31 +00:00
|
|
|
JsonValue(const char*);
|
2019-06-17 17:47:35 +00:00
|
|
|
JsonValue(const String&);
|
|
|
|
JsonValue(const JsonArray&);
|
|
|
|
JsonValue(const JsonObject&);
|
|
|
|
|
2019-06-18 07:37:47 +00:00
|
|
|
String serialized() const;
|
|
|
|
void serialize(StringBuilder&) const;
|
2019-06-17 17:47:35 +00:00
|
|
|
|
2019-06-18 06:55:58 +00:00
|
|
|
String as_string() const
|
|
|
|
{
|
2019-06-24 10:03:31 +00:00
|
|
|
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;
|
2019-06-18 06:55:58 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 11:08:07 +00:00
|
|
|
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_string() const { return m_type == Type::String; }
|
|
|
|
bool is_int() const { return m_type == Type::Int; }
|
|
|
|
bool is_double() const { return m_type == Type::Double; }
|
|
|
|
bool is_array() const { return m_type == Type::Array; }
|
|
|
|
bool is_object() const { return m_type == Type::Object; }
|
|
|
|
bool is_number() const { return m_type == Type::Int || m_type == Type::Double; }
|
|
|
|
|
|
|
|
dword to_dword(dword default_value = 0) const
|
|
|
|
{
|
|
|
|
if (!is_number())
|
|
|
|
return default_value;
|
|
|
|
if (type() == Type::Int)
|
|
|
|
return (dword)m_value.as_int;
|
|
|
|
return (dword)m_value.as_double;
|
|
|
|
}
|
|
|
|
|
2019-06-17 17:47:35 +00:00
|
|
|
private:
|
|
|
|
void clear();
|
|
|
|
void copy_from(const JsonValue&);
|
|
|
|
|
|
|
|
Type m_type { Type::Undefined };
|
|
|
|
|
|
|
|
union {
|
|
|
|
StringImpl* as_string { nullptr };
|
|
|
|
JsonArray* as_array;
|
|
|
|
JsonObject* as_object;
|
|
|
|
double as_double;
|
|
|
|
int as_int;
|
|
|
|
bool as_bool;
|
|
|
|
} m_value;
|
|
|
|
};
|
2019-06-17 19:34:12 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::JsonValue;
|