libnick 2025.9.1
A cross-platform base for native Nickvision applications.
Loading...
Searching...
No Matches
jsonfilebase.h
Go to the documentation of this file.
1
22
23#ifndef JSONFILEBASE_H
24#define JSONFILEBASE_H
25
26#include <filesystem>
27#include <mutex>
28#include <string>
29#include <boost/json.hpp>
30#include "events/event.h"
31#include "ijsonserializable.h"
32
33namespace Nickvision::Helpers
34{
39 {
40 public:
46 JsonFileBase(const std::filesystem::path& path);
50 virtual ~JsonFileBase() noexcept = default;
55 const std::filesystem::path& getPath() const noexcept;
60 Events::Event<Events::EventArgs>& saved() noexcept;
65 bool save() noexcept;
70 boost::json::value toJson() const noexcept override;
71
72 protected:
76 bool contains(const std::string& key) const noexcept;
84 template<SupportedJsonValue T>
85 T get(const std::string& key, const T& defaultValue) const noexcept
86 {
87 std::lock_guard lock{ m_mutex };
88 if constexpr (std::is_same_v<T, int>)
89 {
90 if(!m_json.contains(key) || !m_json[key].is_int64())
91 {
92 return defaultValue;
93 }
94 return static_cast<int>(m_json[key].as_int64());
95 }
96 else if constexpr (std::is_same_v<T, std::int64_t>)
97 {
98 if(!m_json.contains(key) || !m_json[key].is_int64())
99 {
100 return defaultValue;
101 }
102 return m_json[key].as_int64();
103 }
104 else if constexpr (std::is_same_v<T, double>)
105 {
106 if(!m_json.contains(key) || !m_json[key].is_double())
107 {
108 return defaultValue;
109 }
110 return m_json[key].as_double();
111 }
112 else if constexpr (std::is_same_v<T, bool>)
113 {
114 if(!m_json.contains(key) || !m_json[key].is_bool())
115 {
116 return defaultValue;
117 }
118 return m_json[key].as_bool();
119 }
120 else if constexpr (std::is_same_v<T, std::string>)
121 {
122 if(!m_json.contains(key) || !m_json[key].is_string())
123 {
124 return defaultValue;
125 }
126 return std::string(m_json[key].as_string());
127 }
128 else if constexpr (std::is_same_v<T, boost::json::array>)
129 {
130 if(!m_json.contains(key) || !m_json[key].is_array())
131 {
132 return defaultValue;
133 }
134 return m_json[key].as_array();
135 }
136 else if constexpr (std::is_same_v<T, boost::json::object>)
137 {
138 if(!m_json.contains(key) || !m_json[key].is_object())
139 {
140 return defaultValue;
141 }
142 return m_json[key].as_object();
143 }
144 }
145
151 template<SupportedJsonValue T>
152 void set(const std::string& key, const T& value) noexcept
153 {
154 std::lock_guard lock{ m_mutex };
155 m_json[key] = value;
156 }
157
158 private:
159 mutable std::mutex m_mutex;
160 std::filesystem::path m_path;
161 mutable boost::json::object m_json;
163 };
164}
165
166#endif //JSONFILEBASE_H
An event that can have handlers subscribe to it, which in turn will be called when the event is invok...
Definition event.h:48
An interface for objects serializable to Json.
Definition ijsonserializable.h:14
T get(const std::string &key, const T &defaultValue) const noexcept
Gets a value from the json object.
Definition jsonfilebase.h:85
void set(const std::string &key, const T &value) noexcept
Sets a value in the json object.
Definition jsonfilebase.h:152
const std::filesystem::path & getPath() const noexcept
Gets the path of the json file.
bool contains(const std::string &key) const noexcept
Gets whether or not the json object contains a key.
boost::json::value toJson() const noexcept override
Serializes the object to Json.
virtual ~JsonFileBase() noexcept=default
Destructs a JsonFileBase.
bool save() noexcept
Saves the config file to disk.
JsonFileBase(const std::filesystem::path &path)
Constructs a JsonFileBase, loading the file from disk.
Events::Event< Events::EventArgs > & saved() noexcept
Gets the Saved event.
Definition ijsonserializable.h:28
Definition event.h:33
Definition cancellationtoken.h:30