Midnight Sun Firmware
Loading...
Searching...
No Matches
json_manager.h
1#pragma once
2
3/************************************************************************************************
4 * @file json_manager.h
5 *
6 * @brief Header file defining the JSONManager class
7 *
8 * @date 2025-01-04
9 * @author Aryan Kashem
10 ************************************************************************************************/
11
12/* Standard library Headers */
13#include <filesystem>
14#include <fstream>
15#include <iostream>
16#include <string>
17#include <unordered_map>
18
19/* Inter-component Headers */
20#include <nlohmann/json.hpp>
21
22/* Intra-component Headers */
23
30#define PROJECT_VERSION "1.0.0"
31
39 private:
40 static constexpr const char *DEFAULT_JSON_PATH = "./Simulation_JSON/";
41 std::filesystem::path m_projectBasePath;
48 void createDefaultProjectJSON(const std::string &projectName);
49
55 std::filesystem::path getProjectFilePath(const std::string &projectName);
56
62 nlohmann::json loadProjectJSON(const std::string &projectName);
63
69 void saveProjectJSON(const std::string &projectName, const nlohmann::json &projectData);
70
71 public:
78
85 bool projectExists(const std::string &projectName);
86
91 void deleteProject(const std::string &projectName);
92
102 template <typename T>
103 void setProjectValue(const std::string &projectName, const std::string &key, T value) {
104 try {
105 nlohmann::json projectJSON = loadProjectJSON(projectName);
106
107 projectJSON[key] = value;
108
109 saveProjectJSON(projectName, projectJSON);
110 } catch (const std::exception &e) {
111 std::cerr << "Error setting project value: " << e.what() << std::endl;
112 }
113 }
114
124 template <typename T>
125 void setProjectNestedValue(const std::string &projectName, const std::vector<std::string> &keyPath, const T &value) {
126 try {
127 nlohmann::json projectJSON = loadProjectJSON(projectName);
128
129 nlohmann::json *current = &projectJSON;
130
131 /* Navigate to the desired key location */
132 for (size_t i = 0; i < keyPath.size() - 1; ++i) {
133 if (!current->contains(keyPath[i])) {
134 (*current)[keyPath[i]] = nlohmann::json::object();
135 }
136 /* Update the JSON pointer to the nested JSON */
137 current = &((*current)[keyPath[i]]);
138 }
139
140 (*current)[keyPath.back()] = value;
141
142 saveProjectJSON(projectName, projectJSON);
143 } catch (const std::exception &e) {
144 std::cerr << "Error setting nested project value: " << e.what() << std::endl;
145 }
146 }
147
156 template <typename T>
157 T getProjectValue(const std::string &projectName, const std::string &key) {
158 try {
159 nlohmann::json projectJSON = loadProjectJSON(projectName);
160
161 if (projectJSON.contains(key)) {
162 return projectJSON[key].get<T>();
163 }
164
165 } catch (const std::exception &e) {
166 std::cerr << "Error getting project value: " << e.what() << std::endl;
167 }
168 return static_cast<T>(0U);
169 }
170
179 template <typename T = nlohmann::json>
180 T getProjectNestedValue(const std::string &projectName, const std::vector<std::string> &keyPath, const T &defaultValue = T()) {
181 try {
182 nlohmann::json projectJSON = loadProjectJSON(projectName);
183
184 const nlohmann::json *current = &projectJSON;
185 /* Navigate to the desired key location */
186 for (const auto &key : keyPath) {
187 if (!current->contains(key)) {
188 return defaultValue;
189 }
190 /* Update the JSON pointer to the nested JSON */
191 current = &((*current)[key]);
192 }
193
194 return current->get<T>();
195 } catch (const std::exception &e) {
196 std::cerr << "Error getting nested project value: " << e.what() << std::endl;
197 return defaultValue;
198 }
199 }
200};
201
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