5#ifndef TILESON_JSON11_HPP
6#define TILESON_JSON11_HPP
17 if(m_arrayCache.count(key.data()) == 0)
19 if(m_json->is_object())
21 m_arrayCache[key.data()] = std::make_unique<Json11>(m_json->operator[](key.data()));
25 return *m_arrayCache[key.data()].get();
28 inline explicit Json11(
const json11::Json &json) : m_json {&json}
33 inline IJson&
at(std::string_view key)
override
35 if(m_arrayCache.count(key.data()) == 0)
37 if(m_json->is_object())
39 m_arrayCache[key.data()] = std::make_unique<Json11>(m_json->operator[](key.data()));
42 return *m_arrayCache[key.data()].get();
47 if(m_arrayPosCache.count(pos) == 0)
49 const std::vector<json11::Json> &a = m_json->array_items();
50 m_arrayPosCache[pos] = std::make_unique<Json11>(a.at(pos));
53 return *m_arrayPosCache[pos];
56 std::vector<std::unique_ptr<IJson>>
array()
override
58 std::vector<std::unique_ptr<IJson>> vec;
59 if(m_json->is_array())
61 for (
const json11::Json &item : m_json->array_items())
63 vec.emplace_back(std::make_unique<Json11>(item));
70 inline std::vector<std::unique_ptr<IJson>> &
array(std::string_view key)
override
72 if(m_arrayListDataCache.count(key.data()) == 0)
74 if(
count(key.data()) > 0)
78 const json11::Json &v = m_json->operator[](key.data());
81 for (
const json11::Json &item : v.array_items())
83 m_arrayListDataCache[key.data()].emplace_back(std::make_unique<Json11>(item));
91 return m_arrayListDataCache[key.data()];
94 [[nodiscard]]
inline size_t size()
const override
96 if(m_json->is_object())
97 return m_json->object_items().size();
98 else if(m_json->is_array())
99 return m_json->array_items().size();
104 inline bool parse(
const fs::path &path)
override
109 if (fs::exists(path) && fs::is_regular_file(path))
111 std::ifstream file(path.generic_string());
113 m_path = path.parent_path();
115 file.seekg(0, std::ios::end);
116 str.reserve(
static_cast<size_t>(file.tellg()));
117 file.seekg(0, std::ios::beg);
119 str.assign((std::istreambuf_iterator<char>(file)),
120 std::istreambuf_iterator<char>());
122 m_data = std::make_unique<json11::Json>();
126 std::string strError;
127 *m_data = json11::Json::parse(str, strError);
128 if(!strError.empty())
130 std::cerr << strError <<
"\n";
133 m_json = m_data.get();
135 catch (
const std::exception &error)
137 std::string message =
"Json11 parse error: ";
138 message += std::string(error.what());
139 message += std::string(
"\n");
140 std::cerr << message;
148 inline bool parse(
const void *data,
size_t size)
override
158 str.assign((std::istreambuf_iterator<char>(mem)),
159 std::istreambuf_iterator<char>());
161 m_data = std::make_unique<json11::Json>();
165 std::string strError;
167 *m_data = json11::Json::parse(str, strError);
168 if(!strError.empty())
170 std::cout << strError <<
"\n";
173 m_json = m_data.get();
175 catch (
const std::exception &error)
177 std::string message =
"Json11 parse error: ";
178 message += std::string(error.what());
179 message += std::string(
"\n");
180 std::cerr << message;
186 [[nodiscard]]
inline size_t count(std::string_view key)
const override
192 return m_json->object_items().count(key.data());
198 [[nodiscard]]
inline bool any(std::string_view key)
const override
200 return count(key) > 0;
203 [[nodiscard]]
inline bool isArray()
const override
205 return m_json->is_array();
208 [[nodiscard]]
inline bool isObject()
const override
210 return m_json->is_object();
213 [[nodiscard]]
inline bool isNull()
const override
215 return m_json->is_null();
230 return std::make_unique<Json11>();
233 [[nodiscard]]
inline int32_t
getInt32(std::string_view key)
override
235 return static_cast<int32_t
>(
getDouble(key));
238 [[nodiscard]]
inline uint32_t
getUInt32(std::string_view key)
override
240 return static_cast<uint32_t
>(
getDouble(key));
243 [[nodiscard]]
inline int64_t
getInt64(std::string_view key)
override
245 return static_cast<int64_t
>(
getDouble(key));
248 [[nodiscard]]
inline uint64_t
getUInt64(std::string_view key)
override
250 return static_cast<uint64_t
>(
getDouble(key));
253 [[nodiscard]]
inline double getDouble(std::string_view key)
override
255 return m_json->operator[](key.data()).number_value();
258 [[nodiscard]]
inline std::string
getString(std::string_view key)
override
260 return m_json->operator[](key.data()).string_value();
263 [[nodiscard]]
inline bool getBool(std::string_view key)
override
265 return m_json->operator[](key.data()).bool_value();
268 [[nodiscard]]
float getFloat(std::string_view key)
override
270 return static_cast<float>(
getDouble(key));
275 return static_cast<int32_t
>(
getDouble());
280 return static_cast<uint32_t
>(
getDouble());
285 return static_cast<int64_t
>(
getDouble());
290 return static_cast<uint64_t
>(
getDouble());
295 return m_json->number_value();
300 return m_json->string_value();
305 return m_json->bool_value();
315 inline void clearCache()
317 m_arrayCache.clear();
318 m_arrayPosCache.clear();
319 m_arrayListDataCache.clear();
323 std::unique_ptr<json11::Json> m_data =
nullptr;
325 const json11::Json *m_json =
nullptr;
329 std::map<std::string, std::unique_ptr<IJson>> m_arrayCache;
330 std::map<size_t, std::unique_ptr<IJson>> m_arrayPosCache;
331 std::map<std::string, std::vector<std::unique_ptr<IJson>>> m_arrayListDataCache;
int32_t getInt32() override
Definition Json11.hpp:273
bool getBool(std::string_view key) override
Definition Json11.hpp:263
IJson & at(std::string_view key) override
Definition Json11.hpp:33
bool parse(const void *data, size_t size) override
Definition Json11.hpp:148
float getFloat(std::string_view key) override
Definition Json11.hpp:268
float getFloat() override
Definition Json11.hpp:308
int64_t getInt64() override
Definition Json11.hpp:283
uint64_t getUInt64(std::string_view key) override
Definition Json11.hpp:248
bool getBool() override
Definition Json11.hpp:303
uint64_t getUInt64() override
Definition Json11.hpp:288
bool isNull() const override
Definition Json11.hpp:213
double getDouble() override
Definition Json11.hpp:293
std::string getString() override
Definition Json11.hpp:298
size_t count(std::string_view key) const override
Definition Json11.hpp:186
bool any(std::string_view key) const override
Definition Json11.hpp:198
fs::path directory() const override
Definition Json11.hpp:218
bool isObject() const override
Definition Json11.hpp:208
Json11(const json11::Json &json)
Definition Json11.hpp:28
double getDouble(std::string_view key) override
Definition Json11.hpp:253
void directory(const fs::path &directory) override
Definition Json11.hpp:223
std::vector< std::unique_ptr< IJson > > & array(std::string_view key) override
Definition Json11.hpp:70
uint32_t getUInt32() override
Definition Json11.hpp:278
uint32_t getUInt32(std::string_view key) override
Definition Json11.hpp:238
std::string getString(std::string_view key) override
Definition Json11.hpp:258
IJson & at(size_t pos) override
Definition Json11.hpp:45
bool parse(const fs::path &path) override
Definition Json11.hpp:104
std::vector< std::unique_ptr< IJson > > array() override
Definition Json11.hpp:56
bool isArray() const override
Definition Json11.hpp:203
std::unique_ptr< IJson > create() override
Definition Json11.hpp:228
size_t size() const override
Definition Json11.hpp:94
int32_t getInt32(std::string_view key) override
Definition Json11.hpp:233
int64_t getInt64(std::string_view key) override
Definition Json11.hpp:243
IJson & operator[](std::string_view key) override
Definition Json11.hpp:15
Definition MemoryStream.hpp:12