mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-28 03:09:24 +08:00
实现MQTT协议消息订阅和消息解析流程
This commit is contained in:
@@ -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);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user