5#ifdef INCLUDE_NLOHMANN_JSON_HPP_
7#ifndef TILESON_NLOHMANNJSON_HPP
8#define TILESON_NLOHMANNJSON_HPP
17 inline NlohmannJson() =
default;
20 IJson &operator[](std::string_view key)
override
22 if(m_arrayCache.count(key.data()) == 0)
23 m_arrayCache[key.data()] = std::make_unique<NlohmannJson>(&m_json->operator[](key.data()));
25 return *m_arrayCache[key.data()].get();
28 inline explicit NlohmannJson(nlohmann::json *json) : m_json {json}
33 inline IJson& at(std::string_view key)
override
35 if(m_arrayCache.count(key.data()) == 0)
36 m_arrayCache[key.data()] = std::make_unique<NlohmannJson>(&m_json->operator[](key.data()));
38 return *m_arrayCache[key.data()].get();
41 inline IJson& at(
size_t pos)
override
43 if(m_arrayPosCache.count(pos) == 0)
44 m_arrayPosCache[pos] = std::make_unique<NlohmannJson>(&m_json->at(pos));
46 return *m_arrayPosCache[pos];
49 std::vector<std::unique_ptr<IJson>> array()
override
51 std::vector<std::unique_ptr<IJson>> vec;
52 for(
auto &item : *m_json)
54 nlohmann::json *ptr = &item;
55 vec.emplace_back(std::make_unique<NlohmannJson>(ptr));
61 inline std::vector<std::unique_ptr<IJson>> &array(std::string_view key)
override
63 if(m_arrayListDataCache.count(key.data()) == 0)
65 if (m_json->count(key.data()) > 0 && m_json->operator[](key.data()).is_array())
67 std::for_each(m_json->operator[](key.data()).begin(), m_json->operator[](key.data()).end(), [&](nlohmann::json &item)
69 nlohmann::json *ptr = &item;
70 m_arrayListDataCache[key.data()].emplace_back(std::make_unique<NlohmannJson>(ptr));
76 return m_arrayListDataCache[key.data()];
79 [[nodiscard]]
inline size_t size()
const override
81 return m_json->size();
84 inline bool parse(
const fs::path &path)
override
89 if (fs::exists(path) && fs::is_regular_file(path))
91 m_path = path.parent_path();
92 m_data = std::make_unique<nlohmann::json>();
93 std::ifstream i(path.generic_string());
97 m_json = m_data.get();
99 catch (
const nlohmann::json::parse_error &error)
101 std::string message =
"Parse error: ";
102 message += std::string(error.what());
103 message += std::string(
"\n");
104 std::cerr << message;
112 inline bool parse(
const void *data,
size_t size)
override
116 m_data = std::make_unique<nlohmann::json>();
121 m_json = m_data.get();
123 catch (
const nlohmann::json::parse_error &error)
125 std::string message =
"Parse error: ";
126 message += std::string(error.what());
127 message += std::string(
"\n");
128 std::cerr << message;
134 [[nodiscard]]
inline size_t count(std::string_view key)
const override
136 return m_json->count(key);
139 [[nodiscard]]
inline bool any(std::string_view key)
const override
141 return count(key) > 0;
144 [[nodiscard]]
inline bool isArray()
const override
146 return m_json->is_array();
149 [[nodiscard]]
inline bool isObject()
const override
151 return m_json->is_object();
154 [[nodiscard]]
inline bool isNull()
const override
156 return m_json->is_null();
159 fs::path directory()
const override
164 void directory(
const fs::path &directory)
override
169 std::unique_ptr<IJson> create()
override
171 return std::make_unique<NlohmannJson>();
175 [[nodiscard]]
inline int32_t getInt32(std::string_view key)
override
177 return m_json->operator[](key.data()).get<int32_t>();
180 [[nodiscard]]
inline uint32_t getUInt32(std::string_view key)
override
182 return m_json->operator[](key.data()).get<uint32_t>();
185 [[nodiscard]]
inline int64_t getInt64(std::string_view key)
override
187 return m_json->operator[](key.data()).get<int64_t>();
190 [[nodiscard]]
inline uint64_t getUInt64(std::string_view key)
override
192 return m_json->operator[](key.data()).get<uint64_t>();
195 [[nodiscard]]
inline double getDouble(std::string_view key)
override
197 return m_json->operator[](key.data()).get<double>();
200 [[nodiscard]]
inline std::string getString(std::string_view key)
override
202 return m_json->operator[](key.data()).get<std::string>();
205 [[nodiscard]]
inline bool getBool(std::string_view key)
override
207 return m_json->operator[](key.data()).get<bool>();
210 [[nodiscard]]
float getFloat(std::string_view key)
override
212 return m_json->operator[](key.data()).get<float>();
215 [[nodiscard]]
inline int32_t getInt32()
override
217 return m_json->get<int32_t>();
220 [[nodiscard]]
inline uint32_t getUInt32()
override
222 return m_json->get<uint32_t>();
225 [[nodiscard]]
inline int64_t getInt64()
override
227 return m_json->get<int64_t>();
230 [[nodiscard]]
inline uint64_t getUInt64()
override
232 return m_json->get<uint64_t>();
235 [[nodiscard]]
inline double getDouble()
override
237 return m_json->get<
double>();
240 [[nodiscard]]
inline std::string getString()
override
242 return m_json->get<std::string>();
245 [[nodiscard]]
inline bool getBool()
override
247 return m_json->get<
bool>();
250 [[nodiscard]]
float getFloat()
override
252 return m_json->get<
float>();
256 inline void clearCache()
258 m_arrayCache.clear();
259 m_arrayPosCache.clear();
260 m_arrayListDataCache.clear();
263 nlohmann::json *m_json =
nullptr;
264 std::unique_ptr<nlohmann::json> m_data =
nullptr;
268 std::map<std::string, std::unique_ptr<IJson>> m_arrayCache;
269 std::map<size_t, std::unique_ptr<IJson>> m_arrayPosCache;
270 std::map<std::string, std::vector<std::unique_ptr<IJson>>> m_arrayListDataCache;
Definition MemoryStream.hpp:12