mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 01:47:34 +00:00
AK: Add some classes for JSON encoding.
This patch adds JsonValue, JsonObject and JsonArray. You can use them to build up a JsonObject and then serialize it to a string via to_string(). This patch only implements encoding, no decoding yet.
This commit is contained in:
parent
7ccb84e58e
commit
04a8fc9bd7
7 changed files with 301 additions and 0 deletions
45
AK/JsonObject.h
Normal file
45
AK/JsonObject.h
Normal file
|
@ -0,0 +1,45 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/AKString.h>
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/JsonValue.h>
|
||||
|
||||
class JsonObject {
|
||||
public:
|
||||
JsonObject() { }
|
||||
~JsonObject() { }
|
||||
|
||||
JsonObject(const JsonObject& other)
|
||||
{
|
||||
for (auto& it : other.m_members)
|
||||
m_members.set(it.key, it.value);
|
||||
}
|
||||
|
||||
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, const JsonValue& value)
|
||||
{
|
||||
m_members.set(key, value);
|
||||
}
|
||||
|
||||
template<typename Callback>
|
||||
void for_each_member(Callback callback) const
|
||||
{
|
||||
for (auto& it : m_members)
|
||||
callback(it.key, it.value);
|
||||
}
|
||||
|
||||
String to_string() const;
|
||||
|
||||
private:
|
||||
HashMap<String, JsonValue> m_members;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue