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

Lightweight JSON wrapper for persistent config management using StorageManager. More...

#include <string>
#include <vector>
#include "StorageManager.h"
#include "nlohmann/json.hpp"
+ Include dependency graph for JsonService.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

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

Functions

nlohmann::json mergeDefaults (const nlohmann::json &target, const nlohmann::json &defaults)
 Merge a default JSON structure into a target, preserving existing keys.
 

Detailed Description

Author
Ian Archbell

Part of the PicoFramework application framework. Provides convenience access to a JSON document that can be loaded from and saved to storage. Also supports aliases and direct access to internal nlohmann::json object.

Version
0.1
Date
2025-03-31
License:\n MIT License

Definition in file JsonService.h.

Function Documentation

◆ mergeDefaults()

nlohmann::json mergeDefaults ( const nlohmann::json target,
const nlohmann::json defaults 
)

For nested objects, recursion is applied. Used to safely inject default values into config data.

Parameters
targetTarget JSON object to merge into.
defaultsDefault values to apply where keys are missing.
Returns
A new merged JSON object.

Merge a default JSON structure into a target, preserving existing keys.

Merge a default JSON structure into a target, preserving existing keys.

Definition at line 26 of file JsonService.cpp.

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}
nlohmann::json mergeDefaults(const nlohmann::json &target, const nlohmann::json &defaults)

References mergeDefaults().

Referenced by mergeDefaults().

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