Provides a basic JSON-backed record model. More...
#include <FrameworkModel.h>
Inheritance diagram for FrameworkModel:
Collaboration diagram for FrameworkModel:Public Member Functions | |
| FrameworkModel (const std::string &path) | |
| Constructor. | |
| bool | load () |
| Loads the JSON collection from storage. | |
| bool | save () |
| Saves the current collection to storage. | |
| std::vector< nlohmann::json > | all () const |
| Returns all items in the collection. | |
| std::optional< nlohmann::json > | find (const std::string &id) const |
| Finds an item by ID. | |
| bool | create (const nlohmann::json &item) |
| Adds a new item to the collection. | |
| bool | update (const std::string &id, const nlohmann::json &updatedItem) |
| Updates an item by ID. | |
| bool | remove (const std::string &id) |
| Removes an item by ID. | |
| nlohmann::json | toJson () const |
| Returns the full collection as a JSON array object. | |
| nlohmann::json | findAsJson (const std::string &id) const |
| Finds a single item by ID and returns it as JSON or null. | |
| bool | save (const std::string &id, const json &data) |
| Saves a single item by ID and JSON object. | |
| bool | createFromJson (const nlohmann::json &obj) |
| bool | updateFromJson (const std::string &id, const nlohmann::json &updates) |
| nlohmann::json | deleteAsJson (const std::string &id) |
| bool | saveAll () |
| template<typename T > | |
| T | getValue (const std::string &key, const T &defaultValue=T()) |
| Reads a single top-level key from the model file. | |
| template<typename T > | |
| void | setValue (const std::string &key, const T &value) |
| Sets a single top-level key in the model file. | |
Protected Member Functions | |
| virtual std::string | getIdField () const |
| Returns the JSON key used as the record ID. | |
Protected Attributes | |
| nlohmann::json | collection = nlohmann::json::array() |
| In-memory array of records. | |
Private Attributes | |
| JsonService * | jsonService = nullptr |
| Underlying JSON persistence layer | |
| std::string | storagePath |
| File path for this model. | |
FrameworkModel abstracts away JSON loading/saving and record manipulation. Subclass it to represent specific collections, optionally overriding getIdField() if the record ID is not "id".
Definition at line 36 of file FrameworkModel.h.
| FrameworkModel::FrameworkModel | ( | const std::string & | path | ) |
Constructor.
| storage | Pointer to a StorageManager (e.g. SD, flash). |
| path | Path to the backing JSON file. |
| storage | Pointer to a StorageManager (e.g. SD, flash). |
| path | Path to the backing JSON file. |
Definition at line 26 of file FrameworkModel.cpp.
| std::vector< nlohmann::json > FrameworkModel::all | ( | ) | const |
Returns all items in the collection.
Definition at line 67 of file FrameworkModel.cpp.
References collection, and TRACE.
| bool FrameworkModel::create | ( | const nlohmann::json & | item | ) |
Adds a new item to the collection.
| item | JSON object to insert. |
| item | JSON object to insert. |
Definition at line 97 of file FrameworkModel.cpp.
References collection, find(), and getIdField().
Here is the call graph for this function:| bool FrameworkModel::createFromJson | ( | const nlohmann::json & | obj | ) |
Definition at line 166 of file FrameworkModel.cpp.
References getIdField(), and save().
Here is the call graph for this function:| nlohmann::json FrameworkModel::deleteAsJson | ( | const std::string & | id | ) |
Definition at line 193 of file FrameworkModel.cpp.
References collection, getIdField(), and saveAll().
Here is the call graph for this function:| std::optional< nlohmann::json > FrameworkModel::find | ( | const std::string & | id | ) | const |
Finds an item by ID.
| id | The value of the ID field to match. |
| id | The value of the ID field to match. |
Definition at line 83 of file FrameworkModel.cpp.
References collection, and getIdField().
Referenced by create(), and findAsJson().
Here is the call graph for this function:
Here is the caller graph for this function:| nlohmann::json FrameworkModel::findAsJson | ( | const std::string & | id | ) | const |
Finds a single item by ID and returns it as JSON or null.
Definition at line 140 of file FrameworkModel.cpp.
References find().
Here is the call graph for this function:
|
inlineprotectedvirtual |
Defaults to "id", but can be overridden by subclasses.
Definition at line 172 of file FrameworkModel.h.
Referenced by create(), createFromJson(), deleteAsJson(), find(), remove(), save(), update(), and updateFromJson().
Here is the caller graph for this function:
|
inline |
This is separate from array-style records and useful for persistent app state.
| T | Expected return type |
| key | JSON key name |
| defaultValue | Returned if key is not present |
Definition at line 129 of file FrameworkModel.h.
References JsonService::data(), AppContext::getTypeKey(), and jsonService.
Referenced by PicoModel::onNetworkReady(), and PicoModel::restoreState().
Here is the call graph for this function:
Here is the caller graph for this function:| bool FrameworkModel::load | ( | ) |
Loads the JSON collection from storage.
Definition at line 30 of file FrameworkModel.cpp.
References collection, JsonService::data(), AppContext::getTypeKey(), jsonService, JsonService::load(), storagePath, and TRACE.
Referenced by PicoModel::restoreState().
Here is the call graph for this function:
Here is the caller graph for this function:| bool FrameworkModel::remove | ( | const std::string & | id | ) |
Removes an item by ID.
| id | The ID of the item to remove. |
| id | The ID of the item to remove. |
Definition at line 125 of file FrameworkModel.cpp.
References collection, and getIdField().
Here is the call graph for this function:| bool FrameworkModel::save | ( | ) |
Saves the current collection to storage.
Definition at line 59 of file FrameworkModel.cpp.
References collection, JsonService::data(), AppContext::getTypeKey(), jsonService, JsonService::save(), and storagePath.
Referenced by createFromJson(), save(), PicoModel::saveState(), and updateFromJson().
Here is the call graph for this function:
Here is the caller graph for this function:| bool FrameworkModel::save | ( | const std::string & | id, |
| const json & | data | ||
| ) |
Saves the current collection to storage. (single record)
(single record)
Definition at line 147 of file FrameworkModel.cpp.
References collection, getIdField(), and save().
Here is the call graph for this function:| bool FrameworkModel::saveAll | ( | ) |
Definition at line 209 of file FrameworkModel.cpp.
References collection, JsonService::data(), AppContext::getTypeKey(), jsonService, JsonService::save(), and storagePath.
Referenced by deleteAsJson().
Here is the call graph for this function:
Here is the caller graph for this function:
|
inline |
This does not affect the array-style record collection.
| T | Type of value to store |
| key | JSON key |
| value | Value to assign |
Definition at line 154 of file FrameworkModel.h.
References JsonService::data(), AppContext::getTypeKey(), and jsonService.
Referenced by PicoModel::saveState(), and PicoModel::setLedState().
Here is the call graph for this function:
Here is the caller graph for this function:
|
inline |
Definition at line 100 of file FrameworkModel.h.
References collection.
| bool FrameworkModel::update | ( | const std::string & | id, |
| const nlohmann::json & | updatedItem | ||
| ) |
Updates an item by ID.
| id | The ID of the item to update. |
| updatedItem | The new item JSON to replace the old one. |
| id | The ID of the item to update. |
| updatedItem | The new item JSON to replace the old one. |
Definition at line 110 of file FrameworkModel.cpp.
References collection, and getIdField().
Here is the call graph for this function:| bool FrameworkModel::updateFromJson | ( | const std::string & | id, |
| const nlohmann::json & | updates | ||
| ) |
Definition at line 175 of file FrameworkModel.cpp.
References collection, getIdField(), and save().
Here is the call graph for this function:
|
protected |
Definition at line 174 of file FrameworkModel.h.
Referenced by all(), create(), deleteAsJson(), find(), load(), remove(), save(), save(), saveAll(), toJson(), update(), and updateFromJson().
|
private |
Definition at line 177 of file FrameworkModel.h.
Referenced by getValue(), load(), save(), saveAll(), and setValue().
|
private |
Definition at line 178 of file FrameworkModel.h.