5#ifndef TILESON_GASON_HPP
6#define TILESON_GASON_HPP
17 if(m_arrayCache.count(key.data()) == 0)
19 if(m_json.getTag() == gason::JSON_OBJECT)
21 m_arrayCache[key.data()] = std::make_unique<Gason>(m_objectCache[key.data()]);
25 return *m_arrayCache[key.data()].get();
28 inline explicit Gason(gason::JsonValue 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.getTag() == gason::JSON_OBJECT)
39 m_arrayCache[key.data()] = std::make_unique<Gason>(m_objectCache[key.data()]);
42 return *m_arrayCache[key.data()].get();
47 if(m_arrayPosCache.count(pos) == 0)
50 for (
auto item : m_json)
54 m_arrayPosCache[pos] = std::make_unique<Gason>(item->value);
60 return *m_arrayPosCache[pos];
63 std::vector<std::unique_ptr<IJson>>
array()
override
65 std::vector<std::unique_ptr<IJson>> vec;
66 if(m_json.getTag() == gason::JSON_ARRAY)
68 for (
auto item : m_json)
70 vec.emplace_back(std::make_unique<Gason>(item->value));
77 inline std::vector<std::unique_ptr<IJson>> &
array(std::string_view key)
override
79 if(m_arrayListDataCache.count(key.data()) == 0)
81 if(
count(key.data()) > 0)
85 gason::JsonValue v = m_objectCache[key.data()];
86 if(v.getTag() == gason::JSON_ARRAY)
90 m_arrayListDataCache[key.data()].emplace_back(std::make_unique<Gason>(item->value));
98 return m_arrayListDataCache[key.data()];
101 [[nodiscard]]
inline size_t size()
const override
104 return m_objectCache.size();
107 inline bool parse(
const fs::path &path)
override
111 if (fs::exists(path) && fs::is_regular_file(path))
113 std::ifstream file(path.generic_string());
115 m_path = path.parent_path();
117 file.seekg(0, std::ios::end);
118 str.reserve(
static_cast<size_t>(file.tellg()));
119 file.seekg(0, std::ios::beg);
121 str.assign((std::istreambuf_iterator<char>(file)),
122 std::istreambuf_iterator<char>());
124 m_data = std::make_unique<gason::JsonValue>();
125 m_allocator = std::make_unique<gason::JsonAllocator>();
129 int status = gason::jsonParse(str.data(), &m_endptr, m_data.get(), *m_allocator);
130 if (status != gason::JSON_OK) {
131 fprintf(stderr,
"%s at %zd\n", gason::jsonStrError(status), m_endptr - str.data());
137 catch (
const std::exception &error)
139 std::string message =
"Parse error: ";
140 message += std::string(error.what());
141 message += std::string(
"\n");
142 std::cerr << message;
150 inline bool parse(
const void *data,
size_t size)
override
159 str.assign((std::istreambuf_iterator<char>(mem)),
160 std::istreambuf_iterator<char>());
162 m_data = std::make_unique<gason::JsonValue>();
163 m_allocator = std::make_unique<gason::JsonAllocator>();
167 int status = gason::jsonParse(str.data(), &m_endptr, m_data.get(), *m_allocator);
168 if (status != gason::JSON_OK) {
169 fprintf(stderr,
"%s at %zd\n", gason::jsonStrError(status), m_endptr - str.data());
175 catch (
const std::exception &error)
177 std::string message =
"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
190 return m_objectCache.count(key.data());
196 [[nodiscard]]
inline bool any(std::string_view key)
const override
198 return count(key) > 0;
201 [[nodiscard]]
inline bool isArray()
const override
203 return m_json.getTag() == gason::JSON_ARRAY;
206 [[nodiscard]]
inline bool isObject()
const override
208 return m_json.getTag() == gason::JSON_OBJECT;
211 [[nodiscard]]
inline bool isNull()
const override
213 return m_json.getTag() == gason::JSON_NULL;
228 return std::make_unique<Gason>();
232 [[nodiscard]]
inline int32_t
getInt32(std::string_view key)
override
234 return static_cast<int32_t
>(
getDouble(key));
237 [[nodiscard]]
inline uint32_t
getUInt32(std::string_view key)
override
239 return static_cast<uint32_t
>(
getDouble(key));
242 [[nodiscard]]
inline int64_t
getInt64(std::string_view key)
override
244 return static_cast<int64_t
>(
getDouble(key));
247 [[nodiscard]]
inline uint64_t
getUInt64(std::string_view key)
override
249 return static_cast<uint64_t
>(
getDouble(key));
252 [[nodiscard]]
inline double getDouble(std::string_view key)
override
254 return obj(key.data())->toNumber();
257 [[nodiscard]]
inline std::string
getString(std::string_view key)
override
259 return obj(key.data())->toString();
262 [[nodiscard]]
inline bool getBool(std::string_view key)
override
264 return obj(key.data())->getTag() == gason::JSON_TRUE;
267 [[nodiscard]]
float getFloat(std::string_view key)
override
269 return static_cast<float>(
getDouble(key));
274 return static_cast<int32_t
>(
getDouble());
279 return static_cast<uint32_t
>(
getDouble());
284 return static_cast<int64_t
>(
getDouble());
289 return static_cast<uint64_t
>(
getDouble());
294 return m_json.toNumber();
299 return m_json.toString();
304 return m_json.getTag() == gason::JSON_TRUE;
317 const gason::JsonValue *obj(std::string_view key)
320 return &m_objectCache[key.data()];
323 void createObjCache()
325 if(m_objectCache.empty())
327 if(m_json.getTag() == gason::JSON_OBJECT)
329 for (
auto i : m_json)
331 m_objectCache[i->key] = i->value;
337 inline void clearCache()
339 m_objectCache.clear();
340 m_arrayCache.clear();
341 m_arrayPosCache.clear();
342 m_arrayListDataCache.clear();
347 std::unique_ptr<gason::JsonValue> m_data =
nullptr;
348 std::unique_ptr<gason::JsonAllocator> m_allocator =
nullptr;
350 gason::JsonValue m_json;
354 std::map<std::string, gason::JsonValue> m_objectCache;
356 std::map<std::string, std::unique_ptr<IJson>> m_arrayCache;
357 std::map<size_t, std::unique_ptr<IJson>> m_arrayPosCache;
358 std::map<std::string, std::vector<std::unique_ptr<IJson>>> m_arrayListDataCache;
IJson & at(size_t pos) override
Definition Gason.hpp:45
std::string getString() override
Definition Gason.hpp:297
bool isArray() const override
Definition Gason.hpp:201
uint64_t getUInt64(std::string_view key) override
Definition Gason.hpp:247
std::unique_ptr< IJson > create() override
Definition Gason.hpp:226
int64_t getInt64(std::string_view key) override
Definition Gason.hpp:242
std::vector< std::unique_ptr< IJson > > & array(std::string_view key) override
Definition Gason.hpp:77
int64_t getInt64() override
Definition Gason.hpp:282
std::vector< std::unique_ptr< IJson > > array() override
Definition Gason.hpp:63
float getFloat(std::string_view key) override
Definition Gason.hpp:267
uint32_t getUInt32(std::string_view key) override
Definition Gason.hpp:237
bool parse(const void *data, size_t size) override
Definition Gason.hpp:150
bool isObject() const override
Definition Gason.hpp:206
double getDouble() override
Definition Gason.hpp:292
double getDouble(std::string_view key) override
Definition Gason.hpp:252
Gason(gason::JsonValue json)
Definition Gason.hpp:28
bool getBool(std::string_view key) override
Definition Gason.hpp:262
int32_t getInt32() override
Definition Gason.hpp:272
bool any(std::string_view key) const override
Definition Gason.hpp:196
void directory(const fs::path &directory) override
Definition Gason.hpp:221
fs::path directory() const override
Definition Gason.hpp:216
float getFloat() override
Definition Gason.hpp:307
bool getBool() override
Definition Gason.hpp:302
bool parse(const fs::path &path) override
Definition Gason.hpp:107
int32_t getInt32(std::string_view key) override
Definition Gason.hpp:232
IJson & at(std::string_view key) override
Definition Gason.hpp:33
size_t size() const override
Definition Gason.hpp:101
uint32_t getUInt32() override
Definition Gason.hpp:277
uint64_t getUInt64() override
Definition Gason.hpp:287
std::string getString(std::string_view key) override
Definition Gason.hpp:257
size_t count(std::string_view key) const override
Definition Gason.hpp:186
bool isNull() const override
Definition Gason.hpp:211
IJson & operator[](std::string_view key) override
Definition Gason.hpp:15
Definition MemoryStream.hpp:12