Files
eUi/include/generalJsonParsing.hpp
T
RCiRoot 97462c8aab On main: V20260416-2.5.1
增加halconGuard类,用于防止halcon算法的输入输出和程序不一致而引起的报错,并且会检查算法文件是否存在,如不存在则会弹窗提示。
增加handleAiGpuThreadGuard类,用于防止因重复调用同一个模型而引起的报错,并且支持不停机更新模型,支持初始化时检测模型文件是否存在。
增加内存占用显示控件。
用户管理增加root权限,默认密码为dseuiroot,模型热更新时需要此权限。
支持缓存界面上的检测数据。
相机模块新增在回调后的指定时间没有取图时则清空容器的功能。
增加等待结果汇总功能,在启用时同一个SN的图片会等待总结果后再进行存图。
存图线程修改为多线程编码,单线程储存,提高IO效率。并且增加根据内存占用的百分比更新存图逻辑的功能,默认占用超过70%不储存OK原图,超过80%不储存原图,超过90%时不储存所有图片。
2026-04-26 12:25:13 +08:00

126 lines
3.4 KiB
C++

#pragma once
#include <nlohmannJson.hpp>
#include <fstream>
#include <iostream>
// 读取json文件,不按顺序
static nlohmann::json readJsonFromPath(std::string path)
{
nlohmann::json jsonObj;
std::ifstream inputFile(path);
if (!inputFile.is_open()) {
std::cerr << "Could not open the file - " << path << std::endl;
return jsonObj;
}
try {
inputFile >> jsonObj;
}
catch (nlohmann::json::parse_error& e) {
std::cerr << "JSON parse error: " << e.what() << std::endl;
return jsonObj;
}
// 关闭文件
inputFile.close();
return jsonObj;
}
// 保存json文件,不按顺序
static bool saveJsonFromPath(std::string path, nlohmann::json& json)
{
std::ofstream saveFile(path);
if (!saveFile.is_open()) {
std::cerr << "Could not open the file - " << path << std::endl;
return false;
}
try {
saveFile << json.dump(4);
}
catch (nlohmann::json::parse_error& e) {
std::cerr << "JSON parse error: " << e.what() << std::endl;
return false;
}
// 关闭文件
saveFile.close();
return true;
}
// 有序读取 JSON 文件
static nlohmann::ordered_json readOrderedJsonFromPath(const std::string& path)
{
nlohmann::ordered_json jsonObj;
std::ifstream inputFile(path);
if (!inputFile.is_open()) {
std::cerr << "Could not open the file - " << path << std::endl;
return jsonObj;
}
try {
inputFile >> jsonObj;
}
catch (const nlohmann::json::parse_error& e) {
std::cerr << "JSON parse error: " << e.what() << std::endl;
return nlohmann::ordered_json();
}
return jsonObj;
}
// 有序保存 JSON 文件
static bool saveOrderedJsonFromPath(const std::string& path, const nlohmann::ordered_json& json)
{
std::ofstream saveFile(path);
if (!saveFile.is_open()) {
std::cerr << "Could not open the file - " << path << std::endl;
return false;
}
try {
saveFile << json.dump(4); // 缩进4空格
}
catch (const nlohmann::json::type_error& e) {
std::cerr << "JSON write error: " << e.what() << std::endl;
return false;
}
return true;
}
//打印json内容
static void printNlohmannJson(nlohmann::json input, int depth = 0)
{
std::string indent_str(depth, ' ');
// 处理 JSON 对象
if (input.is_object()) {
std::cout << indent_str << "{\n";
for (auto it = input.begin(); it != input.end(); ++it) {
std::cout << indent_str << " \"" << it.key() << "\": ";
printNlohmannJson(it.value(), depth + 2);
}
std::cout << indent_str << "}\n";
}
// 处理 JSON 数组
else if (input.is_array()) {
std::cout << indent_str << "[\n";
for (const auto& element : input) {
printNlohmannJson(element, depth + 2);
}
std::cout << indent_str << "]\n";
}
// 处理 JSON 字符串
else if (input.is_string()) {
std::cout << indent_str << "\"" << input.get<std::string>() << "\"\n";
}
// 处理 JSON 数字
else if (input.is_number()) {
std::cout << indent_str << input << "\n";
}
// 处理 JSON 布尔值
else if (input.is_boolean()) {
std::cout << indent_str << (input.get<bool>() ? "true" : "false") << "\n";
}
// 处理 JSON null
else if (input.is_null()) {
std::cout << indent_str << "null\n";
}
}