mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-28 03:09:24 +08:00
实现削峰套利策略的编辑页面
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
|
||||
using NJson = nlohmann::json;
|
||||
using NJsonNode = nlohmann::json;
|
||||
|
||||
/// =============================================================================================
|
||||
/// 使用说明:
|
||||
@@ -36,32 +37,60 @@ using NJson = nlohmann::json;
|
||||
/// 序列化为字符串 -------------
|
||||
// std::string json_str = j.dump(4); // 4 表示缩进,美化输出
|
||||
|
||||
static bool NJsonLoad(std::string jsonfile, NJson& json)
|
||||
{
|
||||
std::ifstream ifs(jsonfile);
|
||||
if (ifs.is_open())
|
||||
{
|
||||
ifs >> json;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/// 数组解析 -------------
|
||||
// json jArray = json::array()
|
||||
// jArray.push_back(1);
|
||||
// jArray.push_back(2);
|
||||
// json j;
|
||||
// j["data"] = {1,2,3,4,5};
|
||||
// std::vector<int> v1;
|
||||
// v1 = j.at["data"].get<std::vector<int>>()
|
||||
|
||||
static bool NJsonParse(std::string jsonstr, NJson& json)
|
||||
class NJson
|
||||
{
|
||||
try
|
||||
{
|
||||
json = NJson::parse(jsonstr);
|
||||
}
|
||||
catch (nlohmann::json::parse_error& e)
|
||||
public:
|
||||
static bool load(std::string jsonfile, NJsonNode& json)
|
||||
{
|
||||
//std::cerr << "JSON 解析错误: " << e.what() << std::endl;
|
||||
std::ifstream ifs(jsonfile);
|
||||
if (ifs.is_open())
|
||||
{
|
||||
ifs >> json;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool NJsonLHas(NJson& json, std::string key)
|
||||
{
|
||||
return json.contains("database");
|
||||
}
|
||||
static bool parse(std::string jsonstr, NJsonNode& json)
|
||||
{
|
||||
try
|
||||
{
|
||||
json = NJsonNode::parse(jsonstr);
|
||||
}
|
||||
catch (nlohmann::json::parse_error& e)
|
||||
{
|
||||
std::cout << "JSON parse error: " << e.what() << std::endl;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool contains(NJsonNode& json, std::string key)
|
||||
{
|
||||
return json.contains("database");
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static void read(NJsonNode& json, std::string k, T& v)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (json.contains(k)) { v = json.at(k).get<T>(); }
|
||||
}
|
||||
catch (const nlohmann::detail::exception& e)
|
||||
{
|
||||
std::cout << "JSON read error: " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user