Midnight Sun Firmware
Loading...
Searching...
No Matches
JSONManager

Class for managing JSON Files. More...

#include <json_manager.h>

Public Member Functions

 JSONManager ()
 Constructs a JSONManager object. More...
 
bool projectExists (const std::string &projectName)
 Validate if a project JSON exists. More...
 
void deleteProject (const std::string &projectName)
 Delete an existing project JSON. More...
 
template<typename T >
void setProjectValue (const std::string &projectName, const std::string &key, T value)
 Sets a value for a given key in the project JSON. More...
 
template<typename T >
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. More...
 
template<typename T >
getProjectValue (const std::string &projectName, const std::string &key)
 Gets a value for a given key in the project JSON. More...
 
template<typename T = nlohmann::json>
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. More...
 

Private Member Functions

void createDefaultProjectJSON (const std::string &projectName)
 Creates a default project JSON. More...
 
std::filesystem::path getProjectFilePath (const std::string &projectName)
 Get the file path of a specified project. More...
 
nlohmann::json loadProjectJSON (const std::string &projectName)
 Loads the JSON of a specified project. More...
 
void saveProjectJSON (const std::string &projectName, const nlohmann::json &projectData)
 Saves JSON data to a specified project. More...
 

Private Attributes

std::filesystem::path m_projectBasePath
 

Static Private Attributes

static constexpr const char * DEFAULT_JSON_PATH = "./Simulation_JSON/"
 

Detailed Description

Class for managing JSON Files.

This class supports creating new JSON files, writing/reading to and from JSON files Default JSON generation and saving/loading data from provided paths

Constructor & Destructor Documentation

◆ JSONManager()

Constructs a JSONManager object.

Initializes the JSONManager. The constructor sets up internal variables and creates the DEFAULT_JSON_PATH folder

Member Function Documentation

◆ createDefaultProjectJSON()

void createDefaultProjectJSON ( const std::string &  projectName)
private

Creates a default project JSON.

The project shall be created within the DEFAULT_JSON_PATH folder

Parameters
projectNameName of the project being created

◆ deleteProject()

void deleteProject ( const std::string &  projectName)

Delete an existing project JSON.

Parameters
projectNameName of the project to delete

◆ getProjectFilePath()

std::filesystem::path getProjectFilePath ( const std::string &  projectName)
private

Get the file path of a specified project.

Parameters
projectNameName of the project being retrieved
Returns
File path relative to the root directory

◆ getProjectNestedValue()

T getProjectNestedValue ( const std::string &  projectName,
const std::vector< std::string > &  keyPath,
const T &  defaultValue = T() 
)
inline

Gets a nested value for a given key in the project JSON.

This function loads the project's JSON, and then retrieves the value for the given key

Template Parameters
TThe type of the value to be fetched
Parameters
projectNameName of the project whose JSON will be checked
keyPathThe key path in the JSON to check in the format: { 'key1', 'key2', 'key3' }
Returns
Value saved at the JSON key

◆ getProjectValue()

T getProjectValue ( const std::string &  projectName,
const std::string &  key 
)
inline

Gets a value for a given key in the project JSON.

This function loads the project's JSON, and then retrieves the value for the given key

Template Parameters
TThe type of the value to be fetched
Parameters
projectNameName of the project whose JSON will be checked
keyThe key in the JSON to check
Returns
Value saved at the JSON key

◆ loadProjectJSON()

nlohmann::json loadProjectJSON ( const std::string &  projectName)
private

Loads the JSON of a specified project.

Parameters
projectNameName of the project being retrieved
Returns
JSON object of the selected project

◆ projectExists()

bool projectExists ( const std::string &  projectName)

Validate if a project JSON exists.

Parameters
projectNameName of the project to validate
Returns
TRUE if the project JSON exists FALSE if the project JSON does not exist

◆ saveProjectJSON()

void saveProjectJSON ( const std::string &  projectName,
const nlohmann::json &  projectData 
)
private

Saves JSON data to a specified project.

Parameters
projectNameName of the project being written to
projectDataJSON object of the data to be written

◆ setProjectNestedValue()

void setProjectNestedValue ( const std::string &  projectName,
const std::vector< std::string > &  keyPath,
const T &  value 
)
inline

Sets a nested value for a given key in the project JSON.

This function loads the project's JSON, updates the value for the given key, and then saves the updated JSON back to the project file

Template Parameters
TThe type of the value to be set
Parameters
projectNameName of the project whose JSON will be updated
keyPathThe key path in the JSON to update in the format: { 'key1', 'key2', 'key3' }
valueThe value to set for the given key

◆ setProjectValue()

void setProjectValue ( const std::string &  projectName,
const std::string &  key,
value 
)
inline

Sets a value for a given key in the project JSON.

This function loads the project's JSON, updates the value for the given key, and then saves the updated JSON back to the project file

Template Parameters
TThe type of the value to be set
Parameters
projectNameName of the project whose JSON will be updated
keyThe key in the JSON to update
valueThe value to set for the given key

Member Data Documentation

◆ DEFAULT_JSON_PATH

constexpr const char* DEFAULT_JSON_PATH = "./Simulation_JSON/"
staticconstexprprivate

Default JSON folder path

◆ m_projectBasePath

std::filesystem::path m_projectBasePath
private

Temporary variable to store project file path


The documentation for this class was generated from the following files: