mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 04:07:45 +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
53
AK/JsonValue.h
Normal file
53
AK/JsonValue.h
Normal file
|
@ -0,0 +1,53 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/AKString.h>
|
||||
|
||||
class JsonArray;
|
||||
class JsonObject;
|
||||
|
||||
class JsonValue {
|
||||
public:
|
||||
enum class Type {
|
||||
Undefined,
|
||||
Null,
|
||||
Int,
|
||||
Double,
|
||||
Bool,
|
||||
String,
|
||||
Array,
|
||||
Object,
|
||||
};
|
||||
|
||||
explicit JsonValue(Type = Type::Null);
|
||||
~JsonValue() { clear(); }
|
||||
|
||||
JsonValue(const JsonValue&);
|
||||
JsonValue(JsonValue&&);
|
||||
|
||||
JsonValue& operator=(const JsonValue&);
|
||||
JsonValue& operator=(JsonValue&&);
|
||||
|
||||
JsonValue(int);
|
||||
JsonValue(double);
|
||||
JsonValue(bool);
|
||||
JsonValue(const String&);
|
||||
JsonValue(const JsonArray&);
|
||||
JsonValue(const JsonObject&);
|
||||
|
||||
String to_string() const;
|
||||
|
||||
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;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue