diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-06-17 19:47:35 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-06-17 19:47:35 +0200 |
commit | 04a8fc9bd7d4a3dd31cce39266464bb05ca0bfca (patch) | |
tree | d0e7d113138caffe0f56ef19b4814193cc4cc3ec /AK/JsonValue.h | |
parent | 7ccb84e58efd4b48ec51150a5d8495bad71bbfbc (diff) | |
download | serenity-04a8fc9bd7d4a3dd31cce39266464bb05ca0bfca.zip |
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.
Diffstat (limited to 'AK/JsonValue.h')
-rw-r--r-- | AK/JsonValue.h | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/AK/JsonValue.h b/AK/JsonValue.h new file mode 100644 index 0000000000..5bdfb9a5d7 --- /dev/null +++ b/AK/JsonValue.h @@ -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; +}; |