DLT_Splitter/Inc/Json_Utilities.hpp

184 lines
4.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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);
/**
* @brief 解析Json文件
* @param path_to_file 文件路径串
* @param pJson_Value Value 指针
* @return std::string 错误值
*/
std::string JsonFile_Parse(const char *path_to_file, Json_Value *pJson_Value);
/**
* @brief Json对象保存
* @param path_to_file 文件路径串
* @param value Json Value
* @param enable_pretty_print 启用美观打印
* @return std::string 错误值
*/
std::string Json_Export(const char *path_to_file, Json_Value value, bool enable_pretty_print = true);
// Json 数组
class Json_Arry
{
private:
// 元素列表
std::vector<Json_Value> private_element_list;
public:
// 只读引用
const std::vector<Json_Value> &element_list = this->private_element_list;
// 默认构造
Json_Arry() = default;
// 列表构造
Json_Arry(std::initializer_list<Json_Value> init_list) : private_element_list(init_list) {};
Json_Arry(std::vector<Json_Value> init_list) : private_element_list(init_list) {};
// 深拷贝
Json_Arry &operator=(const Json_Arry &another);
// 获取元素大小
size_t size(void) const;
// 数组索引重载
Json_Value operator[](size_t index) const;
// 是否为空
bool isNull(void) const;
};
// 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<Json_Value> 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) const;
Json_Arry asArry(void) const;
Json_Object asObject(void) const;
int64_t asInteger(void) const;
double asFloat(void) const;
bool asBoolean(void) const;
// 是否为空
bool isNull(void) const;
};
// 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() const;
};
// Json 对象
class Json_Object
{
private:
// 元素列表
std::vector<Json_Pair> private_element_list;
public:
// 只读引用
std::vector<Json_Pair> const &element_list = this->private_element_list;
// 默认构造
Json_Object() = default;
// 列表构造
Json_Object(std::initializer_list<Json_Pair> init_list) : private_element_list(init_list) {};
Json_Object(std::vector<Json_Pair> init_list) : private_element_list(init_list) {};
// 深拷贝
Json_Object &operator=(const Json_Object &another);
// 获取元素大小
size_t size(void) const;
// 数组索引重载
Json_Pair operator[](size_t index) const;
// 由Key 查找值
Json_Value get(std::string key) const;
// 是否为空
bool isNull(void) const;
};
#endif