2019-06-17 17:47:35 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-09-06 13:34:26 +00:00
|
|
|
#include <AK/String.h>
|
2019-06-17 17:47:35 +00:00
|
|
|
#include <AK/HashMap.h>
|
2019-08-07 19:28:07 +00:00
|
|
|
#include <AK/JsonArray.h>
|
2019-08-27 11:15:30 +00:00
|
|
|
#include <AK/JsonObjectSerializer.h>
|
2019-06-17 17:47:35 +00:00
|
|
|
#include <AK/JsonValue.h>
|
|
|
|
|
2019-06-17 19:34:12 +00:00
|
|
|
namespace AK {
|
|
|
|
|
2019-06-17 17:47:35 +00:00
|
|
|
class JsonObject {
|
|
|
|
public:
|
2019-08-07 19:28:07 +00:00
|
|
|
JsonObject() {}
|
2019-08-04 09:45:16 +00:00
|
|
|
~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;
|
|
|
|
}
|
2019-06-17 17:47:35 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-10-21 12:47:18 +00:00
|
|
|
bool has(const String& key) const
|
|
|
|
{
|
|
|
|
return m_members.contains(key);
|
|
|
|
}
|
|
|
|
|
2019-07-08 11:08:21 +00:00
|
|
|
void set(const String& key, JsonValue&& value)
|
|
|
|
{
|
|
|
|
m_members.set(key, move(value));
|
|
|
|
}
|
|
|
|
|
2019-06-17 17:47:35 +00:00
|
|
|
void set(const String& key, const JsonValue& value)
|
|
|
|
{
|
2019-07-08 11:08:21 +00:00
|
|
|
m_members.set(key, JsonValue(value));
|
2019-06-17 17:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
void for_each_member(Callback callback) const
|
|
|
|
{
|
|
|
|
for (auto& it : m_members)
|
|
|
|
callback(it.key, it.value);
|
|
|
|
}
|
|
|
|
|
2019-08-07 19:28:07 +00:00
|
|
|
template<typename Builder>
|
|
|
|
typename Builder::OutputType serialized() const;
|
|
|
|
|
|
|
|
template<typename Builder>
|
|
|
|
void serialize(Builder&) const;
|
|
|
|
|
|
|
|
String to_string() const { return serialized<StringBuilder>(); }
|
2019-06-17 17:47:35 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
HashMap<String, JsonValue> m_members;
|
|
|
|
};
|
2019-06-17 19:34:12 +00:00
|
|
|
|
2019-08-07 19:28:07 +00:00
|
|
|
template<typename Builder>
|
|
|
|
inline void JsonObject::serialize(Builder& builder) const
|
|
|
|
{
|
2019-08-27 11:15:30 +00:00
|
|
|
JsonObjectSerializer serializer { builder };
|
2019-08-07 19:28:07 +00:00
|
|
|
for_each_member([&](auto& key, auto& value) {
|
2019-08-27 11:15:30 +00:00
|
|
|
serializer.add(key, value);
|
2019-08-07 19:28:07 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Builder>
|
|
|
|
inline typename Builder::OutputType JsonObject::serialized() const
|
|
|
|
{
|
|
|
|
Builder builder;
|
|
|
|
serialize(builder);
|
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Builder>
|
|
|
|
void JsonValue::serialize(Builder& builder) const
|
|
|
|
{
|
|
|
|
switch (m_type) {
|
|
|
|
case Type::String:
|
|
|
|
builder.appendf("\"%s\"", m_value.as_string->characters());
|
|
|
|
break;
|
|
|
|
case Type::Array:
|
|
|
|
m_value.as_array->serialize(builder);
|
|
|
|
break;
|
|
|
|
case Type::Object:
|
|
|
|
m_value.as_object->serialize(builder);
|
|
|
|
break;
|
|
|
|
case Type::Bool:
|
|
|
|
builder.append(m_value.as_bool ? "true" : "false");
|
|
|
|
break;
|
|
|
|
#ifndef KERNEL
|
|
|
|
case Type::Double:
|
|
|
|
builder.appendf("%g", m_value.as_double);
|
|
|
|
break;
|
|
|
|
#endif
|
2019-10-29 15:36:50 +00:00
|
|
|
case Type::Int32:
|
|
|
|
builder.appendf("%d", as_i32());
|
2019-08-07 19:28:07 +00:00
|
|
|
break;
|
2019-10-29 15:36:50 +00:00
|
|
|
case Type::Int64:
|
|
|
|
builder.appendf("%lld", as_i64());
|
|
|
|
break;
|
|
|
|
case Type::UnsignedInt32:
|
|
|
|
builder.appendf("%u", as_u32());
|
|
|
|
break;
|
|
|
|
case Type::UnsignedInt64:
|
|
|
|
builder.appendf("%llu", as_u64());
|
2019-08-07 19:28:07 +00:00
|
|
|
break;
|
|
|
|
case Type::Undefined:
|
|
|
|
builder.append("undefined");
|
|
|
|
break;
|
|
|
|
case Type::Null:
|
|
|
|
builder.append("null");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Builder>
|
|
|
|
typename Builder::OutputType JsonValue::serialized() const
|
|
|
|
{
|
|
|
|
Builder builder;
|
|
|
|
serialize(builder);
|
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
|
2019-06-17 19:34:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
using AK::JsonObject;
|