1 #ifndef JSONNODE_H
2 #define JSONNODE_H
3
4 #include "JSONDebug.h" //for string type
5 #include "internalJSONNode.h" //internal structure for json value
6 #include <stdexcept>
7 #include <cstdarg> //for the ... parameter
8
9 #ifdef JSON_BINARY
10 #include "JSON_Base64.h"
11 #endif
12
13 #ifdef JSON_LESS_MEMORY
14 #ifdef __GNUC__
15 #pragma pack(push, 1)
16 #elif _MSC_VER
17 #pragma pack(push, JSONNode_pack, 1)
18 #endif
19 #endif
20
21 #ifndef JSON_REF_COUNT
22 #define makeUniqueInternal() (void)0
23 #endif
24
25 #define JSON_CHECK_INTERNAL() JSON_ASSERT(internal != 0, JSON_TEXT("no internal"))
26
27 #ifdef JSON_MUTEX_CALLBACKS
28 #define JSON_MUTEX_COPY_DECL ,void * parentMutex
29 #define JSON_MUTEX_COPY_DECL2 ,void * parentMutex = 0
30 #else
31 #define JSON_MUTEX_COPY_DECL
32 #define JSON_MUTEX_COPY_DECL2
33 #endif
34
35 #ifdef JSON_LIBRARY
36 #define JSON_PTR_LIB *
37 #define JSON_NEW(x) JSONNode::newJSONNode_Shallow(x)
38
39
40 #define DECLARE_FOR_ALL_TYPES(foo)\
41 foo(json_int_t)json_nothrow;\
42 foo(json_number) json_nothrow;\
43 foo(bool) json_nothrow;\
44 foo(const json_string &) json_nothrow;
45
46 #define DECLARE_FOR_ALL_CAST_TYPES_CONST(foo)\
47 foo(json_int_t) const json_nothrow;\
48 foo(json_number) const json_nothrow;\
49 foo(bool) const json_nothrow;\
50 foo(const json_string &) const json_nothrow;\
51
52 #define DECLARE_FOR_ALL_TYPES_CONST(foo)\
53 DECLARE_FOR_ALL_CAST_TYPES_CONST(foo)\
54 foo(const JSONNode &) const json_nothrow;
55
56 #define IMPLEMENT_FOR_ALL_NUMBERS(foo)\
57 foo(json_int_t)\
58 foo(json_number)
59
60
61 #else
62 #define JSON_PTR_LIB
63 #define JSON_NEW(x) x
64
65 #ifdef JSON_ISO_STRICT
66 #define DECLARE_FOR_LONG_LONG(foo)
67 #define DECLARE_FOR_LONG_LONG_CONST(foo)
68 #define IMPLEMENT_FOR_LONG_LONG(foo)
69 #define DECLARE_FOR_LONG_DOUBLE(foo)
70 #define DECLARE_FOR_LONG_DOUBLE_CONST(foo)
71 #define IMPLEMENT_FOR_LONG_DOUBLE(foo)
72 #else
73 #define DECLARE_FOR_LONG_LONG(foo) foo(long long) json_nothrow; foo(unsigned long long) json_nothrow;
74 #define DECLARE_FOR_LONG_LONG_CONST(foo) foo(long long) const json_nothrow; foo(unsigned long long) const json_nothrow;
75 #define IMPLEMENT_FOR_LONG_LONG(foo) foo(long long) foo(unsigned long long)
76 #define DECLARE_FOR_LONG_DOUBLE(foo) foo(long double) json_nothrow;
77 #define DECLARE_FOR_LONG_DOUBLE_CONST(foo) foo(long double) const json_nothrow;
78 #define IMPLEMENT_FOR_LONG_DOUBLE(foo) foo(long double)
79 #endif
80
81 #define DECLARE_FOR_ALL_TYPES(foo)\
82 foo(char) json_nothrow; foo(unsigned char) json_nothrow;\
83 foo(short) json_nothrow; foo(unsigned short) json_nothrow;\
84 foo(int) json_nothrow; foo(unsigned int) json_nothrow;\
85 foo(long) json_nothrow; foo(unsigned long) json_nothrow;\
86 foo(float) json_nothrow; foo(double) json_nothrow;\
87 foo(bool) json_nothrow;\
88 foo(const json_string &) json_nothrow;\
89 foo(const json_char *) json_nothrow;\
90 DECLARE_FOR_LONG_LONG(foo)\
91 DECLARE_FOR_LONG_DOUBLE(foo)
92
93 #define DECLARE_FOR_ALL_CAST_TYPES_CONST(foo)\
94 foo(char) const json_nothrow; foo(unsigned char) const json_nothrow;\
95 foo(short) const json_nothrow; foo(unsigned short) const json_nothrow;\
96 foo(int) const json_nothrow; foo(unsigned int) const json_nothrow;\
97 foo(long) const json_nothrow; foo(unsigned long) const json_nothrow;\
98 foo(float) const json_nothrow; foo(double) const json_nothrow;\
99 foo(bool) const json_nothrow;\
100 foo(const json_string &) const json_nothrow;\
101 DECLARE_FOR_LONG_LONG_CONST(foo)\
102 DECLARE_FOR_LONG_DOUBLE_CONST(foo)
103
104 #define DECLARE_FOR_ALL_TYPES_CONST(foo)\
105 DECLARE_FOR_ALL_CAST_TYPES_CONST(foo)\
106 foo(const JSONNode &) const json_nothrow;\
107 foo(const json_char *) const json_nothrow;
108
109 #define IMPLEMENT_FOR_ALL_NUMBERS(foo)\
110 foo(char) foo(unsigned char)\
111 foo(short) foo(unsigned short)\
112 foo(int) foo(unsigned int)\
113 foo(long) foo(unsigned long)\
114 foo(float) foo(double)\
115 IMPLEMENT_FOR_LONG_LONG(foo)\
116 IMPLEMENT_FOR_LONG_DOUBLE(foo)
117
118 #endif
119
120 #define IMPLEMENT_FOR_ALL_TYPES(foo)\
121 IMPLEMENT_FOR_ALL_NUMBERS(foo)\
122 foo(const json_string &)\
123 foo(bool)
124
125 /*
126 This class is mostly just a wrapper class around internalJSONNode, this class keeps
127 the reference count and handles copy on write and such. This class is also responsible
128 for argument checking and throwing exceptions if needed.
129 */
130
131
132 class JSONNode {
133 public:
134 LIBJSON_OBJECT(JSONNode);
135 explicit JSONNode(char mytype = JSON_NODE) json_nothrow json_hot;
136 #define DECLARE_CTOR(type) explicit JSONNode(const json_string & name_t, type value_t)
137 DECLARE_FOR_ALL_TYPES(DECLARE_CTOR)
138
139 JSONNode(const JSONNode & orig) json_nothrow json_hot;
140 ~JSONNode(void) json_nothrow json_hot;
141
142 #if (defined(JSON_PREPARSE) && defined(JSON_READ_PRIORITY))
143 static JSONNode stringType(const json_string & str);
144 void set_name_(const json_string & newname) json_nothrow json_write_priority;
145 #endif
146
147 json_index_t size(void) const json_nothrow json_read_priority;
148 bool empty(void) const json_nothrow json_read_priority;
149 void clear(void) json_nothrow json_cold;
150 unsigned char type(void) const json_nothrow json_read_priority;
151
152 json_string name(void) const json_nothrow json_read_priority;
153 void set_name(const json_string & newname) json_nothrow json_write_priority;
154 #ifdef JSON_COMMENTS
155 void set_comment(const json_string & comment) json_nothrow;
156 json_string get_comment(void) const json_nothrow;
157 #endif
158 #if !defined(JSON_PREPARSE) && defined(JSON_READ_PRIORITY)
159 void preparse(void) json_nothrow json_read_priority;
160 #endif
161
162
163 json_string as_string(void) const json_nothrow json_read_priority;
164 json_int_t as_int(void) const json_nothrow json_read_priority;
165 json_number as_float(void) const json_nothrow json_read_priority;
166 bool as_bool(void) const json_nothrow json_read_priority;
167
168 #ifdef JSON_CASTABLE
169 JSONNode as_node(void) const json_nothrow json_read_priority;
170 JSONNode as_array(void) const json_nothrow json_read_priority;
171 void cast(char newtype) json_nothrow;
172 #endif
173
174 #ifdef JSON_BINARY
175 std::string as_binary(void) const json_nothrow json_cold;
176 void set_binary(const unsigned char * bin, size_t bytes) json_nothrow json_cold;
177 #endif
178
179 JSONNode & at(json_index_t pos) json_throws(std::out_of_range);
180 const JSONNode & at(json_index_t pos) const json_throws(std::out_of_range);
181
182 JSONNode & operator[](json_index_t pos) json_nothrow;
183 const JSONNode & operator[](json_index_t pos) const json_nothrow;
184
185 JSONNode & at(const json_string & name_t) json_throws(std::out_of_range);
186 const JSONNode & at(const json_string & name_t) const json_throws(std::out_of_range);
187 #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS
188 JSONNode & at_nocase(const json_string & name_t) json_throws(std::out_of_range);
189 const JSONNode & at_nocase(const json_string & name_t) const json_throws(std::out_of_range);
190 #endif
191 JSONNode & operator[](const json_string & name_t) json_nothrow;
192 const JSONNode & operator[](const json_string & name_t) const json_nothrow;
193
194 #ifdef JSON_LIBRARY
195 void push_back(JSONNode * node) json_nothrow;
196 #else
197 void push_back(const JSONNode & node) json_nothrow;
198 #endif
199 void reserve(json_index_t siz) json_nothrow;
200 JSONNode JSON_PTR_LIB pop_back(json_index_t pos) json_throws(std::out_of_range);
201 JSONNode JSON_PTR_LIB pop_back(const json_string & name_t) json_throws(std::out_of_range);
202 #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS
203 JSONNode JSON_PTR_LIB pop_back_nocase(const json_string & name_t) json_throws(std::out_of_range);
204 #endif
205
206 DECLARE_FOR_ALL_TYPES(JSONNode & operator =)
207 JSONNode & operator = (const JSONNode &) json_nothrow;
208
209 DECLARE_FOR_ALL_TYPES_CONST(bool operator ==)
210 DECLARE_FOR_ALL_TYPES_CONST(bool operator !=)
211
212
213 void nullify(void) json_nothrow;
214 void swap(JSONNode & other) json_nothrow;
215 void merge(JSONNode & other) json_nothrow json_cold;
216 void merge(unsigned int num, ...) json_nothrow json_cold;
217 JSONNode duplicate(void) const json_nothrow;
218
219
220 //iterator
221 #ifdef JSON_ITERATORS
222 #ifndef JSON_LIBRARY
223 #define json_iterator_ptr(iter) iter.it
224 #define ptr_to_json_iterator(iter) json_iterator(iter)
225
226 struct iterator;
227 struct const_iterator {
228 inline const_iterator& operator ++(void) json_nothrow { ++it; return *this; }
229 inline const_iterator& operator --(void) json_nothrow { --it; return *this; }
230 inline const_iterator& operator +=(long i) json_nothrow { it += i; return *this; }
231 inline const_iterator& operator -=(long i) json_nothrow { it -= i; return *this; }
232 inline const_iterator operator ++(int) json_nothrow {
233 const_iterator result(*this);
234 ++it;
235 return result;
236 }
237 inline const_iterator operator --(int) json_nothrow {
238 const_iterator result(*this);
239 --it;
240 return result;
241 }
242 inline const_iterator operator +(long i) const json_nothrow {
243 const_iterator result(*this);
244 result.it += i;
245 return result;
246 }
247 inline const_iterator operator -(long i) const json_nothrow {
248 const_iterator result(*this);
249 result.it -= i;
250 return result;
251 }
252 inline const JSONNode& operator [](size_t pos) const json_nothrow { return const_cast<const JSONNode&>(*it[pos]); };
253 inline const JSONNode& operator *(void) const json_nothrow { return const_cast<const JSONNode&>(*(*it)); }
254 inline const JSONNode* operator ->(void) const json_nothrow { return const_cast<const JSONNode*>(*it); }
255 inline bool operator == (const const_iterator & other) const json_nothrow { return it == other.it; }
256 inline bool operator != (const const_iterator & other) const json_nothrow { return it != other.it; }
257 inline bool operator > (const const_iterator & other) const json_nothrow { return it > other.it; }
258 inline bool operator >= (const const_iterator & other) const json_nothrow { return it >= other.it; }
259 inline bool operator < (const const_iterator & other) const json_nothrow { return it < other.it; }
260 inline bool operator <= (const const_iterator & other) const json_nothrow { return it <= other.it; }
261
262 inline bool operator == (const iterator & other) const json_nothrow { return it == other.it; }
263 inline bool operator != (const iterator & other) const json_nothrow { return it != other.it; }
264 inline bool operator > (const iterator & other) const json_nothrow { return it > other.it; }
265 inline bool operator >= (const iterator & other) const json_nothrow { return it >= other.it; }
266 inline bool operator < (const iterator & other) const json_nothrow { return it < other.it; }
267 inline bool operator <= (const iterator & other) const json_nothrow { return it <= other.it; }
268
269 inline const_iterator & operator =(const const_iterator & orig) json_nothrow { it = orig.it; return *this; }
const_iteratorconst_iterator270 const_iterator (const const_iterator & orig) json_nothrow : it(orig.it) {}
271 private:
272 JSONNode ** it;
const_iteratorconst_iterator273 const_iterator(JSONNode ** starter) : it(starter) {}
274 friend class JSONNode;
275 friend struct iterator;
276 };
277 const_iterator begin(void) const json_nothrow;
278 const_iterator end(void) const json_nothrow;
279
280 struct iterator {
281 inline iterator& operator ++(void) json_nothrow { ++it; return *this; }
282 inline iterator& operator --(void) json_nothrow { --it; return *this; }
283 inline iterator& operator +=(long i) json_nothrow { it += i; return *this; }
284 inline iterator& operator -=(long i) json_nothrow { it -= i; return *this; }
285 inline iterator operator ++(int) json_nothrow {
286 iterator result(*this);
287 ++it;
288 return result;
289 }
290 inline iterator operator --(int) json_nothrow {
291 iterator result(*this);
292 --it;
293 return result;
294 }
295 inline iterator operator +(long i) const json_nothrow {
296 iterator result(*this);
297 result.it += i;
298 return result;
299 }
300 inline iterator operator -(long i) const json_nothrow {
301 iterator result(*this);
302 result.it -= i;
303 return result;
304 }
305 inline JSONNode& operator [](size_t pos) const json_nothrow { return *it[pos]; };
306 inline JSONNode& operator *(void) const json_nothrow { return *(*it); }
307 inline JSONNode* operator ->(void) const json_nothrow { return *it; }
308 inline bool operator == (const iterator & other) const json_nothrow { return it == other.it; }
309 inline bool operator != (const iterator & other) const json_nothrow { return it != other.it; }
310 inline bool operator > (const iterator & other) const json_nothrow { return it > other.it; }
311 inline bool operator >= (const iterator & other) const json_nothrow { return it >= other.it; }
312 inline bool operator < (const iterator & other) const json_nothrow { return it < other.it; }
313 inline bool operator <= (const iterator & other) const json_nothrow { return it <= other.it; }
314 inline iterator & operator = (const iterator & orig) json_nothrow { it = orig.it; return *this; }
315
316 inline bool operator == (const const_iterator & other) const json_nothrow { return it == other.it; }
317 inline bool operator != (const const_iterator & other) const json_nothrow { return it != other.it; }
318 inline bool operator > (const const_iterator & other) const json_nothrow { return it > other.it; }
319 inline bool operator >= (const const_iterator & other) const json_nothrow { return it >= other.it; }
320 inline bool operator < (const const_iterator & other) const json_nothrow { return it < other.it; }
321 inline bool operator <= (const const_iterator & other) const json_nothrow { return it <= other.it; }
322 inline iterator & operator = (const const_iterator & orig) json_nothrow { it = orig.it; return *this; }
323
iteratoriterator324 iterator (const iterator & orig) json_nothrow : it(orig.it) {}
const_iteratoriterator325 inline operator const_iterator() const json_nothrow { return const_iterator(it); }
326 private:
327 JSONNode ** it;
iteratoriterator328 iterator(JSONNode ** starter) json_nothrow : it(starter) {}
329 friend class JSONNode;
330 friend struct const_iterator;
331 };
332 typedef iterator json_iterator;
333
334 struct reverse_iterator;
335 struct reverse_const_iterator {
336 inline reverse_const_iterator& operator ++(void) json_nothrow{ --it; return *this; }
337 inline reverse_const_iterator& operator --(void) json_nothrow{ ++it; return *this; }
338 inline reverse_const_iterator& operator +=(long i) json_nothrow{ it -= i; return *this; }
339 inline reverse_const_iterator& operator -=(long i) json_nothrow{ it += i; return *this; }
340 inline reverse_const_iterator operator ++(int) json_nothrow{
341 reverse_const_iterator result(*this);
342 --it;
343 return result;
344 }
345 inline reverse_const_iterator operator --(int) json_nothrow{
346 reverse_const_iterator result(*this);
347 ++it;
348 return result;
349 }
350 inline reverse_const_iterator operator +(long i) const json_nothrow {
351 reverse_const_iterator result(*this);
352 result.it -= i;
353 return result;
354 }
355 inline reverse_const_iterator operator -(long i) const json_nothrow {
356 reverse_const_iterator result(*this);
357 result.it += i;
358 return result;
359 }
360 inline const JSONNode& operator [](size_t pos) const json_nothrow { return const_cast<const JSONNode&>(*it[pos]); };
361 inline const JSONNode& operator *(void) const json_nothrow { return const_cast<const JSONNode&>(*(*it)); }
362 inline const JSONNode* operator ->(void) const json_nothrow { return const_cast<const JSONNode*>(*it); }
363 inline bool operator == (const reverse_const_iterator & other) const json_nothrow { return it == other.it; }
364 inline bool operator != (const reverse_const_iterator & other) const json_nothrow { return it != other.it; }
365 inline bool operator < (const reverse_const_iterator & other) const json_nothrow { return it > other.it; }
366 inline bool operator <= (const reverse_const_iterator & other) const json_nothrow { return it >= other.it; }
367 inline bool operator > (const reverse_const_iterator & other) const json_nothrow { return it < other.it; }
368 inline bool operator >= (const reverse_const_iterator & other) const json_nothrow { return it <= other.it; }
369
370 inline bool operator == (const reverse_iterator & other) const json_nothrow { return it == other.it; }
371 inline bool operator != (const reverse_iterator & other) const json_nothrow { return it != other.it; }
372 inline bool operator < (const reverse_iterator & other) const json_nothrow { return it > other.it; }
373 inline bool operator <= (const reverse_iterator & other) const json_nothrow { return it >= other.it; }
374 inline bool operator > (const reverse_iterator & other) const json_nothrow { return it < other.it; }
375 inline bool operator >= (const reverse_iterator & other) const json_nothrow { return it <= other.it; }
376
377 inline reverse_const_iterator & operator = (const reverse_const_iterator & orig) json_nothrow { it = orig.it; return *this; }
reverse_const_iteratorreverse_const_iterator378 reverse_const_iterator (const reverse_const_iterator & orig) json_nothrow : it(orig.it) {}
379 private:
380 JSONNode ** it;
reverse_const_iteratorreverse_const_iterator381 reverse_const_iterator(JSONNode ** starter) json_nothrow : it(starter) {}
382 friend class JSONNode;
383 friend struct reverse_iterator;
384 };
385 reverse_const_iterator rbegin(void) const json_nothrow;
386 reverse_const_iterator rend(void) const json_nothrow;
387
388 struct reverse_iterator {
389 inline reverse_iterator& operator ++(void) json_nothrow { --it; return *this; }
390 inline reverse_iterator& operator --(void) json_nothrow { ++it; return *this; }
391 inline reverse_iterator& operator +=(long i) json_nothrow { it -= i; return *this; }
392 inline reverse_iterator& operator -=(long i) json_nothrow { it += i; return *this; }
393 inline reverse_iterator operator ++(int) json_nothrow {
394 reverse_iterator result(*this);
395 --it;
396 return result;
397 }
398 inline reverse_iterator operator --(int) json_nothrow {
399 reverse_iterator result(*this);
400 ++it;
401 return result;
402 }
403 inline reverse_iterator operator +(long i) const json_nothrow {
404 reverse_iterator result(*this);
405 result.it -= i;
406 return result;
407 }
408 inline reverse_iterator operator -(long i) const json_nothrow {
409 reverse_iterator result(*this);
410 result.it += i;
411 return result;
412 }
413 inline JSONNode& operator [](size_t pos) const json_nothrow { return *it[pos]; };
414 inline JSONNode& operator *(void) const json_nothrow { return *(*it); }
415 inline JSONNode* operator ->(void) const json_nothrow { return *it; }
416 inline bool operator == (const reverse_iterator & other) const json_nothrow { return it == other.it; }
417 inline bool operator != (const reverse_iterator & other) const json_nothrow { return it != other.it; }
418 inline bool operator < (const reverse_iterator & other) const json_nothrow { return it > other.it; }
419 inline bool operator <= (const reverse_iterator & other) const json_nothrow { return it >= other.it; }
420 inline bool operator > (const reverse_iterator & other) const json_nothrow { return it < other.it; }
421 inline bool operator >= (const reverse_iterator & other) const json_nothrow { return it <= other.it; }
422
423 inline bool operator == (const reverse_const_iterator & other) const json_nothrow { return it == other.it; }
424 inline bool operator != (const reverse_const_iterator & other) const json_nothrow { return it != other.it; }
425 inline bool operator < (const reverse_const_iterator & other) const json_nothrow { return it > other.it; }
426 inline bool operator <= (const reverse_const_iterator & other) const json_nothrow { return it >= other.it; }
427 inline bool operator > (const reverse_const_iterator & other) const json_nothrow { return it < other.it; }
428 inline bool operator >= (const reverse_const_iterator & other) const json_nothrow { return it <= other.it; }
429
430 inline reverse_iterator & operator = (const reverse_iterator & orig) json_nothrow { it = orig.it; return *this; }
reverse_iteratorreverse_iterator431 reverse_iterator (const reverse_iterator & orig) json_nothrow : it(orig.it) {}
reverse_const_iteratorreverse_iterator432 inline operator reverse_const_iterator() const json_nothrow { return reverse_const_iterator(it); }
433 private:
434 JSONNode ** it;
reverse_iteratorreverse_iterator435 reverse_iterator(JSONNode ** starter) json_nothrow : it(starter) {}
436 friend class JSONNode;
437 friend struct reverse_const_iterator;
438 };
439 reverse_iterator rbegin(void) json_nothrow;
440 reverse_iterator rend(void) json_nothrow;
441
442 const_iterator find(const json_string & name_t) const json_nothrow;
443 #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS
444 const_iterator find_nocase(const json_string & name_t) const json_nothrow;
445 #endif
446
447 reverse_iterator erase(reverse_iterator pos) json_nothrow;
448 reverse_iterator erase(reverse_iterator start, const reverse_iterator & end) json_nothrow;
449
450 iterator insert(iterator pos, const JSONNode & x) json_nothrow;
451 reverse_iterator insert(reverse_iterator pos, const JSONNode & x) json_nothrow;
452 iterator insert(iterator pos, const reverse_iterator & _start, const reverse_iterator & _end) json_nothrow;
453 reverse_iterator insert(reverse_iterator pos, const iterator & _start, const iterator & _end) json_nothrow;
454 reverse_iterator insert(reverse_iterator pos, const reverse_iterator & _start, const reverse_iterator & _end) json_nothrow;
455
456 json_iterator insert(json_iterator pos, const const_iterator & _start, const const_iterator & _end) json_nothrow;
457 reverse_iterator insert(reverse_iterator pos, const const_iterator & _start, const const_iterator & _end) json_nothrow;
458 json_iterator insert(json_iterator pos, const reverse_const_iterator & _start, const reverse_const_iterator & _end) json_nothrow;
459 reverse_iterator insert(reverse_iterator pos, const reverse_const_iterator & _start, const reverse_const_iterator & _end) json_nothrow;
460 #else
461 typedef JSONNode** json_iterator;
462 #define json_iterator_ptr(iter) iter
463 #define ptr_to_json_iterator(iter) iter
464 json_iterator insert(json_iterator pos, JSONNode * x) json_nothrow;
465 #endif
466
467 json_iterator begin(void) json_nothrow;
468 json_iterator end(void) json_nothrow;
469
470 json_iterator find(const json_string & name_t) json_nothrow;
471 #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS
472 json_iterator find_nocase(const json_string & name_t) json_nothrow;
473 #endif
474 json_iterator erase(json_iterator pos) json_nothrow;
475 json_iterator erase(json_iterator start, const json_iterator & end) json_nothrow;
476 json_iterator insert(json_iterator pos, const json_iterator & _start, const json_iterator & _end) json_nothrow;
477 #endif
478
479
480 #ifdef JSON_MUTEX_CALLBACKS
481 static void register_mutex_callbacks(json_mutex_callback_t lock, json_mutex_callback_t unlock, void * manager_lock) json_nothrow json_cold;
482 #ifdef JSON_MUTEX_MANAGE
483 static void register_mutex_destructor(json_mutex_callback_t destroy) json_nothrow json_cold;
484 #endif
485 static void set_global_mutex(void * mutex) json_nothrow json_cold;
486 void set_mutex(void * mutex) json_nothrow json_cold;
487 void lock(int thread) json_nothrow json_cold;
488 void unlock(int thread) json_nothrow json_cold;
489 struct auto_lock {
490 public:
auto_lockauto_lock491 auto_lock(JSONNode & node, int thread) json_nothrow: mynode(&node), mythread(thread){
492 mynode -> lock(mythread);
493 }
auto_lockauto_lock494 auto_lock(JSONNode * node, int thread) json_nothrow: mynode(node), mythread(thread){
495 mynode -> lock(mythread);
496 }
~auto_lockauto_lock497 ~auto_lock(void) json_nothrow{
498 mynode -> unlock(mythread);
499 }
500 private:
501 auto_lock & operator = (const auto_lock &);
502 auto_lock(const auto_lock &);
503 JSONNode * mynode;
504 int mythread;
505 };
506 static void * getThisLock(JSONNode * pthis) json_nothrow json_cold;
507 #endif
508
509 #ifdef JSON_WRITE_PRIORITY
510 #ifdef JSON_LESS_MEMORY
511 #define DEFAULT_APPROX_SIZE 8
512 #define DEFAULT_APPROX_SIZE_FORMATTED 16
513 #else
514 #define DEFAULT_APPROX_SIZE 1024
515 #define DEFAULT_APPROX_SIZE_FORMATTED 2048
516 #endif
517 json_string write(size_t approxsize = DEFAULT_APPROX_SIZE) const json_nothrow json_write_priority;
518 json_string write_formatted(size_t approxsize = DEFAULT_APPROX_SIZE_FORMATTED) const json_nothrow json_write_priority;
519 #endif
520
521 #ifdef JSON_DEBUG
522 #ifndef JSON_LIBRARY
523 JSONNode dump(void) const json_nothrow;
524 #endif
525 #endif
526 static void deleteJSONNode(JSONNode * ptr) json_nothrow json_hot;
527 static JSONNode * newJSONNode_Shallow(const JSONNode & orig) json_hot;
528
529 #define DECLARE_CAST_OP(type) operator type()
530 //DECLARE_FOR_ALL_CAST_TYPES_CONST(DECLARE_CAST_OP)
531 JSON_PRIVATE
532 static JSONNode * newJSONNode(const JSONNode & orig JSON_MUTEX_COPY_DECL2) json_hot;
533 static JSONNode * newJSONNode(internalJSONNode * internal_t) json_hot;
534 #ifdef JSON_READ_PRIORITY
535 //used by JSONWorker
JSONNode(const json_string & unparsed)536 JSONNode(const json_string & unparsed) json_nothrow : internal(internalJSONNode::newInternal(unparsed)){ //root, specialized because it can only be array or node
537 LIBJSON_CTOR;
538 }
539 #endif
JSONNode(internalJSONNode * internal_t)540 JSONNode(internalJSONNode * internal_t) json_nothrow : internal(internal_t){ //do not increment anything, this is only used in one case and it's already taken care of
541 LIBJSON_CTOR;
542 }
543 JSONNode(bool, JSONNode & orig) json_nothrow json_hot;
544
545 void decRef(void) json_nothrow json_hot; //decrements internal's counter, deletes it if needed
546 #ifdef JSON_REF_COUNT
547 void makeUniqueInternal(void) json_nothrow; //makes internal it's own
548 void merge(JSONNode * other) json_nothrow json_cold;
549 #endif
550
551 #ifdef JSON_DEBUG
552 #ifndef JSON_LIBRARY
553 JSONNode dump(size_t & totalmemory) json_nothrow;
554 #endif
555 #endif
556
557 #ifdef JSON_ITERATORS
558 #ifndef JSON_LIBRARY
559 json_iterator insertFRR(json_iterator pos, JSONNode ** const _start, JSONNode ** const _end) json_nothrow;
560 reverse_iterator insertRRR(reverse_iterator pos, JSONNode ** const _start, JSONNode ** const _end) json_nothrow;
561 reverse_iterator insertRFF(reverse_iterator pos, JSONNode ** const _start, JSONNode ** const _end) json_nothrow;
562 #endif
563 json_iterator insertFFF(json_iterator pos, JSONNode ** const _start, JSONNode ** const _end) json_nothrow;
564 #endif
565
clear_name(void)566 inline void clear_name(void) json_nothrow {
567 JSON_CHECK_INTERNAL();
568 makeUniqueInternal();
569 internal -> clearname();
570 }
571
572 mutable internalJSONNode * internal;
573 friend class JSONWorker;
574 friend class internalJSONNode;
575 };
576
577
578
579 /*
580 Implementations are here to keep the class declaration cleaner. They can't be placed in a different
581 file because they are inlined.
582 */
583
584 #define CAST_OP(type)\
585 inline JSONNode::operator type() const json_nothrow {\
586 return static_cast<type>(*internal);\
587 }
588 //IMPLEMENT_FOR_ALL_TYPES(CAST_OP)
589
590
JSONNode(char mytype)591 inline JSONNode::JSONNode(char mytype) json_nothrow : internal(internalJSONNode::newInternal(mytype)){
592 JSON_ASSERT((mytype == JSON_NULL) ||
593 (mytype == JSON_STRING) ||
594 (mytype == JSON_NUMBER) ||
595 (mytype == JSON_BOOL) ||
596 (mytype == JSON_ARRAY) ||
597 (mytype == JSON_NODE), JSON_TEXT("Not a proper JSON type"));
598 LIBJSON_CTOR;
599 }
600
JSONNode(const JSONNode & orig)601 inline JSONNode::JSONNode(const JSONNode & orig) json_nothrow : internal(orig.internal -> incRef()){
602 LIBJSON_COPY_CTOR;
603 }
604
605 //this allows a temp node to simply transfer its contents, even with ref counting off
JSONNode(bool,JSONNode & orig)606 inline JSONNode::JSONNode(bool, JSONNode & orig) json_nothrow : internal(orig.internal){
607 orig.internal = 0;
608 LIBJSON_CTOR;
609 }
610
~JSONNode(void)611 inline JSONNode::~JSONNode(void) json_nothrow{
612 if (internal != 0) decRef();
613 LIBJSON_DTOR;
614 }
615
size(void)616 inline json_index_t JSONNode::size(void) const json_nothrow {
617 JSON_CHECK_INTERNAL();
618 return internal -> size();
619 }
620
empty(void)621 inline bool JSONNode::empty(void) const json_nothrow {
622 JSON_CHECK_INTERNAL();
623 return internal -> empty();
624 }
625
clear(void)626 inline void JSONNode::clear(void) json_nothrow {
627 JSON_CHECK_INTERNAL();
628 if (!empty()){
629 makeUniqueInternal();
630 internal -> CHILDREN -> clear();
631 }
632 }
633
type(void)634 inline unsigned char JSONNode::type(void) const json_nothrow {
635 JSON_CHECK_INTERNAL();
636 return internal -> type();
637 }
638
name(void)639 inline json_string JSONNode::name(void) const json_nothrow {
640 JSON_CHECK_INTERNAL();
641 return internal -> name();
642 }
643
set_name(const json_string & newname)644 inline void JSONNode::set_name(const json_string & newname) json_nothrow{
645 JSON_CHECK_INTERNAL();
646 makeUniqueInternal();
647 internal -> setname(newname);
648 }
649
650 #ifdef JSON_COMMENTS
set_comment(const json_string & newname)651 inline void JSONNode::set_comment(const json_string & newname) json_nothrow{
652 JSON_CHECK_INTERNAL();
653 makeUniqueInternal();
654 internal -> setcomment(newname);
655 }
656
get_comment(void)657 inline json_string JSONNode::get_comment(void) const json_nothrow {
658 JSON_CHECK_INTERNAL();
659 return internal -> getcomment();
660 }
661 #endif
662
663 //#ifdef JSON_DEPRECATED_FUNCTIONS
as_string(void)664 inline json_string JSONNode::as_string(void) const json_nothrow {
665 JSON_CHECK_INTERNAL();
666 return static_cast<json_string>(*internal);
667 }
668
as_int(void)669 inline json_int_t JSONNode::as_int(void) const json_nothrow {
670 JSON_CHECK_INTERNAL();
671 return static_cast<json_int_t>(*internal);
672 }
673
as_float(void)674 inline json_number JSONNode::as_float(void) const json_nothrow {
675 JSON_CHECK_INTERNAL();
676 return static_cast<json_number>(*internal);
677 }
678
as_bool(void)679 inline bool JSONNode::as_bool(void) const json_nothrow {
680 JSON_CHECK_INTERNAL();
681 return static_cast<bool>(*internal);
682 }
683 //#endif
684
685 #ifdef JSON_BINARY
set_binary(const unsigned char * bin,size_t bytes)686 inline void JSONNode::set_binary(const unsigned char * bin, size_t bytes) json_nothrow{
687 JSON_CHECK_INTERNAL();
688 *this = JSONBase64::json_encode64(bin, bytes);
689 }
690
as_binary(void)691 inline std::string JSONNode::as_binary(void) const json_nothrow {
692 JSON_ASSERT_SAFE(type() == JSON_STRING, JSON_TEXT("using as_binary for a non-string type"), return json_global(EMPTY_STD_STRING););
693 JSON_CHECK_INTERNAL();
694 return JSONBase64::json_decode64(as_string());
695 }
696 #endif
697
698 inline JSONNode & JSONNode::operator[](const json_string & name_t) json_nothrow {
699 JSON_CHECK_INTERNAL();
700 makeUniqueInternal();
701 return *(*(internal -> at(name_t)));
702 }
703
704 inline const JSONNode & JSONNode::operator[](const json_string & name_t) const json_nothrow {
705 JSON_CHECK_INTERNAL();
706 return *(*(internal -> at(name_t)));
707 }
708
709 #ifdef JSON_LIBRARY
push_back(JSONNode * child)710 inline void JSONNode::push_back(JSONNode * child) json_nothrow{
711 #else
712 inline void JSONNode::push_back(const JSONNode & child) json_nothrow{
713 #endif
714 JSON_CHECK_INTERNAL();
715 makeUniqueInternal();
716 internal -> push_back(child);
717 }
718
719 inline void JSONNode::reserve(json_index_t siz) json_nothrow{
720 makeUniqueInternal();
721 internal -> reserve(siz);
722 }
723
724 inline JSONNode & JSONNode::operator = (const JSONNode & orig) json_nothrow {
725 JSON_CHECK_INTERNAL();
726 #ifdef JSON_REF_COUNT
727 if (internal == orig.internal) return *this; //don't want it accidentally deleting itself
728 #endif
729 decRef(); //dereference my current one
730 internal = orig.internal -> incRef(); //increase reference of original
731 return *this;
732 }
733
734 #ifndef JSON_LIBRARY
735 inline JSONNode & JSONNode::operator = (const json_char * val) json_nothrow {
736 JSON_CHECK_INTERNAL();
737 *this = json_string(val);
738 return *this;
739 }
740 #endif
741
742 #define NODE_SET_TYPED(type)\
743 inline JSONNode & JSONNode::operator = (type val) json_nothrow {\
744 LIBJSON_ASSIGNMENT;\
745 JSON_CHECK_INTERNAL();\
746 makeUniqueInternal();\
747 internal -> Set(val);\
748 return *this;\
749 }
750 IMPLEMENT_FOR_ALL_TYPES(NODE_SET_TYPED)
751
752
753 /*
754 This section is the equality operators
755 */
756
757 #define NODE_CHECK_EQUALITY(type)\
758 inline bool JSONNode::operator == (type val) const json_nothrow {\
759 JSON_CHECK_INTERNAL();\
760 return internal -> IsEqualToNum<type>(val);\
761 }
762
763 IMPLEMENT_FOR_ALL_NUMBERS(NODE_CHECK_EQUALITY)
764
765 inline bool JSONNode::operator == (const json_string & val) const json_nothrow {
766 JSON_CHECK_INTERNAL();
767 return internal -> IsEqualTo(val);
768 }
769
770 #ifndef JSON_LIBRARY
771 inline bool JSONNode::operator == (const json_char * val) const json_nothrow {
772 JSON_CHECK_INTERNAL();
773 return *this == json_string(val);
774 }
775 #endif
776
777 inline bool JSONNode::operator == (bool val) const json_nothrow {
778 JSON_CHECK_INTERNAL();
779 return internal -> IsEqualTo(val);
780 }
781 inline bool JSONNode::operator == (const JSONNode & val) const json_nothrow {
782 JSON_CHECK_INTERNAL();
783 return internal -> IsEqualTo(val.internal);
784 }
785
786
787 /*
788 This section is the inequality operators
789 */
790
791
792 #define NODE_CHECK_INEQUALITY(type)\
793 inline bool JSONNode::operator != (type val) const json_nothrow {\
794 JSON_CHECK_INTERNAL();\
795 return !(*this == val);\
796 }
797
798 IMPLEMENT_FOR_ALL_TYPES(NODE_CHECK_INEQUALITY)
799 NODE_CHECK_INEQUALITY(const JSONNode &)
800 #ifndef JSON_LIBRARY
801 NODE_CHECK_INEQUALITY(const json_char * )
802 #endif
803
804 inline void JSONNode::nullify(void) json_nothrow {
805 JSON_CHECK_INTERNAL();
806 makeUniqueInternal();
807 internal -> Nullify();
808 }
809
810 inline void JSONNode::swap(JSONNode & other) json_nothrow {
811 JSON_CHECK_INTERNAL();
812 internalJSONNode * temp = other.internal;
813 other.internal = internal;
814 internal = temp;
815 JSON_CHECK_INTERNAL();
816 }
817
818 inline void JSONNode::decRef(void) json_nothrow { //decrements internal's counter, deletes it if needed
819 JSON_CHECK_INTERNAL();
820 #ifdef JSON_REF_COUNT
821 internal -> decRef();
822 if (internal -> hasNoReferences()){
823 internalJSONNode::deleteInternal(internal);
824 }
825 #else
826 internalJSONNode::deleteInternal(internal);
827 #endif
828 }
829
830 #ifdef JSON_REF_COUNT
831 inline void JSONNode::makeUniqueInternal() json_nothrow { //makes internal it's own
832 JSON_CHECK_INTERNAL();
833 internal = internal -> makeUnique(); //might return itself or a new one that's exactly the same
834 }
835 #endif
836
837 #ifdef JSON_ITERATORS
838 inline JSONNode::json_iterator JSONNode::begin(void) json_nothrow {
839 JSON_CHECK_INTERNAL();
840 JSON_ASSERT(type() == JSON_NODE || type() == JSON_ARRAY, json_global(ERROR_NON_ITERATABLE) + JSON_TEXT("begin"));
841 makeUniqueInternal();
842 return json_iterator(internal -> begin());
843 }
844
845 inline JSONNode::json_iterator JSONNode::end(void) json_nothrow {
846 JSON_CHECK_INTERNAL();
847 JSON_ASSERT(type() == JSON_NODE || type() == JSON_ARRAY, json_global(ERROR_NON_ITERATABLE) + JSON_TEXT("end"));
848 makeUniqueInternal();
849 return json_iterator(internal -> end());
850 }
851
852 #ifndef JSON_LIBRARY
853 inline JSONNode::const_iterator JSONNode::begin(void) const json_nothrow {
854 JSON_CHECK_INTERNAL();
855 JSON_ASSERT(type() == JSON_NODE || type() == JSON_ARRAY, json_global(ERROR_NON_ITERATABLE) + JSON_TEXT("begin"));
856 return JSONNode::const_iterator(internal -> begin());
857 }
858
859 inline JSONNode::const_iterator JSONNode::end(void) const json_nothrow {
860 JSON_CHECK_INTERNAL();
861 JSON_ASSERT(type() == JSON_NODE || type() == JSON_ARRAY, json_global(ERROR_NON_ITERATABLE) + JSON_TEXT("end"));
862 return JSONNode::const_iterator(internal -> end());
863 }
864
865 inline JSONNode::reverse_iterator JSONNode::rbegin(void) json_nothrow {
866 JSON_CHECK_INTERNAL();
867 JSON_ASSERT(type() == JSON_NODE || type() == JSON_ARRAY, json_global(ERROR_NON_ITERATABLE) + JSON_TEXT("rbegin"));
868 makeUniqueInternal();
869 return JSONNode::reverse_iterator(internal -> end() - 1);
870 }
871
872 inline JSONNode::reverse_iterator JSONNode::rend(void) json_nothrow {
873 JSON_CHECK_INTERNAL();
874 JSON_ASSERT(type() == JSON_NODE || type() == JSON_ARRAY, json_global(ERROR_NON_ITERATABLE) + JSON_TEXT("rend"));
875 makeUniqueInternal();
876 return JSONNode::reverse_iterator(internal -> begin() - 1);
877 }
878
879 inline JSONNode::reverse_const_iterator JSONNode::rbegin(void) const json_nothrow {
880 JSON_CHECK_INTERNAL();
881 JSON_ASSERT(type() == JSON_NODE || type() == JSON_ARRAY, json_global(ERROR_NON_ITERATABLE) + JSON_TEXT("rbegin"));
882 return JSONNode::reverse_const_iterator(internal -> end() - 1);
883 }
884
885 inline JSONNode::reverse_const_iterator JSONNode::rend(void) const json_nothrow {
886 JSON_CHECK_INTERNAL();
887 JSON_ASSERT(type() == JSON_NODE || type() == JSON_ARRAY, json_global(ERROR_NON_ITERATABLE) + JSON_TEXT("rend"));
888 return JSONNode::reverse_const_iterator(internal -> begin() - 1);
889 }
890
891 inline JSONNode::iterator JSONNode::insert(json_iterator pos, const const_iterator & _start, const const_iterator & _end) json_nothrow {
892 return insertFFF(pos, _start.it, _end.it);
893 }
894
895 inline JSONNode::reverse_iterator JSONNode::insert(reverse_iterator pos, const const_iterator & _start, const const_iterator & _end) json_nothrow {
896 return insertRFF(pos, _start.it, _end.it);
897 }
898
899 inline JSONNode::reverse_iterator JSONNode::insert(reverse_iterator pos, const iterator & _start, const iterator & _end) json_nothrow {
900 return insertRFF(pos, _start.it, _end.it);
901 }
902
903 inline JSONNode::reverse_iterator JSONNode::insert(reverse_iterator pos, const reverse_const_iterator & _start, const reverse_const_iterator & _end) json_nothrow {
904 return insertRRR(pos, _start.it, _end.it);
905 }
906
907 inline JSONNode::reverse_iterator JSONNode::insert(reverse_iterator pos, const reverse_iterator & _start, const reverse_iterator & _end) json_nothrow {
908 return insertRRR(pos, _start.it, _end.it);
909 }
910
911 inline JSONNode::iterator JSONNode::insert(json_iterator pos, const reverse_const_iterator & _start, const reverse_const_iterator & _end) json_nothrow {
912 return insertFRR(pos, _start.it, _end.it);
913 }
914
915 inline JSONNode::iterator JSONNode::insert(iterator pos, const reverse_iterator & _start, const reverse_iterator & _end) json_nothrow {
916 return insertFRR(pos, _start.it, _end.it);
917 }
918 #endif
919
920 inline JSONNode::json_iterator JSONNode::insert(json_iterator pos, const json_iterator & _start, const json_iterator & _end) json_nothrow {
921 return insertFFF(pos, json_iterator_ptr(_start), json_iterator_ptr(_end));
922 }
923 #endif
924
925 #ifdef JSON_WRITE_PRIORITY
926 inline json_string JSONNode::write(size_t approxsize) const json_nothrow {
927 JSON_CHECK_INTERNAL();
928 JSON_ASSERT_SAFE(type() == JSON_NODE || type() == JSON_ARRAY, JSON_TEXT("Writing a non-writable node"), return json_global(EMPTY_JSON_STRING););
929 json_string result;
930 result.reserve(approxsize);
931 internal -> Write(0xFFFFFFFF, true, result);
932 return result;
933 }
934
935 inline json_string JSONNode::write_formatted(size_t approxsize) const json_nothrow {
936 JSON_CHECK_INTERNAL();
937 JSON_ASSERT_SAFE(type() == JSON_NODE || type() == JSON_ARRAY, JSON_TEXT("Writing a non-writable node"), return json_global(EMPTY_JSON_STRING););
938 json_string result;
939 result.reserve(approxsize);
940 internal -> Write(0, true, result);
941 return result;
942 }
943
944 #endif
945
946 #if !defined(JSON_PREPARSE) && defined(JSON_READ_PRIORITY)
947 inline void JSONNode::preparse(void) json_nothrow {
948 JSON_CHECK_INTERNAL();
949 internal -> preparse();
950 }
951 #endif
952
953 #ifdef JSON_DEBUG
954 #ifndef JSON_LIBRARY
955 inline JSONNode JSONNode::dump(void) const json_nothrow {
956 JSON_CHECK_INTERNAL();
957 JSONNode dumpage(JSON_NODE);
958 dumpage.push_back(JSON_NEW(JSONNode(JSON_TEXT("this"), (long)this)));
959 size_t total = 0;
960 JSONNode node(internal -> Dump(total));
961 dumpage.push_back(JSON_NEW(JSONNode(JSON_TEXT("total bytes used"), total)));
962 dumpage.push_back(JSON_NEW(JSONNode(JSON_TEXT("bytes used"), sizeof(JSONNode))));
963 dumpage.push_back(JSON_NEW(node));
964 return dumpage;
965 }
966
967 inline JSONNode JSONNode::dump(size_t & totalmemory) json_nothrow {
968 JSON_CHECK_INTERNAL();
969 JSONNode dumpage(JSON_NODE);
970 dumpage.push_back(JSON_NEW(JSONNode(JSON_TEXT("this"), (long)this)));
971 dumpage.push_back(JSON_NEW(JSONNode(JSON_TEXT("bytes used"), sizeof(JSONNode))));
972 dumpage.push_back(JSON_NEW(internal -> Dump(totalmemory)));
973 return dumpage;
974 }
975 #endif
976 #endif
977
978 #ifdef JSON_LESS_MEMORY
979 #ifdef __GNUC__
980 #pragma pack(pop)
981 #elif _MSC_VER
982 #pragma pack(pop, JSONNode_pack)
983 #endif
984 #endif
985 #endif
986