You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
1.2 KiB
C

2 months ago
// 轻量 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);
// 解析多边形/二维点数组: "Filter_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);
#endif // JSON_UTILS_H