Tileson  1.3.0
A helpful json parser for Tiled maps
NlohmannJson.hpp
Go to the documentation of this file.
1 //
2 // Created by robin on 08.01.2021.
3 //
4 
5 #ifdef INCLUDE_NLOHMANN_JSON_HPP_
6 
7 #ifndef TILESON_NLOHMANNJSON_HPP
8 #define TILESON_NLOHMANNJSON_HPP
9 
10 namespace tson
11 {
12  class NlohmannJson : public tson::IJson
13  {
14  public:
15  inline NlohmannJson() = default;
16 
17 
18  IJson &operator[](std::string_view key) override
19  {
20  if(m_arrayCache.count(key.data()) == 0)
21  m_arrayCache[key.data()] = std::make_unique<NlohmannJson>(&m_json->operator[](key.data()));//.front());
22 
23  return *m_arrayCache[key.data()].get();
24  }
25 
26  inline explicit NlohmannJson(nlohmann::json *json) : m_json {json}
27  {
28 
29  }
30 
31  inline IJson& at(std::string_view key) override
32  {
33  if(m_arrayCache.count(key.data()) == 0)
34  m_arrayCache[key.data()] = std::make_unique<NlohmannJson>(&m_json->operator[](key.data()));//.front());
35 
36  return *m_arrayCache[key.data()].get();
37  }
38 
39  inline IJson& at(size_t pos) override
40  {
41  if(m_arrayPosCache.count(pos) == 0)
42  m_arrayPosCache[pos] = std::make_unique<NlohmannJson>(&m_json->at(pos));
43 
44  return *m_arrayPosCache[pos];
45  }
46 
47  std::vector<std::unique_ptr<IJson>> array() override
48  {
49  std::vector<std::unique_ptr<IJson>> vec;
50  for(auto &item : *m_json)
51  {
52  nlohmann::json *ptr = &item;
53  vec.emplace_back(std::make_unique<NlohmannJson>(ptr));
54  }
55 
56  return vec;
57  }
58 
59  inline std::vector<std::unique_ptr<IJson>> &array(std::string_view key) override
60  {
61  if(m_arrayListDataCache.count(key.data()) == 0)
62  {
63  if (m_json->count(key.data()) > 0 && m_json->operator[](key.data()).is_array())
64  {
65  std::for_each(m_json->operator[](key.data()).begin(), m_json->operator[](key.data()).end(), [&](nlohmann::json &item)
66  {
67  nlohmann::json *ptr = &item;
68  m_arrayListDataCache[key.data()].emplace_back(std::make_unique<NlohmannJson>(ptr));
69  });
70  }
71  }
72 
73 
74  return m_arrayListDataCache[key.data()];
75  }
76 
77  [[nodiscard]] inline size_t size() const override
78  {
79  return m_json->size();
80  }
81 
82  inline bool parse(const fs::path &path) override
83  {
84  clearCache();
85  m_data = nullptr;
86  m_json = nullptr;
87  if (fs::exists(path) && fs::is_regular_file(path))
88  {
89  m_path = path.parent_path();
90  m_data = std::make_unique<nlohmann::json>();
91  std::ifstream i(path.u8string());
92  try
93  {
94  i >> *m_data;
95  m_json = m_data.get();
96  }
97  catch (const nlohmann::json::parse_error &error)
98  {
99  std::string message = "Parse error: ";
100  message += std::string(error.what());
101  message += std::string("\n");
102  std::cerr << message;
103  return false;
104  }
105  return true;
106  }
107  return false;
108  }
109 
110  inline bool parse(const void *data, size_t size) override
111  {
112  clearCache();
113  m_json = nullptr;
114  m_data = std::make_unique<nlohmann::json>();
115  tson::MemoryStream mem{(uint8_t *) data, size};
116  try
117  {
118  mem >> *m_data;
119  m_json = m_data.get();
120  }
121  catch (const nlohmann::json::parse_error &error)
122  {
123  std::string message = "Parse error: ";
124  message += std::string(error.what());
125  message += std::string("\n");
126  std::cerr << message;
127  return false;
128  }
129  return true;
130  }
131 
132  [[nodiscard]] inline size_t count(std::string_view key) const override
133  {
134  return m_json->count(key);
135  }
136 
137  [[nodiscard]] inline bool any(std::string_view key) const override
138  {
139  return count(key) > 0;
140  }
141 
142  [[nodiscard]] inline bool isArray() const override
143  {
144  return m_json->is_array();
145  }
146 
147  [[nodiscard]] inline bool isObject() const override
148  {
149  return m_json->is_object();
150  }
151 
152  [[nodiscard]] inline bool isNull() const override
153  {
154  return m_json->is_null();
155  }
156 
157  fs::path directory() const override
158  {
159  return m_path;
160  }
161 
162  void directory(const fs::path &directory) override
163  {
164  m_path = directory;
165  }
166 
167  protected:
168  [[nodiscard]] inline int32_t getInt32(std::string_view key) override
169  {
170  return m_json->operator[](key.data()).get<int32_t>();
171  }
172 
173  [[nodiscard]] inline uint32_t getUInt32(std::string_view key) override
174  {
175  return m_json->operator[](key.data()).get<uint32_t>();
176  }
177 
178  [[nodiscard]] inline int64_t getInt64(std::string_view key) override
179  {
180  return m_json->operator[](key.data()).get<int64_t>();
181  }
182 
183  [[nodiscard]] inline uint64_t getUInt64(std::string_view key) override
184  {
185  return m_json->operator[](key.data()).get<uint64_t>();
186  }
187 
188  [[nodiscard]] inline double getDouble(std::string_view key) override
189  {
190  return m_json->operator[](key.data()).get<double>();
191  }
192 
193  [[nodiscard]] inline std::string getString(std::string_view key) override
194  {
195  return m_json->operator[](key.data()).get<std::string>();
196  }
197 
198  [[nodiscard]] inline bool getBool(std::string_view key) override
199  {
200  return m_json->operator[](key.data()).get<bool>();
201  }
202 
203  [[nodiscard]] float getFloat(std::string_view key) override
204  {
205  return m_json->operator[](key.data()).get<float>();
206  }
207 
208  [[nodiscard]] inline int32_t getInt32() override
209  {
210  return m_json->get<int32_t>();
211  }
212 
213  [[nodiscard]] inline uint32_t getUInt32() override
214  {
215  return m_json->get<uint32_t>();
216  }
217 
218  [[nodiscard]] inline int64_t getInt64() override
219  {
220  return m_json->get<int64_t>();
221  }
222 
223  [[nodiscard]] inline uint64_t getUInt64() override
224  {
225  return m_json->get<uint64_t>();
226  }
227 
228  [[nodiscard]] inline double getDouble() override
229  {
230  return m_json->get<double>();
231  }
232 
233  [[nodiscard]] inline std::string getString() override
234  {
235  return m_json->get<std::string>();
236  }
237 
238  [[nodiscard]] inline bool getBool() override
239  {
240  return m_json->get<bool>();
241  }
242 
243  [[nodiscard]] float getFloat() override
244  {
245  return m_json->get<float>();
246  }
247 
248  private:
249  inline void clearCache()
250  {
251  m_arrayCache.clear();
252  m_arrayPosCache.clear();
253  m_arrayListDataCache.clear();
254  }
255 
256  nlohmann::json *m_json = nullptr;
257  std::unique_ptr<nlohmann::json> m_data = nullptr; //Only used if this is the owner json!
258  fs::path m_path;
259 
260  //Cache!
261  std::map<std::string, std::unique_ptr<IJson>> m_arrayCache;
262  std::map<size_t, std::unique_ptr<IJson>> m_arrayPosCache;
263  std::map<std::string, std::vector<std::unique_ptr<IJson>>> m_arrayListDataCache;
264 
265  };
266 }
267 #endif //TILESON_NLOHMANNJSON_HPP
268 
269 #endif //INCLUDE_NLOHMANN_JSON_HPP_
Definition: IJson.hpp:11
Definition: MemoryStream.hpp:12
Definition: Base64.hpp:12