|
|
|
|
|
// 轻量 JSON 解析工具(仅覆盖本程序所需子集,无第三方依赖)
|
|
|
|
|
|
#ifndef JSON_UTILS_H
|
|
|
|
|
|
#define JSON_UTILS_H
|
|
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
|
|
|
|
// 去除字符串首尾空白(空格/tab/回车/换行)
|
|
|
|
|
|
std::string json_trim(const std::string& s);
|
|
|
|
|
|
|
|
|
|
|
|
// 取 JSON 对象/数组中 "key" 对应的 value 原文。
|
|
|
|
|
|
// 返回子串:字符串(含引号)、数字、true/false、{...}、[...] 均原样返回(不含外层空白)。
|
|
|
|
|
|
// 找不到返回空串(若找到但值为空字符串则返回 "\"\"")。
|
|
|
|
|
|
std::string json_get_raw(const std::string& text, const std::string& key);
|
|
|
|
|
|
|
|
|
|
|
|
// 以下便捷函数从 raw 中解析基础类型,找不到返回 false
|
|
|
|
|
|
bool json_get_string(const std::string& text, const std::string& key, std::string& out);
|
|
|
|
|
|
bool json_get_number(const std::string& text, const std::string& key, double& out);
|
|
|
|
|
|
bool json_get_bool(const std::string& text, const std::string& key, bool& out);
|
|
|
|
|
|
|
|
|
|
|
|
// 解析多边形/二维点数组: "polygon":[[-650,0],[-650,670],[650,670],[650,0]]
|
|
|
|
|
|
// 追加到 out,成功返回解析到的点数
|
|
|
|
|
|
int json_get_point_array(const std::string& text, const std::string& key,
|
|
|
|
|
|
std::vector<std::pair<double, double>>& out);
|
|
|
|
|
|
|
|
|
|
|
|
// ---- 分组两级查找:先取 "group": {...} 对象,再在该对象内按 key 取 value ----
|
|
|
|
|
|
// 用于嵌套分组配置(如 {"radar": {"ip": ...}} 中取 radar 组内的 ip)。
|
|
|
|
|
|
// group 传空串时等价于不带 group 的版本。
|
|
|
|
|
|
std::string json_get_raw(const std::string& text, const std::string& group, const std::string& key);
|
|
|
|
|
|
bool json_get_string(const std::string& text, const std::string& group, const std::string& key, std::string& out);
|
|
|
|
|
|
bool json_get_number(const std::string& text, const std::string& group, const std::string& key, double& out);
|
|
|
|
|
|
bool json_get_bool(const std::string& text, const std::string& group, const std::string& key, bool& out);
|
|
|
|
|
|
int json_get_point_array(const std::string& text, const std::string& group, const std::string& key,
|
|
|
|
|
|
std::vector<std::pair<double, double>>& out);
|
|
|
|
|
|
|
|
|
|
|
|
#endif // JSON_UTILS_H
|