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