Tileson  1.3.0
A helpful json parser for Tiled maps
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 
8 namespace 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.u8string());
112  std::string str;
113  m_path = path.parent_path();
114 
115  file.seekg(0, std::ios::end);
116  str.reserve(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  protected:
229  [[nodiscard]] inline int32_t getInt32(std::string_view key) override
230  {
231  return static_cast<int32_t>(getDouble(key));
232  }
233 
234  [[nodiscard]] inline uint32_t getUInt32(std::string_view key) override
235  {
236  return static_cast<uint32_t>(getDouble(key));
237  }
238 
239  [[nodiscard]] inline int64_t getInt64(std::string_view key) override
240  {
241  return static_cast<int64_t>(getDouble(key));
242  }
243 
244  [[nodiscard]] inline uint64_t getUInt64(std::string_view key) override
245  {
246  return static_cast<uint64_t>(getDouble(key));
247  }
248 
249  [[nodiscard]] inline double getDouble(std::string_view key) override
250  {
251  return m_json->operator[](key.data()).number_value();
252  }
253 
254  [[nodiscard]] inline std::string getString(std::string_view key) override
255  {
256  return m_json->operator[](key.data()).string_value(); // .get<std::string>();
257  }
258 
259  [[nodiscard]] inline bool getBool(std::string_view key) override
260  {
261  return m_json->operator[](key.data()).bool_value();
262  }
263 
264  [[nodiscard]] float getFloat(std::string_view key) override
265  {
266  return static_cast<float>(getDouble(key));
267  }
268 
269  [[nodiscard]] inline int32_t getInt32() override
270  {
271  return static_cast<int32_t>(getDouble());
272  }
273 
274  [[nodiscard]] inline uint32_t getUInt32() override
275  {
276  return static_cast<uint32_t>(getDouble());
277  }
278 
279  [[nodiscard]] inline int64_t getInt64() override
280  {
281  return static_cast<int64_t>(getDouble());
282  }
283 
284  [[nodiscard]] inline uint64_t getUInt64() override
285  {
286  return static_cast<uint64_t>(getDouble());
287  }
288 
289  [[nodiscard]] inline double getDouble() override
290  {
291  return m_json->number_value();
292  }
293 
294  [[nodiscard]] inline std::string getString() override
295  {
296  return m_json->string_value();
297  }
298 
299  [[nodiscard]] inline bool getBool() override
300  {
301  return m_json->bool_value();
302  }
303 
304  [[nodiscard]] float getFloat() override
305  {
306  return static_cast<float>(getDouble());
307  }
308 
309  private:
310 
311  inline void clearCache()
312  {
313  m_arrayCache.clear();
314  m_arrayPosCache.clear();
315  m_arrayListDataCache.clear();
316  }
317 
318  //Owner values
319  char *m_endptr;
320  std::unique_ptr<json11::Json> m_data = nullptr; //Only used if this is the owner json!
321 
322  const json11::Json *m_json = nullptr;
323  fs::path m_path;
324 
325  //Cache!
326  std::map<std::string, std::unique_ptr<IJson>> m_arrayCache;
327  std::map<size_t, std::unique_ptr<IJson>> m_arrayPosCache;
328  std::map<std::string, std::vector<std::unique_ptr<IJson>>> m_arrayListDataCache;
329 
330  };
331 }
332 
333 #endif //TILESON_JSON11_HPP
Definition: IJson.hpp:11
Definition: Json11.hpp:11
int32_t getInt32() override
Definition: Json11.hpp:269
bool getBool(std::string_view key) override
Definition: Json11.hpp:259
bool parse(const void *data, size_t size) override
Definition: Json11.hpp:148
float getFloat(std::string_view key) override
Definition: Json11.hpp:264
Json11()=default
float getFloat() override
Definition: Json11.hpp:304
int64_t getInt64() override
Definition: Json11.hpp:279
uint64_t getUInt64(std::string_view key) override
Definition: Json11.hpp:244
bool getBool() override
Definition: Json11.hpp:299
uint64_t getUInt64() override
Definition: Json11.hpp:284
bool isNull() const override
Definition: Json11.hpp:213
double getDouble() override
Definition: Json11.hpp:289
std::vector< std::unique_ptr< IJson > > & array(std::string_view key) override
Definition: Json11.hpp:70
std::string getString() override
Definition: Json11.hpp:294
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:249
void directory(const fs::path &directory) override
Definition: Json11.hpp:223
uint32_t getUInt32() override
Definition: Json11.hpp:274
std::vector< std::unique_ptr< IJson > > array() override
Definition: Json11.hpp:56
IJson & operator[](std::string_view key) override
Definition: Json11.hpp:15
uint32_t getUInt32(std::string_view key) override
Definition: Json11.hpp:234
std::string getString(std::string_view key) override
Definition: Json11.hpp:254
IJson & at(size_t pos) override
Definition: Json11.hpp:45
IJson & at(std::string_view key) override
Definition: Json11.hpp:33
bool parse(const fs::path &path) override
Definition: Json11.hpp:104
bool isArray() const override
Definition: Json11.hpp:203
size_t size() const override
Definition: Json11.hpp:94
int32_t getInt32(std::string_view key) override
Definition: Json11.hpp:229
int64_t getInt64(std::string_view key) override
Definition: Json11.hpp:239
Definition: MemoryStream.hpp:12
Definition: Base64.hpp:12