diff --git a/Inc/Json_Utilities.hpp b/Inc/Json_Utilities.hpp new file mode 100644 index 0000000..aca8ba7 --- /dev/null +++ b/Inc/Json_Utilities.hpp @@ -0,0 +1,156 @@ +#ifndef __JSON_UTILITIES_HPP__ +#define __JSON_UTILITIES_HPP__ + +extern "C" +{ +#include "stdio.h" +#include "stdlib.h" +#include "stdint.h" +} + +#include "vector" +#include "string" + +class Json_Pair; // Json 键值对 +class Json_Arry; // Json 数组 元素必须是值 +class Json_Object; // Json 对象 对象内必须是键值对序列 +class Json_Value; // Json 值(可以是数组、对象、字符串、浮点数值和整数值、逻辑值、NULL) + +// Json 数组 +class Json_Arry +{ +private: + // 元素列表 + std::vector private_element_list; + +public: + // 默认构造 + Json_Arry() = default; + // 列表构造 + Json_Arry(std::initializer_list init_list) : private_element_list(init_list) {}; + Json_Arry(std::vector init_list) : private_element_list(init_list) {}; + + // 深拷贝 + Json_Arry &operator=(const Json_Arry &another); + // 获取元素大小 + size_t size(void); + // 数组索引重载 + Json_Value operator[](size_t index); + + // 是否为空 + bool isNull(void); +}; + +// Json 值定义 +class Json_Value +{ +public: + // 类型枚举 + typedef enum + { + TYPE_NULL, // 空值 + TYPE_STR, // 字符串 + TYPE_ARRY, // 数组 + TYPE_OBJ, // 对象 + TYPE_NUM_INTEGER, // 整数常数 + TYPE_NUM_FLOAT, // 浮点常数 + TYPE_BOOLEAN, // 布尔逻辑值 + } Type; + +protected: + Type private_type = TYPE_NULL; // 类型枚举 + void *p_mem = nullptr; // 数据内存 +public: + // 只读引用 + const Type &type = this->private_type; + + // 默认构造 + Json_Value() = default; + + // 值构造 + Json_Value(std::string str); + Json_Value(const char *str) : Json_Value(std::string(str)) {}; + Json_Value(Json_Arry arry); + Json_Value(std::initializer_list init_list) : Json_Value(Json_Arry(init_list)) {}; + Json_Value(Json_Object object); + Json_Value(int64_t num_integer); + Json_Value(int num_integer) : Json_Value((int64_t)num_integer) {}; + Json_Value(double num_float); + Json_Value(bool boolean); + // 浅拷贝构造 + Json_Value(const Json_Value &temp); + + // 深拷贝 + Json_Value &operator=(const Json_Value &another); + + // 析构函数 + ~Json_Value(); + + // 获取值 + std::string asString(void); + Json_Arry asArry(void); + Json_Object asObject(void); + int64_t asInteger(void); + double asFloat(void); + bool asBoolean(void); + + // 是否为空 + bool isNull(void); +}; + +// Json 键值对定义 +class Json_Pair +{ +private: + bool Null_Flag = true; // 空判定 + std::string private_key_str; // 键 + Json_Value private_value; // 值 +public: + // 只读引用 + std::string &key = this->private_key_str; + Json_Value &value = this->private_value; + + // 默认构造 + Json_Pair() : Null_Flag(true) {}; + // 必要构造 + Json_Pair(std::string key, Json_Value value) : Null_Flag(false), private_key_str(key), private_value(value) {}; + // 浅拷贝构造 + Json_Pair(const Json_Pair &temp); + + // 深拷贝 + Json_Pair &operator=(const Json_Pair &another); + + // 空判定 + bool isNull(); +}; + +// Json 对象 +class Json_Object +{ +private: + // 元素列表 + std::vector private_element_list; + +public: + // 默认构造 + Json_Object() = default; + // 列表构造 + Json_Object(std::initializer_list init_list) : private_element_list(init_list) {}; + Json_Object(std::vector init_list) : private_element_list(init_list) {}; + + // 深拷贝 + Json_Object &operator=(const Json_Object &another); + + // 获取元素大小 + size_t size(void); + // 数组索引重载 + Json_Pair operator[](size_t index); + + // 由Key 查找值 + Json_Value get(std::string key); + + // 是否为空 + bool isNull(void); +}; + +#endif \ No newline at end of file diff --git a/Src/Json_Utilities.cpp b/Src/Json_Utilities.cpp new file mode 100644 index 0000000..f7e8e8c --- /dev/null +++ b/Src/Json_Utilities.cpp @@ -0,0 +1,305 @@ +#include "Json_Utilities.hpp" + +// std::string 构造 +Json_Value::Json_Value(std::string str) : private_type(TYPE_STR), p_mem(malloc(sizeof(str))) +{ + if (p_mem != nullptr) + new (p_mem) std::string(str); + else + this->private_type = TYPE_NULL; +} + +// Json_Arry 构造 +Json_Value::Json_Value(Json_Arry arry) : private_type(TYPE_ARRY), p_mem(malloc(sizeof(arry))) +{ + if (p_mem != nullptr) + new (p_mem) Json_Arry(arry); + else + this->private_type = TYPE_NULL; +} + +// Json_Object 构造 +Json_Value::Json_Value(Json_Object object) : private_type(TYPE_OBJ), p_mem(malloc(sizeof(object))) +{ + if (p_mem != nullptr) + new (p_mem) Json_Object(object); + else + this->private_type = TYPE_NULL; +} + +// 整数值 构造 +Json_Value::Json_Value(int64_t num_integer) : private_type(TYPE_NUM_INTEGER), p_mem(malloc(sizeof(num_integer))) +{ + if (p_mem != nullptr) + *(decltype(num_integer) *)p_mem = num_integer; + else + this->private_type = TYPE_NULL; +} + +// 浮点值 构造 +Json_Value::Json_Value(double num_float) : private_type(TYPE_NUM_FLOAT), p_mem(malloc(sizeof(num_float))) +{ + if (p_mem != nullptr) + *(decltype(num_float) *)p_mem = num_float; + else + this->private_type = TYPE_NULL; +} + +// Boolean 逻辑值构造 +Json_Value::Json_Value(bool boolean) : private_type(TYPE_BOOLEAN), p_mem(malloc(sizeof(boolean))) +{ + if (p_mem != nullptr) + *(decltype(boolean) *)p_mem = boolean; + else + this->private_type = TYPE_NULL; +} + +// 浅拷贝构造 +Json_Value::Json_Value(const Json_Value &temp) : private_type(TYPE_NULL), p_mem(nullptr) +{ + // 执行深拷贝 + *this = temp; +} + +// 深拷贝 +Json_Value &Json_Value::operator=(const Json_Value &another) +{ + if (this != &another) + { + // 先析构释放内存 + this->~Json_Value(); + + // 重新赋值 + this->private_type = another.type; + switch (another.type) + { + case TYPE_STR: + this->p_mem = malloc(sizeof(std::string)); + if (this->p_mem != nullptr) + new (this->p_mem) std::string(*((std::string *)another.p_mem)); + break; + case TYPE_ARRY: + this->p_mem = malloc(sizeof(Json_Arry)); + if (this->p_mem != nullptr) + new (this->p_mem) Json_Arry(*((Json_Arry *)another.p_mem)); + break; + case TYPE_OBJ: + this->p_mem = malloc(sizeof(Json_Object)); + if (this->p_mem != nullptr) + new (this->p_mem) Json_Object(*((Json_Object *)another.p_mem)); + break; + case TYPE_NUM_INTEGER: + this->p_mem = malloc(sizeof(int64_t)); + if (this->p_mem != nullptr) + *((int64_t *)this->p_mem) = *((int64_t *)another.p_mem); + break; + case TYPE_NUM_FLOAT: + this->p_mem = malloc(sizeof(double)); + if (this->p_mem != nullptr) + *((double *)this->p_mem) = *((double *)another.p_mem); + break; + case TYPE_BOOLEAN: + this->p_mem = malloc(sizeof(bool)); + if (this->p_mem != nullptr) + *((bool *)this->p_mem) = *((bool *)another.p_mem); + break; + default: + this->p_mem = nullptr; + } + } + + return *this; +} + +// 析构 +Json_Value::~Json_Value() +{ + this->private_type = TYPE_NULL; + + if (this->p_mem != nullptr) + { + // 对象析构 + switch (this->type) + { + case TYPE_STR: + ((std::string *)p_mem)->~basic_string(); + break; + case TYPE_ARRY: + ((Json_Arry *)p_mem)->~Json_Arry(); + break; + case TYPE_OBJ: + ((Json_Object *)p_mem)->~Json_Object(); + break; + default: + break; + }; + + // 释放内存 + free(p_mem); + } +} + +// 作为直接值获取 +std::string Json_Value::asString(void) +{ + if (this->type == TYPE_STR) + return *(std::string *)this->p_mem; + else + return std::string(""); +} + +// 作为 Json_Arry 获取 +Json_Arry Json_Value::asArry(void) +{ + if (this->type == TYPE_ARRY) + return *(Json_Arry *)this->p_mem; + else + return Json_Arry(); +} + +// 作为 Json_Object 获取 +Json_Object Json_Value::asObject(void) +{ + if (this->type == TYPE_OBJ) + return *(Json_Object *)this->p_mem; + else + return Json_Object(); +} + +// 作为 int64 获取 +int64_t Json_Value::asInteger(void) +{ + if (this->type == TYPE_NUM_INTEGER) + return *(int64_t *)this->p_mem; + else + return 0; +} + +// 作为 double 获取 +double Json_Value::asFloat(void) +{ + if (this->type == TYPE_NUM_FLOAT) + return *(double *)this->p_mem; + else + return 0; +} + +// 作为 Boolean 逻辑值获取 +bool Json_Value::asBoolean(void) +{ + if (this->type == TYPE_BOOLEAN) + return *(bool *)this->p_mem; + else + return 0; +} + +// 是否为空 +bool Json_Value::isNull(void) +{ + return (this->private_type == TYPE_NULL); +} + +// ================================================================================================= + +// 浅拷贝构造 +Json_Pair::Json_Pair(const Json_Pair &temp) +{ + *this = temp; +} + +// 深拷贝 +Json_Pair &Json_Pair::operator=(const Json_Pair &another) +{ + if (this != &another) + { + this->Null_Flag = another.Null_Flag; + this->private_key_str = another.private_key_str; + this->private_value = another.private_value; + } + + return *this; +} + +// 空判定 +bool Json_Pair::isNull() +{ + return this->Null_Flag; +} + +// ================================================================================================= + +// 深拷贝 +Json_Arry &Json_Arry::operator=(const Json_Arry &another) +{ + if (this != &another) + { + this->private_element_list = another.private_element_list; + } + + return *this; +} + +// 获取元素大小 +size_t Json_Arry::size(void) +{ + return this->private_element_list.size(); +} + +// 数组索引重载 +Json_Value Json_Arry::operator[](size_t index) +{ + if (index < this->private_element_list.size()) + return this->private_element_list[index]; + else + return Json_Value(); +} + +// 是否为空 +bool Json_Arry::isNull(void) +{ + return (this->private_element_list.size() == 0); +} + +// ================================================================================================= + +// 深拷贝 +Json_Object &Json_Object::operator=(const Json_Object &another) +{ + if (this != &another) + { + this->private_element_list = another.private_element_list; + } + + return *this; +} + +// 获取元素大小 +size_t Json_Object::size(void) +{ + return this->private_element_list.size(); +} + +// 数组索引重载 +Json_Pair Json_Object::operator[](size_t index) +{ + if (index < this->private_element_list.size()) + return this->private_element_list[index]; + else + return Json_Pair(); +} + +// 由Key 查找值 +Json_Value Json_Object::get(std::string key) +{ + for (auto pair : this->private_element_list) + if (pair.key == key) + return pair.value; + + return Json_Value(); +} + +// 是否为空 +bool Json_Object::isNull(void) +{ + return (this->private_element_list.size() == 0); +} \ No newline at end of file