Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
JsonService Class Reference

Manages loading and saving of a single JSON document using StorageManager. More...

#include <JsonService.h>

+ Collaboration diagram for JsonService:

Public Member Functions

 JsonService (StorageManager *storage)
 Construct a new JsonService.
 
bool load (const std::string &path)
 Load a JSON file from storage.
 
bool save (const std::string &path) const
 Save the current JSON data to storage.
 
nlohmann::json & data ()
 Access the internal JSON object.
 
const nlohmann::json & data () const
 Const access to the internal JSON object.
 
nlohmann::json & root ()
 Alias for data().
 
const nlohmann::json & root () const
 Const alias for data().
 
nlohmann::json & operator* ()
 Operator alias for data().
 
const nlohmann::json & operator* () const
 Const operator alias for data().
 
bool hasValidData () const
 

Private Attributes

StorageManagerstorage
 
nlohmann::json data_
 

Detailed Description

Definition at line 27 of file JsonService.h.

Constructor & Destructor Documentation

◆ JsonService()

JsonService::JsonService ( StorageManager storage)

Construct a new JsonService.

Parameters
storagePointer to a StorageManager for persistent access.
Parameters
storagePointer to a StorageManager for persistent access.

Definition at line 48 of file JsonService.cpp.

49 : storage(storage) {}
StorageManager * storage
Definition JsonService.h:83

Member Function Documentation

◆ data() [1/2]

nlohmann::json & JsonService::data ( )

Access the internal JSON object.

Definition at line 104 of file JsonService.cpp.

105{
106 return data_;
107}
nlohmann::json data_
Definition JsonService.h:84

References data_.

Referenced by FrameworkModel::getValue(), FrameworkModel::load(), FrameworkModel::save(), FrameworkModel::saveAll(), and FrameworkModel::setValue().

+ Here is the caller graph for this function:

◆ data() [2/2]

const nlohmann::json & JsonService::data ( ) const

Definition at line 110 of file JsonService.cpp.

111{
112 return data_;
113}

References data_.

◆ hasValidData()

bool JsonService::hasValidData ( ) const

Definition at line 139 of file JsonService.cpp.

139 {
140 return !data_.is_discarded();
141}

References data_.

◆ load()

bool JsonService::load ( const std::string &  path)

Load a JSON file from storage.

Parameters
pathFile path to load from.
Returns
true if load was successful and JSON parsed correctly.
Parameters
pathFile path to load from.
Returns
true if load was successful and JSON parsed correctly.

Definition at line 52 of file JsonService.cpp.

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}
#define TRACE(...)
Default trace (INFO level).
Definition DebugTrace.h:187
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.

References data_, StorageManager::isMounted(), StorageManager::mount(), StorageManager::readFile(), storage, and TRACE.

Referenced by FrameworkModel::load().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ operator*() [1/2]

nlohmann::json & JsonService::operator* ( )

Operator alias for data().

Definition at line 128 of file JsonService.cpp.

129{
130 return data_;
131}

References data_.

◆ operator*() [2/2]

const nlohmann::json & JsonService::operator* ( ) const

Definition at line 134 of file JsonService.cpp.

135{
136 return data_;
137}

References data_.

◆ root() [1/2]

nlohmann::json & JsonService::root ( )

Alias for data().

Definition at line 116 of file JsonService.cpp.

117{
118 return data_;
119}

References data_.

◆ root() [2/2]

const nlohmann::json & JsonService::root ( ) const

Definition at line 122 of file JsonService.cpp.

123{
124 return data_;
125}

References data_.

◆ save()

bool JsonService::save ( const std::string &  path) const

Save the current JSON data to storage.

Parameters
pathFile path to save to.
Returns
true if save was successful.
Parameters
pathFile path to save to.
Returns
true if save was successful.

Definition at line 85 of file JsonService.cpp.

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}
virtual bool writeFile(const std::string &path, const std::vector< uint8_t > &data)=0
Write a memory buffer to a file.

References data_, StorageManager::isMounted(), StorageManager::mount(), storage, TRACE, and StorageManager::writeFile().

Referenced by FrameworkModel::save(), and FrameworkModel::saveAll().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ data_

nlohmann::json JsonService::data_
private

Definition at line 84 of file JsonService.h.

Referenced by data(), data(), hasValidData(), load(), operator*(), operator*(), root(), root(), and save().

◆ storage

StorageManager* JsonService::storage
private

Definition at line 83 of file JsonService.h.

Referenced by load(), and save().


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