5 #ifdef INCLUDE_NLOHMANN_JSON_HPP_
7 #ifndef TILESON_NLOHMANNJSON_HPP
8 #define TILESON_NLOHMANNJSON_HPP
15 inline NlohmannJson() =
default;
18 IJson &operator[](std::string_view key)
override
20 if(m_arrayCache.count(key.data()) == 0)
21 m_arrayCache[key.data()] = std::make_unique<NlohmannJson>(&m_json->operator[](key.data()));
23 return *m_arrayCache[key.data()].get();
26 inline explicit NlohmannJson(nlohmann::json *json) : m_json {json}
31 inline IJson& at(std::string_view key)
override
33 if(m_arrayCache.count(key.data()) == 0)
34 m_arrayCache[key.data()] = std::make_unique<NlohmannJson>(&m_json->operator[](key.data()));
36 return *m_arrayCache[key.data()].get();
39 inline IJson& at(
size_t pos)
override
41 if(m_arrayPosCache.count(pos) == 0)
42 m_arrayPosCache[pos] = std::make_unique<NlohmannJson>(&m_json->at(pos));
44 return *m_arrayPosCache[pos];
47 std::vector<std::unique_ptr<IJson>> array()
override
49 std::vector<std::unique_ptr<IJson>> vec;
50 for(
auto &item : *m_json)
52 nlohmann::json *ptr = &item;
53 vec.emplace_back(std::make_unique<NlohmannJson>(ptr));
59 inline std::vector<std::unique_ptr<IJson>> &array(std::string_view key)
override
61 if(m_arrayListDataCache.count(key.data()) == 0)
63 if (m_json->count(key.data()) > 0 && m_json->operator[](key.data()).is_array())
65 std::for_each(m_json->operator[](key.data()).begin(), m_json->operator[](key.data()).end(), [&](nlohmann::json &item)
67 nlohmann::json *ptr = &item;
68 m_arrayListDataCache[key.data()].emplace_back(std::make_unique<NlohmannJson>(ptr));
74 return m_arrayListDataCache[key.data()];
77 [[nodiscard]]
inline size_t size()
const override
79 return m_json->size();
82 inline bool parse(
const fs::path &path)
override
87 if (fs::exists(path) && fs::is_regular_file(path))
89 m_path = path.parent_path();
90 m_data = std::make_unique<nlohmann::json>();
91 std::ifstream i(path.u8string());
95 m_json = m_data.get();
97 catch (
const nlohmann::json::parse_error &error)
99 std::string message =
"Parse error: ";
100 message += std::string(error.what());
101 message += std::string(
"\n");
102 std::cerr << message;
110 inline bool parse(
const void *data,
size_t size)
override
114 m_data = std::make_unique<nlohmann::json>();
119 m_json = m_data.get();
121 catch (
const nlohmann::json::parse_error &error)
123 std::string message =
"Parse error: ";
124 message += std::string(error.what());
125 message += std::string(
"\n");
126 std::cerr << message;
132 [[nodiscard]]
inline size_t count(std::string_view key)
const override
134 return m_json->count(key);
137 [[nodiscard]]
inline bool any(std::string_view key)
const override
139 return count(key) > 0;
142 [[nodiscard]]
inline bool isArray()
const override
144 return m_json->is_array();
147 [[nodiscard]]
inline bool isObject()
const override
149 return m_json->is_object();
152 [[nodiscard]]
inline bool isNull()
const override
154 return m_json->is_null();
157 fs::path directory()
const override
162 void directory(
const fs::path &directory)
override
168 [[nodiscard]]
inline int32_t getInt32(std::string_view key)
override
170 return m_json->operator[](key.data()).get<int32_t>();
173 [[nodiscard]]
inline uint32_t getUInt32(std::string_view key)
override
175 return m_json->operator[](key.data()).get<uint32_t>();
178 [[nodiscard]]
inline int64_t getInt64(std::string_view key)
override
180 return m_json->operator[](key.data()).get<int64_t>();
183 [[nodiscard]]
inline uint64_t getUInt64(std::string_view key)
override
185 return m_json->operator[](key.data()).get<uint64_t>();
188 [[nodiscard]]
inline double getDouble(std::string_view key)
override
190 return m_json->operator[](key.data()).get<double>();
193 [[nodiscard]]
inline std::string getString(std::string_view key)
override
195 return m_json->operator[](key.data()).get<std::string>();
198 [[nodiscard]]
inline bool getBool(std::string_view key)
override
200 return m_json->operator[](key.data()).get<bool>();
203 [[nodiscard]]
float getFloat(std::string_view key)
override
205 return m_json->operator[](key.data()).get<float>();
208 [[nodiscard]]
inline int32_t getInt32()
override
210 return m_json->get<int32_t>();
213 [[nodiscard]]
inline uint32_t getUInt32()
override
215 return m_json->get<uint32_t>();
218 [[nodiscard]]
inline int64_t getInt64()
override
220 return m_json->get<int64_t>();
223 [[nodiscard]]
inline uint64_t getUInt64()
override
225 return m_json->get<uint64_t>();
228 [[nodiscard]]
inline double getDouble()
override
230 return m_json->get<
double>();
233 [[nodiscard]]
inline std::string getString()
override
235 return m_json->get<std::string>();
238 [[nodiscard]]
inline bool getBool()
override
240 return m_json->get<
bool>();
243 [[nodiscard]]
float getFloat()
override
245 return m_json->get<
float>();
249 inline void clearCache()
251 m_arrayCache.clear();
252 m_arrayPosCache.clear();
253 m_arrayListDataCache.clear();
256 nlohmann::json *m_json =
nullptr;
257 std::unique_ptr<nlohmann::json> m_data =
nullptr;
261 std::map<std::string, std::unique_ptr<IJson>> m_arrayCache;
262 std::map<size_t, std::unique_ptr<IJson>> m_arrayPosCache;
263 std::map<std::string, std::vector<std::unique_ptr<IJson>>> m_arrayListDataCache;
Definition: MemoryStream.hpp:12
Definition: Base64.hpp:12