-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtinyjson.hpp
executable file
·365 lines (305 loc) · 12.9 KB
/
tinyjson.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#ifndef JSON_LITE_HPP
#define JSON_LITE_HPP
#include <cstring>
#include <memory>
#include <sstream>
#include <string>
#include <unordered_map>
#include <variant>
#include <vector>
namespace tinyjson
{
#define FLATTEN_INLINE inline __attribute__((flatten))
enum class element_kind { T_INVALID, T_TRUE, T_FALSE, T_STRING, T_NUMBER, T_OBJECT, T_ARRAY, T_NULL };
std::string& escape_string(const std::string_view& str, std::string* escaped);
union element_value {
char* str;
double number;
bool boolean;
};
struct element {
private:
/// the element's kind
element_kind m_kind = element_kind::T_INVALID;
/// if the Element has a name -> its here
std::string m_property_name;
/// the element's value
element_value m_value;
/// list of all children
std::vector<element> m_children;
/// provide `O(1)` access for elements by name
std::unordered_map<std::string, element*> m_elements_map;
private:
static const char* parse_string(tinyjson::element* item, const char* str);
static const char* parse_number(tinyjson::element* item, const char* num);
static const char* parse_array(tinyjson::element* item, const char* value);
static const char* parse_object(tinyjson::element* item, const char* value);
static const char* parse_value(tinyjson::element* item, const char* value);
void index_elements();
private:
/// append new item to the end of the children list and return a reference to it
FLATTEN_INLINE element& append_new()
{
element new_elem;
m_children.push_back(std::move(new_elem));
return m_children.back();
}
FLATTEN_INLINE const char* suffix(bool is_last, bool pretty) const
{
if (is_last) {
return pretty ? "\n" : "";
} else {
return pretty ? ",\n" : ",";
}
}
/// new property element with a given name
/// and return it. This method does not set the value
/// but it does add the newly added item to the index
/// table
element& add_property_internal(const std::string& name);
public:
/// construct json from string
static bool parse(const std::string& content, element* root);
/// construct json from file
static bool parse_file(const std::string& path, element* root);
static bool create_array(element* arr);
static bool create_object(element* obj);
element();
// no copy constructor is allowed, only `move`
element(element& other) = delete;
element(element&& other);
~element();
// Check functions
FLATTEN_INLINE bool is_array() const { return m_kind == element_kind::T_ARRAY; }
FLATTEN_INLINE bool is_object() const { return m_kind == element_kind::T_OBJECT; }
FLATTEN_INLINE bool is_string() const { return m_kind == element_kind::T_STRING; }
FLATTEN_INLINE bool is_number() const { return m_kind == element_kind::T_NUMBER; }
FLATTEN_INLINE bool is_true() const { return m_kind == element_kind::T_TRUE; }
FLATTEN_INLINE bool is_false() const { return m_kind == element_kind::T_FALSE; }
FLATTEN_INLINE bool is_null() const { return m_kind == element_kind::T_NULL; }
FLATTEN_INLINE bool is_ok() const { return m_kind != element_kind::T_INVALID; }
// "as" methods
// element.as<std::string>
/// return the value as a string
/// @param val [output]
/// @param default_value default value to return in case of an error
template <typename T> FLATTEN_INLINE bool as_str(T* val, const char* default_value = "") const
{
if (!is_string()) {
*val = default_value;
return false;
}
*val = m_value.str;
return true;
}
/// return the value as a number. return the `default_value` on error
template <typename T> FLATTEN_INLINE T to_str(const char* default_value = "") const
{
T value;
as_str(&value, default_value);
return std::move(value);
}
/// return the value as a number. return false on error
/// @param val [output]
/// @param default_value default value to return in case of an error
template <typename T> FLATTEN_INLINE bool as_number(T* val, int default_value = -1) const
{
if (!is_number()) {
*val = static_cast<T>(default_value);
return false;
}
*val = static_cast<T>(m_value.number);
return true;
}
/// return the value as a number. return the `default_value` on error
template <typename T> FLATTEN_INLINE T to_number(int default_value = -1) const
{
T value;
as_number(&value, default_value);
return value;
}
/// return the value as a bool
/// @param val [output]
/// @param default_value default value to return in case of an error
FLATTEN_INLINE bool as_bool(bool* val, bool default_value = false) const
{
switch (m_kind) {
case element_kind::T_FALSE:
*val = false;
return true;
case element_kind::T_TRUE:
*val = true;
return true;
default:
return false;
}
}
/// return the value as a bool. return the `default_value` on error
FLATTEN_INLINE bool to_bool(bool default_value = false) const
{
bool v;
as_bool(&v, default_value);
return v;
}
/// access element by name
const element& operator[](const char* index) const;
element& operator[](const char* index);
FLATTEN_INLINE const element& operator[](const std::string& index) const { return operator[](index.c_str()); }
FLATTEN_INLINE element& operator[](const std::string& index) { return operator[](index.c_str()); }
/// access element by position
const element& operator[](size_t index) const;
element& operator[](size_t index);
FLATTEN_INLINE const element& operator[](int index) const { return operator[](static_cast<size_t>(index)); }
FLATTEN_INLINE element& operator[](int index) { return operator[](static_cast<size_t>(index)); }
/// STL like api, so we can have `for` loops
FLATTEN_INLINE std::vector<element>::const_iterator begin() const { return m_children.begin(); }
FLATTEN_INLINE std::vector<element>::iterator begin() { return m_children.begin(); }
FLATTEN_INLINE std::vector<element>::const_iterator end() const { return m_children.end(); }
FLATTEN_INLINE std::vector<element>::iterator end() { return m_children.end(); }
FLATTEN_INLINE std::vector<element>::size_type size() const { return m_children.size(); }
/// return true if this Element has no children
FLATTEN_INLINE bool empty() const { return m_children.empty(); }
/// delete all children
FLATTEN_INLINE void clear()
{
m_children.clear();
m_elements_map.clear();
}
/// return true if this Element contains a child with a given name
bool contains(const char* name) const;
/// return true if this Element contains a child with a given name
bool contains(const std::string& name) const;
// write API
/// add new property to the `this`. return ref to `this`
/// @return reference to `this`
element& add_property(const std::string& name, double value);
/// add new property to the `this`. return ref to `this`
/// @return reference to `this`
element& add_property(const std::string& name, int value);
/// add new property to the `this`. return ref to `this`
/// @return reference to `this`
element& add_property(const std::string& name, long value);
/// add new property to the `this`. return ref to `this`
/// @return reference to `this`
element& add_property(const std::string& name, size_t value);
/// add new property to the `this`. return ref to `this`
/// @return reference to `this`
element& add_property(const std::string& name, const std::string& value);
/// add new property to the `this`. return ref to `this`
/// @return reference to `this`
element& add_property(const std::string& name, const char* value);
/// add new property to the `this`. return ref to `this`
/// @return reference to `this`
element& add_property(const std::string& name, bool b);
/// add new property to the `this`. return ref to `this`
/// @return reference to `this`
element& add_element(element&& elem);
/// add new property to the `this`. return ref to `this`
/// @return reference to `this`
element& add_property_null(const std::string& name);
/// add new array with a given name. return the newly added Element
/// @return the newly added object
element& add_array(const std::string& name);
/// add new object with a given name. return the newly added Element
/// @return the newly added object
element& add_object(const std::string& name);
/// add an Element of type string to the array, return the array
/// @return reference to `this`
element& add_array_item(const std::string& value);
/// add an Element of type string to the array, return the array
/// @return reference to `this`
element& add_array_item(const char* value);
/// add an Element of type double to the array, return the array
/// @return reference to `this`
element& add_array_item(double value);
/// add an Element of type bool to the array, return the array
/// @return reference to `this`
element& add_array_item(bool b);
/// add an Element of type string to the array, return the array
/// @return reference to `this`
element& add_array_item(element elem);
/// create new empty Element of type object and append it to the end of the array
/// @return the newly added object
element& add_array_object();
FLATTEN_INLINE const char* property_name() const
{
if (m_property_name.empty()) {
return nullptr;
}
return m_property_name.c_str();
}
FLATTEN_INLINE void to_string(std::ostream& ss, int depth, bool last_child, bool pretty) const
{
std::string indent(depth, ' ');
if (!pretty) {
indent.clear();
}
const std::string NEW_LINE = pretty ? "\n" : "";
ss << indent;
if (property_name()) {
ss << "\"" << property_name() << "\":" << (pretty ? " " : "");
}
switch (m_kind) {
case element_kind::T_STRING: {
std::string_view sv;
as_str(&sv);
if (sv.empty()) {
ss << R"("")" << suffix(last_child, pretty);
} else {
std::string escaped_str;
ss << escape_string(sv, &escaped_str) << suffix(last_child, pretty);
}
} break;
case element_kind::T_NUMBER: {
double d;
as_number(&d);
ss << d << suffix(last_child, pretty);
} break;
case element_kind::T_TRUE: {
ss << "true" << suffix(last_child, pretty);
} break;
case element_kind::T_FALSE: {
ss << "false" << suffix(last_child, pretty);
} break;
case element_kind::T_NULL: {
ss << "null" << suffix(last_child, pretty);
} break;
case element_kind::T_OBJECT: {
if (m_children.empty()) {
ss << "{}" << suffix(last_child, pretty);
} else {
ss << "{" << NEW_LINE;
for (size_t i = 0; i < m_children.size(); ++i) {
bool is_last = i == m_children.size() - 1;
m_children[i].to_string(ss, depth + 1, is_last, pretty);
}
ss << indent << "}" << suffix(last_child, pretty);
}
} break;
case element_kind::T_ARRAY: {
if (m_children.empty()) {
ss << "[]" << suffix(last_child, pretty);
} else {
ss << "[" << NEW_LINE;
for (size_t i = 0; i < m_children.size(); ++i) {
bool is_last = i == m_children.size() - 1;
m_children[i].to_string(ss, depth + 1, is_last, pretty);
}
ss << indent << "]" << suffix(last_child, pretty);
}
} break;
case element_kind::T_INVALID:
break;
}
}
};
FLATTEN_INLINE void to_string(const element& root, std::ostream& ss, bool pretty = true)
{
root.to_string(ss, 0, true, pretty);
}
/// For convenience. Same as calling `tinyjson::element::parse`
FLATTEN_INLINE bool parse(const std::string& content, element* root) { return element::parse(content, root); }
/// For convenience. Same as calling `tinyjson::element::parse_file`
FLATTEN_INLINE bool parse_file(const std::string& content, element* root) { return element::parse_file(content, root); }
} // namespace tinyjson
#endif // JSON_LITE_HPP