实现MQTT协议消息订阅和消息解析流程

This commit is contained in:
lixiaoyuan
2025-09-08 19:34:12 +08:00
parent 566a3b050c
commit e2995eff92
17 changed files with 642 additions and 288 deletions

View File

@@ -51,28 +51,10 @@ using njson = nlohmann::json;
class JSON
{
public:
static bool load(std::string jsonfile, njson& json)
{
std::ifstream ifs(jsonfile);
if (!ifs.is_open()) { return false; }
try { ifs >> json; }
catch (nlohmann::json::parse_error& e) { return false; }
return true;
}
static bool load(std::string jsonfile, njson& json);
static bool parse(std::string jsonstr, njson& json)
{
try
{
if (!jsonstr.empty()) { json = njson::parse(jsonstr); }
}
catch (nlohmann::json::parse_error& e)
{
std::cout << "JSON parse error: " << e.what() << "\n" << jsonstr << std::endl;
return false;
}
return true;
}
static bool parse(std::string jsonstr, njson& json);
template <typename T>
static void read(njson& json, std::string k, T& v)
@@ -87,14 +69,10 @@ public:
}
}
static void parse(std::string jsonstr, std::vector<std::string>& vd)
{
njson jsonroot;
if (JSON::parse(jsonstr, jsonroot))
{
vd = jsonroot.get<std::vector<std::string>>();
}
}
static std::string readStr(njson& json, std::string k);
static void parse(std::string jsonstr, std::vector<std::string>& vd);
};