Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
JsonService.cpp
Go to the documentation of this file.
1
16#include "framework_config.h" // Must be included before DebugTrace.h to ensure framework_config.h is processed first
17#include "DebugTrace.h"
19
20#include "storage/JsonService.h"
21#include <cstdio>
22#include <cstdint>
23#include <vector>
24
26nlohmann::json mergeDefaults(const nlohmann::json &target, const nlohmann::json &defaults)
27{
28 nlohmann::json result = target;
29
30 for (auto it = defaults.begin(); it != defaults.end(); ++it)
31 {
32 const std::string &key = it.key();
33
34 if (!result.contains(key))
35 {
36 result[key] = it.value();
37 }
38 else if (result[key].is_object() && it.value().is_object())
39 {
40 result[key] = mergeDefaults(result[key], it.value());
41 }
42 }
43
44 return result;
45}
46
49 : storage(storage) {}
50
52bool JsonService::load(const std::string &path)
53{
54 std::vector<uint8_t> buffer;
55 if (!storage)
56 return false;
57
58 if(!storage->isMounted())
59 {
60 storage->mount();
61 }
62
63 if (!storage->readFile(path, buffer))
64 {
65 TRACE("Failed to read JSON file: %s\n", path.c_str());
66 return false;
67 }
68
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());
72
73 // Treat empty file as valid empty object
74 if (str.empty())
75 {
76 data_ = nlohmann::json::object();
77 return true;
78 }
79
80 data_ = nlohmann::json::parse(str, nullptr, false); // No exceptions
81 return !data_.is_discarded();
82}
83
85bool JsonService::save(const std::string &path) const
86{
87 if (!storage)
88 return false;
89 if(!storage->isMounted())
90 {
91 storage->mount();
92 }
93 std::string content = data_.dump(2); // Pretty print
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());
97
98 bool ok = storage->writeFile(path, buffer);
99 TRACE("Saved JSON file to %s: %s\n", path.c_str(), ok ? "ok" : "failed");
100 return ok;
101}
102
104nlohmann::json &JsonService::data()
105{
106 return data_;
107}
108
110const nlohmann::json &JsonService::data() const
111{
112 return data_;
113}
114
116nlohmann::json &JsonService::root()
117{
118 return data_;
119}
120
122const nlohmann::json &JsonService::root() const
123{
124 return data_;
125}
126
128nlohmann::json &JsonService::operator*()
129{
130 return data_;
131}
132
134const nlohmann::json &JsonService::operator*() const
135{
136 return data_;
137}
138
140 return !data_.is_discarded();
141}
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.
Definition DebugTrace.h:170
#define TRACE(...)
Default trace (INFO level).
Definition DebugTrace.h:187
nlohmann::json mergeDefaults(const nlohmann::json &target, const nlohmann::json &defaults)
Manages loading and saving of a single JSON document using StorageManager.
Definition JsonService.h:28
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().
nlohmann::json data_
Definition JsonService.h:84
bool save(const std::string &path) const
Save the current JSON data to storage.
StorageManager * storage
Definition JsonService.h:83
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.