37 lines
1.0 KiB
C++
37 lines
1.0 KiB
C++
#pragma once
|
|
#include <nlohmann/json.hpp>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
static inline nlohmann::json JSONOpenOrCreate(const std::string& path) {
|
|
auto fileExists = fs::exists(path);
|
|
|
|
if (fileExists) {
|
|
auto file = std::fstream(path, std::fstream::in | std::fstream::out);
|
|
auto json = nlohmann::json::parse(file);
|
|
file.close();
|
|
return json;
|
|
} else {
|
|
auto file = std::fstream(path, std::fstream::in | std::fstream::out | std::fstream::trunc);
|
|
nlohmann::json json;
|
|
json["audio"]["volume"] = 0.5;
|
|
json["cpu"]["type"] = std::string("interpreter");
|
|
|
|
file << json;
|
|
file.close();
|
|
|
|
return json;
|
|
}
|
|
}
|
|
|
|
template <typename T>
|
|
static inline void JSONSetField(nlohmann::json& json, const std::string& field1, const std::string& field2, const T& value) {
|
|
json[field1][field2] = value;
|
|
}
|
|
|
|
template <typename T>
|
|
static inline const T& JSONGetField(nlohmann::json& json, const std::string& field1, const std::string& field2) {
|
|
return json[field1][field2];
|
|
} |