17#include <unordered_map>
20#include <nlohmann/json.hpp>
30#define PROJECT_VERSION "1.0.0"
69 void saveProjectJSON(
const std::string &projectName,
const nlohmann::json &projectData);
102 template <
typename T>
103 void setProjectValue(
const std::string &projectName,
const std::string &key, T value) {
107 projectJSON[key] = value;
110 }
catch (
const std::exception &e) {
111 std::cerr <<
"Error setting project value: " << e.what() << std::endl;
124 template <
typename T>
125 void setProjectNestedValue(
const std::string &projectName,
const std::vector<std::string> &keyPath,
const T &value) {
129 nlohmann::json *current = &projectJSON;
132 for (
size_t i = 0; i < keyPath.size() - 1; ++i) {
133 if (!current->contains(keyPath[i])) {
134 (*current)[keyPath[i]] = nlohmann::json::object();
137 current = &((*current)[keyPath[i]]);
140 (*current)[keyPath.back()] = value;
143 }
catch (
const std::exception &e) {
144 std::cerr <<
"Error setting nested project value: " << e.what() << std::endl;
156 template <
typename T>
161 if (projectJSON.contains(key)) {
162 return projectJSON[key].get<T>();
165 }
catch (
const std::exception &e) {
166 std::cerr <<
"Error getting project value: " << e.what() << std::endl;
168 return static_cast<T
>(0U);
179 template <
typename T = nlohmann::json>
180 T
getProjectNestedValue(
const std::string &projectName,
const std::vector<std::string> &keyPath,
const T &defaultValue = T()) {
184 const nlohmann::json *current = &projectJSON;
186 for (
const auto &key : keyPath) {
187 if (!current->contains(key)) {
191 current = &((*current)[key]);
194 return current->get<T>();
195 }
catch (
const std::exception &e) {
196 std::cerr <<
"Error getting nested project value: " << e.what() << std::endl;
Class for managing JSON Files.
Definition: json_manager.h:38
std::filesystem::path m_projectBasePath
Definition: json_manager.h:41
void createDefaultProjectJSON(const std::string &projectName)
Creates a default project JSON.
Definition: json_manager.cc:19
std::filesystem::path getProjectFilePath(const std::string &projectName)
Get the file path of a specified project.
Definition: json_manager.cc:39
static constexpr const char * DEFAULT_JSON_PATH
Definition: json_manager.h:40
T getProjectNestedValue(const std::string &projectName, const std::vector< std::string > &keyPath, const T &defaultValue=T())
Gets a nested value for a given key in the project JSON.
Definition: json_manager.h:180
void setProjectValue(const std::string &projectName, const std::string &key, T value)
Sets a value for a given key in the project JSON.
Definition: json_manager.h:103
T getProjectValue(const std::string &projectName, const std::string &key)
Gets a value for a given key in the project JSON.
Definition: json_manager.h:157
void saveProjectJSON(const std::string &projectName, const nlohmann::json &projectData)
Saves JSON data to a specified project.
Definition: json_manager.cc:72
nlohmann::json loadProjectJSON(const std::string &projectName)
Loads the JSON of a specified project.
Definition: json_manager.cc:51
JSONManager()
Constructs a JSONManager object.
Definition: json_manager.cc:87
bool projectExists(const std::string &projectName)
Validate if a project JSON exists.
Definition: json_manager.cc:100
void setProjectNestedValue(const std::string &projectName, const std::vector< std::string > &keyPath, const T &value)
Sets a nested value for a given key in the project JSON.
Definition: json_manager.h:125
void deleteProject(const std::string &projectName)
Delete an existing project JSON.
Definition: json_manager.cc:104