Tileson 1.4.0
A helpful json parser for Tiled maps
Loading...
Searching...
No Matches
Json11.hpp
Go to the documentation of this file.
1//
2// Created by robin on 16.01.2021.
3//
4
5#ifndef TILESON_JSON11_HPP
6#define TILESON_JSON11_HPP
7
8namespace tson
9{
10 class Json11 : public tson::IJson
11 {
12 public:
13 inline Json11() = default;
14
15 IJson &operator[](std::string_view key) override
16 {
17 if(m_arrayCache.count(key.data()) == 0)
18 {
19 if(m_json->is_object())
20 {
21 m_arrayCache[key.data()] = std::make_unique<Json11>(m_json->operator[](key.data()));
22 }
23 }
24
25 return *m_arrayCache[key.data()].get();
26 }
27
28 inline explicit Json11(const json11::Json &json) : m_json {&json}
29 {
30
31 }
32
33 inline IJson& at(std::string_view key) override
34 {
35 if(m_arrayCache.count(key.data()) == 0)
36 {
37 if(m_json->is_object())
38 {
39 m_arrayCache[key.data()] = std::make_unique<Json11>(m_json->operator[](key.data()));
40 }
41 }
42 return *m_arrayCache[key.data()].get();
43 }
44
45 inline IJson& at(size_t pos) override
46 {
47 if(m_arrayPosCache.count(pos) == 0)
48 {
49 const std::vector<json11::Json> &a = m_json->array_items();
50 m_arrayPosCache[pos] = std::make_unique<Json11>(a.at(pos));
51 }
52
53 return *m_arrayPosCache[pos];
54 }
55
56 std::vector<std::unique_ptr<IJson>> array() override
57 {
58 std::vector<std::unique_ptr<IJson>> vec;
59 if(m_json->is_array())
60 {
61 for (const json11::Json &item : m_json->array_items())
62 {
63 vec.emplace_back(std::make_unique<Json11>(item));
64 }
65 }
66
67 return vec;
68 }
69
70 inline std::vector<std::unique_ptr<IJson>> &array(std::string_view key) override
71 {
72 if(m_arrayListDataCache.count(key.data()) == 0)
73 {
74 if(count(key.data()) > 0)
75 {
76 if(isObject())
77 {
78 const json11::Json &v = m_json->operator[](key.data());
79 if(v.is_array())
80 {
81 for (const json11::Json &item : v.array_items())
82 {
83 m_arrayListDataCache[key.data()].emplace_back(std::make_unique<Json11>(item));
84 }
85 }
86 }
87 }
88 }
89
90
91 return m_arrayListDataCache[key.data()];
92 }
93
94 [[nodiscard]] inline size_t size() const override
95 {
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();
100
101 return 0;
102 }
103
104 inline bool parse(const fs::path &path) override
105 {
106 clearCache();
107 m_data = nullptr;
108 m_json = nullptr;
109 if (fs::exists(path) && fs::is_regular_file(path))
110 {
111 std::ifstream file(path.generic_string());
112 std::string str;
113 m_path = path.parent_path();
114
115 file.seekg(0, std::ios::end);
116 str.reserve(static_cast<size_t>(file.tellg()));
117 file.seekg(0, std::ios::beg);
118
119 str.assign((std::istreambuf_iterator<char>(file)),
120 std::istreambuf_iterator<char>());
121
122 m_data = std::make_unique<json11::Json>();
123
124 try
125 {
126 std::string strError;
127 *m_data = json11::Json::parse(str, strError);
128 if(!strError.empty())
129 {
130 std::cerr << strError << "\n";
131 return false;
132 }
133 m_json = m_data.get();
134 }
135 catch (const std::exception &error)
136 {
137 std::string message = "Json11 parse error: ";
138 message += std::string(error.what());
139 message += std::string("\n");
140 std::cerr << message;
141 return false;
142 }
143 return true;
144 }
145 return false;
146 }
147
148 inline bool parse(const void *data, size_t size) override
149 {
150 clearCache();
151 m_json = nullptr;
152 std::string str;
153
154 str.reserve(size);
155
156 tson::MemoryStream mem{(uint8_t *) data, size};
157
158 str.assign((std::istreambuf_iterator<char>(mem)),
159 std::istreambuf_iterator<char>());
160
161 m_data = std::make_unique<json11::Json>();
162
163 try
164 {
165 std::string strError;
166
167 *m_data = json11::Json::parse(str, strError);
168 if(!strError.empty())
169 {
170 std::cout << strError << "\n";
171 return false;
172 }
173 m_json = m_data.get();
174 }
175 catch (const std::exception &error)
176 {
177 std::string message = "Json11 parse error: ";
178 message += std::string(error.what());
179 message += std::string("\n");
180 std::cerr << message;
181 return false;
182 }
183 return true;
184 }
185
186 [[nodiscard]] inline size_t count(std::string_view key) const override
187 {
188 if (isObject())
189 {
190 //const json11::Json &j = m_json->operator[](key.data());
191 //size_t s1 = j.object_items().size();
192 return m_json->object_items().count(key.data());
193 }
194
195 return 0;
196 }
197
198 [[nodiscard]] inline bool any(std::string_view key) const override
199 {
200 return count(key) > 0;
201 }
202
203 [[nodiscard]] inline bool isArray() const override
204 {
205 return m_json->is_array();
206 }
207
208 [[nodiscard]] inline bool isObject() const override
209 {
210 return m_json->is_object();
211 }
212
213 [[nodiscard]] inline bool isNull() const override
214 {
215 return m_json->is_null();
216 }
217
218 fs::path directory() const override
219 {
220 return m_path;
221 }
222
223 void directory(const fs::path &directory) override
224 {
225 m_path = directory;
226 }
227
228 std::unique_ptr<IJson> create() override
229 {
230 return std::make_unique<Json11>();
231 }
232 protected:
233 [[nodiscard]] inline int32_t getInt32(std::string_view key) override
234 {
235 return static_cast<int32_t>(getDouble(key));
236 }
237
238 [[nodiscard]] inline uint32_t getUInt32(std::string_view key) override
239 {
240 return static_cast<uint32_t>(getDouble(key));
241 }
242
243 [[nodiscard]] inline int64_t getInt64(std::string_view key) override
244 {
245 return static_cast<int64_t>(getDouble(key));
246 }
247
248 [[nodiscard]] inline uint64_t getUInt64(std::string_view key) override
249 {
250 return static_cast<uint64_t>(getDouble(key));
251 }
252
253 [[nodiscard]] inline double getDouble(std::string_view key) override
254 {
255 return m_json->operator[](key.data()).number_value();
256 }
257
258 [[nodiscard]] inline std::string getString(std::string_view key) override
259 {
260 return m_json->operator[](key.data()).string_value(); // .get<std::string>();
261 }
262
263 [[nodiscard]] inline bool getBool(std::string_view key) override
264 {
265 return m_json->operator[](key.data()).bool_value();
266 }
267
268 [[nodiscard]] float getFloat(std::string_view key) override
269 {
270 return static_cast<float>(getDouble(key));
271 }
272
273 [[nodiscard]] inline int32_t getInt32() override
274 {
275 return static_cast<int32_t>(getDouble());
276 }
277
278 [[nodiscard]] inline uint32_t getUInt32() override
279 {
280 return static_cast<uint32_t>(getDouble());
281 }
282
283 [[nodiscard]] inline int64_t getInt64() override
284 {
285 return static_cast<int64_t>(getDouble());
286 }
287
288 [[nodiscard]] inline uint64_t getUInt64() override
289 {
290 return static_cast<uint64_t>(getDouble());
291 }
292
293 [[nodiscard]] inline double getDouble() override
294 {
295 return m_json->number_value();
296 }
297
298 [[nodiscard]] inline std::string getString() override
299 {
300 return m_json->string_value();
301 }
302
303 [[nodiscard]] inline bool getBool() override
304 {
305 return m_json->bool_value();
306 }
307
308 [[nodiscard]] float getFloat() override
309 {
310 return static_cast<float>(getDouble());
311 }
312
313 private:
314
315 inline void clearCache()
316 {
317 m_arrayCache.clear();
318 m_arrayPosCache.clear();
319 m_arrayListDataCache.clear();
320 }
321
322 //Owner values
323 std::unique_ptr<json11::Json> m_data = nullptr; //Only used if this is the owner json!
324
325 const json11::Json *m_json = nullptr;
326 fs::path m_path;
327
328 //Cache!
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;
332
333 };
334}
335
336#endif //TILESON_JSON11_HPP
Definition IJson.hpp:11
Definition Json11.hpp:11
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
Json11()=default
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
Definition Base64.hpp:12