#ifndef __JSON_UTILITIES_HPP__ #define __JSON_UTILITIES_HPP__ extern "C" { #include "stdint.h" } #include "vector" #include "string" class Json_Pair; // Json 键值对 class Json_Arry; // Json 数组 元素必须是值 class Json_Object; // Json 对象 对象内必须是键值对序列 class Json_Value; // Json 值(可以是数组、对象、字符串、浮点数值和整数值、逻辑值、NULL) /** * @brief Json文件 格式检查 * @param path_to_file 文件路径串 * @return std::string 错误值 */ std::string JsonFile_FormatCheck(const char *path_to_file); // 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