-
Notifications
You must be signed in to change notification settings - Fork 13
/
gason.hpp
314 lines (284 loc) · 9.35 KB
/
gason.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
/**
* @file gason.hpp
* a simple and fast JSon parser in plain C/C++ with no dependency.
*
*
* @author Ivan Vashchaev
* @version 1.0.0
* @date 2014-07-07
* based on this commit: ede29fc
* https://github.com/vivkin/gason
*
* @author amir zamani
* @version 2.1.0
* @date 2014-07-11
* https://github.com/azadkuh/gason--
*
* @author amir zamani
* @version 2.2.0
* @date 2014-11-24
* add latest features from gason/#d09cd7a
*
*/
#ifndef __GASON_HPP__
#define __GASON_HPP__
///////////////////////////////////////////////////////////////////////////////
#include <stdint.h>
#include <stddef.h>
#include <assert.h>
#include <string.h>
///////////////////////////////////////////////////////////////////////////////
namespace gason {
///////////////////////////////////////////////////////////////////////////////
#if __cplusplus <= 199711L
# define GASON_OLDTOOLCHAINS_SUPPORT
#endif
#if defined(GASON_OLDTOOLCHAINS_SUPPORT)
# ifndef nullptr
# define nullptr NULL
# endif
#endif // GASON_OLDTOOLCHAINS_SUPPORT
///////////////////////////////////////////////////////////////////////////////
/** tag (type) of each JSon element. */
enum JsonTag {
JSON_NUMBER = 0, ///< double (floating point) value
JSON_STRING, ///< string value
JSON_ARRAY, ///< an array value
JSON_OBJECT, ///< an object value
JSON_TRUE, ///< true value
JSON_FALSE, ///< false value
JSON_NULL = 0xF ///< null or invalid value
};
///////////////////////////////////////////////////////////////////////////////
struct JsonNode;
///////////////////////////////////////////////////////////////////////////////
/** JSon value of @sa JsonTag type. */
struct JsonValue {
union {
uint64_t ival;
double fval;
};
JsonValue(double x) : fval(x) {
}
JsonValue(JsonTag tag = JSON_NULL, void *payload = nullptr) {
assert((uintptr_t)payload <= JSON_VALUE_PAYLOAD_MASK);
ival = JSON_VALUE_NAN_MASK | ((uint64_t)tag << JSON_VALUE_TAG_SHIFT) | (uintptr_t)payload;
}
bool isNumber() const {
return getTag() == JSON_NUMBER;
}
bool isBoolean() const {
return getTag() == JSON_TRUE || getTag() == JSON_FALSE;
}
bool isString() const {
return getTag() == JSON_STRING;
}
bool isNode() const {
return getTag() == JSON_ARRAY || getTag() == JSON_OBJECT;
}
bool isArray() const {
return getTag() == JSON_ARRAY;
}
bool isObject() const {
return getTag() == JSON_OBJECT;
}
bool isDouble() const {
return (int64_t)ival <= (int64_t)JSON_VALUE_NAN_MASK;
}
JsonTag getTag() const {
return isDouble() ? JSON_NUMBER : JsonTag((ival >> JSON_VALUE_TAG_SHIFT) & JSON_VALUE_TAG_MASK);
}
int toInt(bool* ok = nullptr) const {
return (int) toNumber(ok);
}
double toNumber(bool* ok = nullptr) const {
if ( !checkType(isNumber(), ok) )
return 0.0;
return fval;
}
bool toBool(bool* ok = nullptr) const {
if ( !checkType(isBoolean(), ok) )
return false;
return getPayload() == JSON_TRUE;
}
char* toString(bool* ok = nullptr) const {
if ( !checkType(isString(), ok) )
return nullptr;
return (char *)getPayload();
}
JsonNode* toNode(bool* ok = nullptr) const {
if ( !checkType(isNode(), ok) )
return nullptr;
return (JsonNode *)getPayload();
}
/** returns true if this object is not NULL. */
operator bool()const {
return getTag() != JSON_NULL;
}
/** returns true if this object has typeof tag value. */
bool operator==(JsonTag tag) const {
return getTag() == tag;
}
/** returns true if this object is not typeof tag value. */
bool operator!=(JsonTag tag) const {
return getTag() != tag;
}
/** overloads @sa at. */
JsonValue operator[](size_t index) const {
return at(index);
}
/** overloads @sa child. */
JsonValue operator()(const char* keyName) const {
return child(keyName);
}
/** returns a child value associated with the key = keyName. */
JsonValue child(const char* keyName) const;
/** returns the item at index position i in the array. */
JsonValue at(size_t i) const;
protected:
uint64_t getPayload() const {
assert(!isDouble());
return ival & JSON_VALUE_PAYLOAD_MASK;
}
bool checkType(bool isMatched, bool *ok) const {
if ( ok == 0 ) {
assert(isMatched && "failed to convert a JsonValue because of type difference.");
return true;
}
*ok = isMatched;
return isMatched;
}
static const uint64_t JSON_VALUE_PAYLOAD_MASK = 0x00007FFFFFFFFFFFULL;
static const uint64_t JSON_VALUE_NAN_MASK = 0x7FF8000000000000ULL;
static const uint64_t JSON_VALUE_NULL = 0x7FFF800000000000ULL;
static const uint64_t JSON_VALUE_TAG_MASK = 0xF;
static const uint64_t JSON_VALUE_TAG_SHIFT = 47;
};
///////////////////////////////////////////////////////////////////////////////
/** a JsonNode is a JsonValue who is an array or object. */
struct JsonNode {
JsonValue value;
JsonNode* next;
char* key;
};
///////////////////////////////////////////////////////////////////////////////
struct JsonIterator {
JsonNode* p;
explicit JsonIterator(JsonNode* n = nullptr) : p(n) {
}
void operator++() {
p = p->next;
}
void operator++(int) {
p = p->next;
}
bool isValid()const {
return p != nullptr;
}
bool hasNext()const {
return p->next != nullptr;
}
bool operator==(const char* key) const {
return strncmp(p->key, key, strlen(key)) == 0;
}
bool operator!=(const JsonIterator &x) const {
return p != x.p;
}
JsonNode* operator*() const {
return p;
}
JsonNode* operator->() const {
return p;
}
};
inline JsonIterator begin(JsonValue o) {
bool bok;
return JsonIterator(o.toNode(&bok));
}
inline JsonIterator end(JsonValue) {
return JsonIterator(nullptr);
}
///////////////////////////////////////////////////////////////////////////////
enum JsonParseStatus {
JSON_PARSE_OK,
JSON_PARSE_BAD_NUMBER,
JSON_PARSE_BAD_STRING,
JSON_PARSE_BAD_IDENTIFIER,
JSON_PARSE_STACK_OVERFLOW,
JSON_PARSE_STACK_UNDERFLOW,
JSON_PARSE_MISMATCH_BRACKET,
JSON_PARSE_UNEXPECTED_CHARACTER,
JSON_PARSE_UNQUOTED_KEY,
JSON_PARSE_BREAKING_BAD,
JSON_PARSE_ALLOCATION_FAILURE
};
/** Automatic memory manager for parsed JsonValue.
* @sa jsonParse().
* used internally by jsonParse(). you do not need to manually handle this object.
* just keep allocator instance as long as you want to keep JsonValues.
*/
class JsonAllocator {
public:
JsonAllocator() : head(nullptr) {
}
~JsonAllocator() {
deallocate();
}
/** frees the allocated memory.
* you do not need to call this function. If for some reason you have to
* release the memory manually, beware that after deallocate(),
* all the parsed JsonValues are invalid.
*/
void deallocate();
#if defined(GASON_OLDTOOLCHAINS_SUPPORT)
private:
JsonAllocator(const JsonAllocator &);
JsonAllocator &operator=(const JsonAllocator &);
#else
JsonAllocator(const JsonAllocator &) = delete;
JsonAllocator &operator=(const JsonAllocator &) = delete;
JsonAllocator(JsonAllocator &&x) : head(x.head) {
x.head = nullptr;
}
JsonAllocator &operator=(JsonAllocator &&x) {
head = x.head;
x.head = nullptr;
return *this;
}
#endif
protected:
struct Zone;
Zone* head;
void* allocate(size_t size);
void reset();
friend JsonParseStatus jsonParse(char*, char**, JsonValue*, JsonAllocator&);
};
JsonParseStatus
jsonParse(char *str, char **endptr, JsonValue *value, JsonAllocator &allocator);
/** parses a JSon string and return root object or array.
* JsonValues are the index tree for actual data on jsonString. allocator object holds
* and manage these indices.
*
* to keep the JsonValue valid, you have to keep both jsonString (data)
* and allocator (instances) alive.
*
* to avoid calling alloc/free many times (a serious problem on embedded devices)
* the allocator is re-usable and you can hold and re-use instance for parsing
* as many as jsonStrings you want. JsonAllocator automatically expands
* if more memory is required and frees all memories automatically.
*
* @param jsonString the JSon string. this buffer will be modified by this function.
* @param root root object of JSon string. could be an object or array.
* @param allocator an instance to memory manager.
* this function calls allocator.reset() before parsing.
* @return returns JSON_PARSE_OK or a proper error code.
*/
inline JsonParseStatus
jsonParse(char* jsonString, JsonValue& root, JsonAllocator& allocator) {
char *endptr = nullptr;
return jsonParse(jsonString, &endptr, &root, allocator);
}
///////////////////////////////////////////////////////////////////////////////
} // namespace gason
///////////////////////////////////////////////////////////////////////////////
#endif // __GASON_HPP__