20#include "storage/JsonService.h"
26nlohmann::json
mergeDefaults(
const nlohmann::json &target,
const nlohmann::json &defaults)
28 nlohmann::json result = target;
30 for (
auto it = defaults.begin(); it != defaults.end(); ++it)
32 const std::string &key = it.key();
34 if (!result.contains(key))
36 result[key] = it.value();
38 else if (result[key].is_object() && it.value().is_object())
54 std::vector<uint8_t> buffer;
65 TRACE(
"Failed to read JSON file: %s\n", path.c_str());
69 std::string str(buffer.begin(), buffer.end());
70 TRACE(
"Loaded JSON file from %s: %zu bytes\n", path.c_str(), str.size());
71 TRACE(
"Content: %s\n", str.c_str());
76 data_ = nlohmann::json::object();
80 data_ = nlohmann::json::parse(str,
nullptr,
false);
81 return !
data_.is_discarded();
93 std::string content =
data_.dump(2);
94 std::vector<uint8_t> buffer(content.begin(), content.end());
95 TRACE(
"Buffer size: %zu bytes\n", buffer.size());
96 TRACE(
"Buffer content: %s\n", content.c_str());
99 TRACE(
"Saved JSON file to %s: %s\n", path.c_str(), ok ?
"ok" :
"failed");
140 return !
data_.is_discarded();
Macro-based debug trace system with optional SD file logging.
#define TRACE_INIT(MODULE_NAME)
Declare trace usage in a source file for a given module.
#define TRACE(...)
Default trace (INFO level).
nlohmann::json mergeDefaults(const nlohmann::json &target, const nlohmann::json &defaults)
Manages loading and saving of a single JSON document using StorageManager.
nlohmann::json & operator*()
Operator alias for data().
nlohmann::json & data()
Access the internal JSON object.
JsonService(StorageManager *storage)
Construct a new JsonService.
bool hasValidData() const
nlohmann::json & root()
Alias for data().
bool save(const std::string &path) const
Save the current JSON data to storage.
bool load(const std::string &path)
Load a JSON file from storage.
Abstract base class for storage access and file operations.
virtual bool writeFile(const std::string &path, const std::vector< uint8_t > &data)=0
Write a memory buffer to a file.
virtual bool isMounted() const =0
Check mounted.
virtual bool readFile(const std::string &path, std::vector< uint8_t > &buffer)=0
Read a file into a memory buffer.
virtual bool mount()=0
Mount the underlying storage.
Delegates to user or system configuration.