1、json结构体定义
#include "nlohmann/json.hpp"struct CommonParam {int imageQuality = 80;float imagePixelRatio = 2.0;NLOHMANN_DEFINE_TYPE_INTRUSIVE(CommonParam,imageQuality, imagePixelRatio)
};struct SpComposParam {int sequenceQuality = 80;float maxFrameRate = 24.0;NLOHMANN_DEFINE_TYPE_INTRUSIVE(SpComposParam, sequenceQuality,maxFrameRate)
};struct ConfigParam {CommonParam commonParam;SpComposParam spComposParam;bool enable = false;NLOHMANN_DEFINE_TYPE_INTRUSIVE(ConfigParam, commonParam, spComposParam, enable)
};
2、json文件读写函数
bool getConfigParam(ConfigParam& param) {try {// 打开文件并读取 JSON 数据std::ifstream file(configFileSavePath);if (!file.is_open()) {std::cerr << "Failed to open file for reading: " << configFileSavePath << std::endl;return false;}nlohmann::json j;file >> j;file.close();configParam = j.get<ConfigParam>();param = configParam;return true;} catch (const std::exception& e) {std::cerr << "Error reading JSON from file: " << e.what() << std::endl;return false;}
}bool setConfigParam(const ConfigParam& param) {try {configParam = param;nlohmann::json j = configParam;std::ofstream file(configFileSavePath);if (!file.is_open()) {std::cerr << "Failed to open file for writing: " << configFileSavePath << std::endl;return false;}file << std::setw(4) << j << std::endl;file.close();return true;} catch (const std::exception& e) {std::cerr << "Error writing JSON to file: " << e.what() << std::endl;return false;}
}