Mercurial > hg > tweakathon2ios
comparison json/json.h @ 0:a223551fdc1f
First commit - copy from tweakathlon.
author | Robert Tubb <rt300@eecs.qmul.ac.uk> |
---|---|
date | Fri, 10 Oct 2014 11:46:42 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:a223551fdc1f |
---|---|
1 /// Json-cpp amalgated header (http://jsoncpp.sourceforge.net/). | |
2 /// It is intented to be used with #include <json/json.h> | |
3 | |
4 // ////////////////////////////////////////////////////////////////////// | |
5 // Beginning of content of file: LICENSE | |
6 // ////////////////////////////////////////////////////////////////////// | |
7 | |
8 /* | |
9 The JsonCpp library's source code, including accompanying documentation, | |
10 tests and demonstration applications, are licensed under the following | |
11 conditions... | |
12 | |
13 The author (Baptiste Lepilleur) explicitly disclaims copyright in all | |
14 jurisdictions which recognize such a disclaimer. In such jurisdictions, | |
15 this software is released into the Public Domain. | |
16 | |
17 In jurisdictions which do not recognize Public Domain property (e.g. Germany as of | |
18 2010), this software is Copyright (c) 2007-2010 by Baptiste Lepilleur, and is | |
19 released under the terms of the MIT License (see below). | |
20 | |
21 In jurisdictions which recognize Public Domain property, the user of this | |
22 software may choose to accept it either as 1) Public Domain, 2) under the | |
23 conditions of the MIT License (see below), or 3) under the terms of dual | |
24 Public Domain/MIT License conditions described here, as they choose. | |
25 | |
26 The MIT License is about as close to Public Domain as a license can get, and is | |
27 described in clear, concise terms at: | |
28 | |
29 http://en.wikipedia.org/wiki/MIT_License | |
30 | |
31 The full text of the MIT License follows: | |
32 | |
33 ======================================================================== | |
34 Copyright (c) 2007-2010 Baptiste Lepilleur | |
35 | |
36 Permission is hereby granted, free of charge, to any person | |
37 obtaining a copy of this software and associated documentation | |
38 files (the "Software"), to deal in the Software without | |
39 restriction, including without limitation the rights to use, copy, | |
40 modify, merge, publish, distribute, sublicense, and/or sell copies | |
41 of the Software, and to permit persons to whom the Software is | |
42 furnished to do so, subject to the following conditions: | |
43 | |
44 The above copyright notice and this permission notice shall be | |
45 included in all copies or substantial portions of the Software. | |
46 | |
47 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
48 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
49 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
50 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | |
51 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
52 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
53 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
54 SOFTWARE. | |
55 ======================================================================== | |
56 (END LICENSE TEXT) | |
57 | |
58 The MIT license is compatible with both the GPL and commercial | |
59 software, affording one all of the rights of Public Domain with the | |
60 minor nuisance of being required to keep the above copyright notice | |
61 and license text in the source code. Note also that by accepting the | |
62 Public Domain "license" you can re-license your copy using whatever | |
63 license you like. | |
64 | |
65 */ | |
66 | |
67 // ////////////////////////////////////////////////////////////////////// | |
68 // End of content of file: LICENSE | |
69 // ////////////////////////////////////////////////////////////////////// | |
70 | |
71 | |
72 | |
73 | |
74 | |
75 #ifndef JSON_AMALGATED_H_INCLUDED | |
76 # define JSON_AMALGATED_H_INCLUDED | |
77 /// If defined, indicates that the source file is amalgated | |
78 /// to prevent private header inclusion. | |
79 #define JSON_IS_AMALGAMATION | |
80 | |
81 // ////////////////////////////////////////////////////////////////////// | |
82 // Beginning of content of file: include/json/config.h | |
83 // ////////////////////////////////////////////////////////////////////// | |
84 | |
85 // Copyright 2007-2010 Baptiste Lepilleur | |
86 // Distributed under MIT license, or public domain if desired and | |
87 // recognized in your jurisdiction. | |
88 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE | |
89 | |
90 #ifndef JSON_CONFIG_H_INCLUDED | |
91 # define JSON_CONFIG_H_INCLUDED | |
92 | |
93 /// If defined, indicates that json library is embedded in CppTL library. | |
94 //# define JSON_IN_CPPTL 1 | |
95 | |
96 /// If defined, indicates that json may leverage CppTL library | |
97 //# define JSON_USE_CPPTL 1 | |
98 /// If defined, indicates that cpptl vector based map should be used instead of std::map | |
99 /// as Value container. | |
100 //# define JSON_USE_CPPTL_SMALLMAP 1 | |
101 /// If defined, indicates that Json specific container should be used | |
102 /// (hash table & simple deque container with customizable allocator). | |
103 /// THIS FEATURE IS STILL EXPERIMENTAL! There is know bugs: See #3177332 | |
104 //# define JSON_VALUE_USE_INTERNAL_MAP 1 | |
105 /// Force usage of standard new/malloc based allocator instead of memory pool based allocator. | |
106 /// The memory pools allocator used optimization (initializing Value and ValueInternalLink | |
107 /// as if it was a POD) that may cause some validation tool to report errors. | |
108 /// Only has effects if JSON_VALUE_USE_INTERNAL_MAP is defined. | |
109 //# define JSON_USE_SIMPLE_INTERNAL_ALLOCATOR 1 | |
110 | |
111 /// If defined, indicates that Json use exception to report invalid type manipulation | |
112 /// instead of C assert macro. | |
113 # define JSON_USE_EXCEPTION 1 | |
114 | |
115 /// If defined, indicates that the source file is amalgated | |
116 /// to prevent private header inclusion. | |
117 /// Remarks: it is automatically defined in the generated amalgated header. | |
118 // #define JSON_IS_AMALGAMATION | |
119 | |
120 | |
121 # ifdef JSON_IN_CPPTL | |
122 # include <cpptl/config.h> | |
123 # ifndef JSON_USE_CPPTL | |
124 # define JSON_USE_CPPTL 1 | |
125 # endif | |
126 # endif | |
127 | |
128 # ifdef JSON_IN_CPPTL | |
129 # define JSON_API CPPTL_API | |
130 # elif defined(JSON_DLL_BUILD) | |
131 # define JSON_API __declspec(dllexport) | |
132 # elif defined(JSON_DLL) | |
133 # define JSON_API __declspec(dllimport) | |
134 # else | |
135 # define JSON_API | |
136 # endif | |
137 | |
138 // If JSON_NO_INT64 is defined, then Json only support C++ "int" type for integer | |
139 // Storages, and 64 bits integer support is disabled. | |
140 // #define JSON_NO_INT64 1 | |
141 | |
142 #if defined(_MSC_VER) && _MSC_VER <= 1200 // MSVC 6 | |
143 // Microsoft Visual Studio 6 only support conversion from __int64 to double | |
144 // (no conversion from unsigned __int64). | |
145 #define JSON_USE_INT64_DOUBLE_CONVERSION 1 | |
146 #endif // if defined(_MSC_VER) && _MSC_VER < 1200 // MSVC 6 | |
147 | |
148 #if defined(_MSC_VER) && _MSC_VER >= 1500 // MSVC 2008 | |
149 /// Indicates that the following function is deprecated. | |
150 # define JSONCPP_DEPRECATED(message) __declspec(deprecated(message)) | |
151 #endif | |
152 | |
153 #if !defined(JSONCPP_DEPRECATED) | |
154 # define JSONCPP_DEPRECATED(message) | |
155 #endif // if !defined(JSONCPP_DEPRECATED) | |
156 | |
157 namespace Json { | |
158 typedef int Int; | |
159 typedef unsigned int UInt; | |
160 # if defined(JSON_NO_INT64) | |
161 typedef int LargestInt; | |
162 typedef unsigned int LargestUInt; | |
163 # undef JSON_HAS_INT64 | |
164 # else // if defined(JSON_NO_INT64) | |
165 // For Microsoft Visual use specific types as long long is not supported | |
166 # if defined(_MSC_VER) // Microsoft Visual Studio | |
167 typedef __int64 Int64; | |
168 typedef unsigned __int64 UInt64; | |
169 # else // if defined(_MSC_VER) // Other platforms, use long long | |
170 typedef long long int Int64; | |
171 typedef unsigned long long int UInt64; | |
172 # endif // if defined(_MSC_VER) | |
173 typedef Int64 LargestInt; | |
174 typedef UInt64 LargestUInt; | |
175 # define JSON_HAS_INT64 | |
176 # endif // if defined(JSON_NO_INT64) | |
177 } // end namespace Json | |
178 | |
179 | |
180 #endif // JSON_CONFIG_H_INCLUDED | |
181 | |
182 // ////////////////////////////////////////////////////////////////////// | |
183 // End of content of file: include/json/config.h | |
184 // ////////////////////////////////////////////////////////////////////// | |
185 | |
186 | |
187 | |
188 | |
189 | |
190 | |
191 // ////////////////////////////////////////////////////////////////////// | |
192 // Beginning of content of file: include/json/forwards.h | |
193 // ////////////////////////////////////////////////////////////////////// | |
194 | |
195 // Copyright 2007-2010 Baptiste Lepilleur | |
196 // Distributed under MIT license, or public domain if desired and | |
197 // recognized in your jurisdiction. | |
198 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE | |
199 | |
200 #ifndef JSON_FORWARDS_H_INCLUDED | |
201 # define JSON_FORWARDS_H_INCLUDED | |
202 | |
203 #if !defined(JSON_IS_AMALGAMATION) | |
204 # include "config.h" | |
205 #endif // if !defined(JSON_IS_AMALGAMATION) | |
206 | |
207 namespace Json { | |
208 | |
209 // writer.h | |
210 class FastWriter; | |
211 class StyledWriter; | |
212 | |
213 // reader.h | |
214 class Reader; | |
215 | |
216 // features.h | |
217 class Features; | |
218 | |
219 // value.h | |
220 typedef unsigned int ArrayIndex; | |
221 class StaticString; | |
222 class Path; | |
223 class PathArgument; | |
224 class Value; | |
225 class ValueIteratorBase; | |
226 class ValueIterator; | |
227 class ValueConstIterator; | |
228 #ifdef JSON_VALUE_USE_INTERNAL_MAP | |
229 class ValueMapAllocator; | |
230 class ValueInternalLink; | |
231 class ValueInternalArray; | |
232 class ValueInternalMap; | |
233 #endif // #ifdef JSON_VALUE_USE_INTERNAL_MAP | |
234 | |
235 } // namespace Json | |
236 | |
237 | |
238 #endif // JSON_FORWARDS_H_INCLUDED | |
239 | |
240 // ////////////////////////////////////////////////////////////////////// | |
241 // End of content of file: include/json/forwards.h | |
242 // ////////////////////////////////////////////////////////////////////// | |
243 | |
244 | |
245 | |
246 | |
247 | |
248 | |
249 // ////////////////////////////////////////////////////////////////////// | |
250 // Beginning of content of file: include/json/features.h | |
251 // ////////////////////////////////////////////////////////////////////// | |
252 | |
253 // Copyright 2007-2010 Baptiste Lepilleur | |
254 // Distributed under MIT license, or public domain if desired and | |
255 // recognized in your jurisdiction. | |
256 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE | |
257 | |
258 #ifndef CPPTL_JSON_FEATURES_H_INCLUDED | |
259 # define CPPTL_JSON_FEATURES_H_INCLUDED | |
260 | |
261 #if !defined(JSON_IS_AMALGAMATION) | |
262 # include "forwards.h" | |
263 #endif // if !defined(JSON_IS_AMALGAMATION) | |
264 | |
265 namespace Json { | |
266 | |
267 /** \brief Configuration passed to reader and writer. | |
268 * This configuration object can be used to force the Reader or Writer | |
269 * to behave in a standard conforming way. | |
270 */ | |
271 class JSON_API Features | |
272 { | |
273 public: | |
274 /** \brief A configuration that allows all features and assumes all strings are UTF-8. | |
275 * - C & C++ comments are allowed | |
276 * - Root object can be any JSON value | |
277 * - Assumes Value strings are encoded in UTF-8 | |
278 */ | |
279 static Features all(); | |
280 | |
281 /** \brief A configuration that is strictly compatible with the JSON specification. | |
282 * - Comments are forbidden. | |
283 * - Root object must be either an array or an object value. | |
284 * - Assumes Value strings are encoded in UTF-8 | |
285 */ | |
286 static Features strictMode(); | |
287 | |
288 /** \brief Initialize the configuration like JsonConfig::allFeatures; | |
289 */ | |
290 Features(); | |
291 | |
292 /// \c true if comments are allowed. Default: \c true. | |
293 bool allowComments_; | |
294 | |
295 /// \c true if root must be either an array or an object value. Default: \c false. | |
296 bool strictRoot_; | |
297 }; | |
298 | |
299 } // namespace Json | |
300 | |
301 #endif // CPPTL_JSON_FEATURES_H_INCLUDED | |
302 | |
303 // ////////////////////////////////////////////////////////////////////// | |
304 // End of content of file: include/json/features.h | |
305 // ////////////////////////////////////////////////////////////////////// | |
306 | |
307 | |
308 | |
309 | |
310 | |
311 | |
312 // ////////////////////////////////////////////////////////////////////// | |
313 // Beginning of content of file: include/json/value.h | |
314 // ////////////////////////////////////////////////////////////////////// | |
315 | |
316 // Copyright 2007-2010 Baptiste Lepilleur | |
317 // Distributed under MIT license, or public domain if desired and | |
318 // recognized in your jurisdiction. | |
319 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE | |
320 | |
321 #ifndef CPPTL_JSON_H_INCLUDED | |
322 # define CPPTL_JSON_H_INCLUDED | |
323 | |
324 #if !defined(JSON_IS_AMALGAMATION) | |
325 # include "forwards.h" | |
326 #endif // if !defined(JSON_IS_AMALGAMATION) | |
327 # include <string> | |
328 # include <vector> | |
329 | |
330 # ifndef JSON_USE_CPPTL_SMALLMAP | |
331 # include <map> | |
332 # else | |
333 # include <cpptl/smallmap.h> | |
334 # endif | |
335 # ifdef JSON_USE_CPPTL | |
336 # include <cpptl/forwards.h> | |
337 # endif | |
338 | |
339 /** \brief JSON (JavaScript Object Notation). | |
340 */ | |
341 namespace Json { | |
342 | |
343 /** \brief Type of the value held by a Value object. | |
344 */ | |
345 enum ValueType | |
346 { | |
347 nullValue = 0, ///< 'null' value | |
348 intValue, ///< signed integer value | |
349 uintValue, ///< unsigned integer value | |
350 realValue, ///< double value | |
351 stringValue, ///< UTF-8 string value | |
352 booleanValue, ///< bool value | |
353 arrayValue, ///< array value (ordered list) | |
354 objectValue ///< object value (collection of name/value pairs). | |
355 }; | |
356 | |
357 enum CommentPlacement | |
358 { | |
359 commentBefore = 0, ///< a comment placed on the line before a value | |
360 commentAfterOnSameLine, ///< a comment just after a value on the same line | |
361 commentAfter, ///< a comment on the line after a value (only make sense for root value) | |
362 numberOfCommentPlacement | |
363 }; | |
364 | |
365 //# ifdef JSON_USE_CPPTL | |
366 // typedef CppTL::AnyEnumerator<const char *> EnumMemberNames; | |
367 // typedef CppTL::AnyEnumerator<const Value &> EnumValues; | |
368 //# endif | |
369 | |
370 /** \brief Lightweight wrapper to tag static string. | |
371 * | |
372 * Value constructor and objectValue member assignement takes advantage of the | |
373 * StaticString and avoid the cost of string duplication when storing the | |
374 * string or the member name. | |
375 * | |
376 * Example of usage: | |
377 * \code | |
378 * Json::Value aValue( StaticString("some text") ); | |
379 * Json::Value object; | |
380 * static const StaticString code("code"); | |
381 * object[code] = 1234; | |
382 * \endcode | |
383 */ | |
384 class JSON_API StaticString | |
385 { | |
386 public: | |
387 explicit StaticString( const char *czstring ) | |
388 : str_( czstring ) | |
389 { | |
390 } | |
391 | |
392 operator const char *() const | |
393 { | |
394 return str_; | |
395 } | |
396 | |
397 const char *c_str() const | |
398 { | |
399 return str_; | |
400 } | |
401 | |
402 private: | |
403 const char *str_; | |
404 }; | |
405 | |
406 /** \brief Represents a <a HREF="http://www.json.org">JSON</a> value. | |
407 * | |
408 * This class is a discriminated union wrapper that can represents a: | |
409 * - signed integer [range: Value::minInt - Value::maxInt] | |
410 * - unsigned integer (range: 0 - Value::maxUInt) | |
411 * - double | |
412 * - UTF-8 string | |
413 * - boolean | |
414 * - 'null' | |
415 * - an ordered list of Value | |
416 * - collection of name/value pairs (javascript object) | |
417 * | |
418 * The type of the held value is represented by a #ValueType and | |
419 * can be obtained using type(). | |
420 * | |
421 * values of an #objectValue or #arrayValue can be accessed using operator[]() methods. | |
422 * Non const methods will automatically create the a #nullValue element | |
423 * if it does not exist. | |
424 * The sequence of an #arrayValue will be automatically resize and initialized | |
425 * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue. | |
426 * | |
427 * The get() methods can be used to obtanis default value in the case the required element | |
428 * does not exist. | |
429 * | |
430 * It is possible to iterate over the list of a #objectValue values using | |
431 * the getMemberNames() method. | |
432 */ | |
433 class JSON_API Value | |
434 { | |
435 friend class ValueIteratorBase; | |
436 # ifdef JSON_VALUE_USE_INTERNAL_MAP | |
437 friend class ValueInternalLink; | |
438 friend class ValueInternalMap; | |
439 # endif | |
440 public: | |
441 typedef std::vector<std::string> Members; | |
442 typedef ValueIterator iterator; | |
443 typedef ValueConstIterator const_iterator; | |
444 typedef Json::UInt UInt; | |
445 typedef Json::Int Int; | |
446 # if defined(JSON_HAS_INT64) | |
447 typedef Json::UInt64 UInt64; | |
448 typedef Json::Int64 Int64; | |
449 #endif // defined(JSON_HAS_INT64) | |
450 typedef Json::LargestInt LargestInt; | |
451 typedef Json::LargestUInt LargestUInt; | |
452 typedef Json::ArrayIndex ArrayIndex; | |
453 | |
454 static const Value null; | |
455 /// Minimum signed integer value that can be stored in a Json::Value. | |
456 static const LargestInt minLargestInt; | |
457 /// Maximum signed integer value that can be stored in a Json::Value. | |
458 static const LargestInt maxLargestInt; | |
459 /// Maximum unsigned integer value that can be stored in a Json::Value. | |
460 static const LargestUInt maxLargestUInt; | |
461 | |
462 /// Minimum signed int value that can be stored in a Json::Value. | |
463 static const Int minInt; | |
464 /// Maximum signed int value that can be stored in a Json::Value. | |
465 static const Int maxInt; | |
466 /// Maximum unsigned int value that can be stored in a Json::Value. | |
467 static const UInt maxUInt; | |
468 | |
469 /// Minimum signed 64 bits int value that can be stored in a Json::Value. | |
470 static const Int64 minInt64; | |
471 /// Maximum signed 64 bits int value that can be stored in a Json::Value. | |
472 static const Int64 maxInt64; | |
473 /// Maximum unsigned 64 bits int value that can be stored in a Json::Value. | |
474 static const UInt64 maxUInt64; | |
475 | |
476 private: | |
477 #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION | |
478 # ifndef JSON_VALUE_USE_INTERNAL_MAP | |
479 class CZString | |
480 { | |
481 public: | |
482 enum DuplicationPolicy | |
483 { | |
484 noDuplication = 0, | |
485 duplicate, | |
486 duplicateOnCopy | |
487 }; | |
488 CZString( ArrayIndex index ); | |
489 CZString( const char *cstr, DuplicationPolicy allocate ); | |
490 CZString( const CZString &other ); | |
491 ~CZString(); | |
492 CZString &operator =( const CZString &other ); | |
493 bool operator<( const CZString &other ) const; | |
494 bool operator==( const CZString &other ) const; | |
495 ArrayIndex index() const; | |
496 const char *c_str() const; | |
497 bool isStaticString() const; | |
498 private: | |
499 void swap( CZString &other ); | |
500 const char *cstr_; | |
501 ArrayIndex index_; | |
502 }; | |
503 | |
504 public: | |
505 # ifndef JSON_USE_CPPTL_SMALLMAP | |
506 typedef std::map<CZString, Value> ObjectValues; | |
507 # else | |
508 typedef CppTL::SmallMap<CZString, Value> ObjectValues; | |
509 # endif // ifndef JSON_USE_CPPTL_SMALLMAP | |
510 # endif // ifndef JSON_VALUE_USE_INTERNAL_MAP | |
511 #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION | |
512 | |
513 public: | |
514 /** \brief Create a default Value of the given type. | |
515 | |
516 This is a very useful constructor. | |
517 To create an empty array, pass arrayValue. | |
518 To create an empty object, pass objectValue. | |
519 Another Value can then be set to this one by assignment. | |
520 This is useful since clear() and resize() will not alter types. | |
521 | |
522 Examples: | |
523 \code | |
524 Json::Value null_value; // null | |
525 Json::Value arr_value(Json::arrayValue); // [] | |
526 Json::Value obj_value(Json::objectValue); // {} | |
527 \endcode | |
528 */ | |
529 Value( ValueType type = nullValue ); | |
530 Value( Int value ); | |
531 Value( UInt value ); | |
532 #if defined(JSON_HAS_INT64) | |
533 Value( Int64 value ); | |
534 Value( UInt64 value ); | |
535 #endif // if defined(JSON_HAS_INT64) | |
536 Value( double value ); | |
537 Value( const char *value ); | |
538 Value( const char *beginValue, const char *endValue ); | |
539 /** \brief Constructs a value from a static string. | |
540 | |
541 * Like other value string constructor but do not duplicate the string for | |
542 * internal storage. The given string must remain alive after the call to this | |
543 * constructor. | |
544 * Example of usage: | |
545 * \code | |
546 * Json::Value aValue( StaticString("some text") ); | |
547 * \endcode | |
548 */ | |
549 Value( const StaticString &value ); | |
550 Value( const std::string &value ); | |
551 # ifdef JSON_USE_CPPTL | |
552 Value( const CppTL::ConstString &value ); | |
553 # endif | |
554 Value( bool value ); | |
555 Value( const Value &other ); | |
556 ~Value(); | |
557 | |
558 Value &operator=( const Value &other ); | |
559 /// Swap values. | |
560 /// \note Currently, comments are intentionally not swapped, for | |
561 /// both logic and efficiency. | |
562 void swap( Value &other ); | |
563 | |
564 ValueType type() const; | |
565 | |
566 bool operator <( const Value &other ) const; | |
567 bool operator <=( const Value &other ) const; | |
568 bool operator >=( const Value &other ) const; | |
569 bool operator >( const Value &other ) const; | |
570 | |
571 bool operator ==( const Value &other ) const; | |
572 bool operator !=( const Value &other ) const; | |
573 | |
574 int compare( const Value &other ) const; | |
575 | |
576 const char *asCString() const; | |
577 std::string asString() const; | |
578 # ifdef JSON_USE_CPPTL | |
579 CppTL::ConstString asConstString() const; | |
580 # endif | |
581 Int asInt() const; | |
582 UInt asUInt() const; | |
583 Int64 asInt64() const; | |
584 UInt64 asUInt64() const; | |
585 LargestInt asLargestInt() const; | |
586 LargestUInt asLargestUInt() const; | |
587 float asFloat() const; | |
588 double asDouble() const; | |
589 bool asBool() const; | |
590 | |
591 bool isNull() const; | |
592 bool isBool() const; | |
593 bool isInt() const; | |
594 bool isUInt() const; | |
595 bool isIntegral() const; | |
596 bool isDouble() const; | |
597 bool isNumeric() const; | |
598 bool isString() const; | |
599 bool isArray() const; | |
600 bool isObject() const; | |
601 | |
602 bool isConvertibleTo( ValueType other ) const; | |
603 | |
604 /// Number of values in array or object | |
605 ArrayIndex size() const; | |
606 | |
607 /// \brief Return true if empty array, empty object, or null; | |
608 /// otherwise, false. | |
609 bool empty() const; | |
610 | |
611 /// Return isNull() | |
612 bool operator!() const; | |
613 | |
614 /// Remove all object members and array elements. | |
615 /// \pre type() is arrayValue, objectValue, or nullValue | |
616 /// \post type() is unchanged | |
617 void clear(); | |
618 | |
619 /// Resize the array to size elements. | |
620 /// New elements are initialized to null. | |
621 /// May only be called on nullValue or arrayValue. | |
622 /// \pre type() is arrayValue or nullValue | |
623 /// \post type() is arrayValue | |
624 void resize( ArrayIndex size ); | |
625 | |
626 /// Access an array element (zero based index ). | |
627 /// If the array contains less than index element, then null value are inserted | |
628 /// in the array so that its size is index+1. | |
629 /// (You may need to say 'value[0u]' to get your compiler to distinguish | |
630 /// this from the operator[] which takes a string.) | |
631 Value &operator[]( ArrayIndex index ); | |
632 | |
633 /// Access an array element (zero based index ). | |
634 /// If the array contains less than index element, then null value are inserted | |
635 /// in the array so that its size is index+1. | |
636 /// (You may need to say 'value[0u]' to get your compiler to distinguish | |
637 /// this from the operator[] which takes a string.) | |
638 Value &operator[]( int index ); | |
639 | |
640 /// Access an array element (zero based index ) | |
641 /// (You may need to say 'value[0u]' to get your compiler to distinguish | |
642 /// this from the operator[] which takes a string.) | |
643 const Value &operator[]( ArrayIndex index ) const; | |
644 | |
645 /// Access an array element (zero based index ) | |
646 /// (You may need to say 'value[0u]' to get your compiler to distinguish | |
647 /// this from the operator[] which takes a string.) | |
648 const Value &operator[]( int index ) const; | |
649 | |
650 /// If the array contains at least index+1 elements, returns the element value, | |
651 /// otherwise returns defaultValue. | |
652 Value get( ArrayIndex index, | |
653 const Value &defaultValue ) const; | |
654 /// Return true if index < size(). | |
655 bool isValidIndex( ArrayIndex index ) const; | |
656 /// \brief Append value to array at the end. | |
657 /// | |
658 /// Equivalent to jsonvalue[jsonvalue.size()] = value; | |
659 Value &append( const Value &value ); | |
660 | |
661 /// Access an object value by name, create a null member if it does not exist. | |
662 Value &operator[]( const char *key ); | |
663 /// Access an object value by name, returns null if there is no member with that name. | |
664 const Value &operator[]( const char *key ) const; | |
665 /// Access an object value by name, create a null member if it does not exist. | |
666 Value &operator[]( const std::string &key ); | |
667 /// Access an object value by name, returns null if there is no member with that name. | |
668 const Value &operator[]( const std::string &key ) const; | |
669 /** \brief Access an object value by name, create a null member if it does not exist. | |
670 | |
671 * If the object as no entry for that name, then the member name used to store | |
672 * the new entry is not duplicated. | |
673 * Example of use: | |
674 * \code | |
675 * Json::Value object; | |
676 * static const StaticString code("code"); | |
677 * object[code] = 1234; | |
678 * \endcode | |
679 */ | |
680 Value &operator[]( const StaticString &key ); | |
681 # ifdef JSON_USE_CPPTL | |
682 /// Access an object value by name, create a null member if it does not exist. | |
683 Value &operator[]( const CppTL::ConstString &key ); | |
684 /// Access an object value by name, returns null if there is no member with that name. | |
685 const Value &operator[]( const CppTL::ConstString &key ) const; | |
686 # endif | |
687 /// Return the member named key if it exist, defaultValue otherwise. | |
688 Value get( const char *key, | |
689 const Value &defaultValue ) const; | |
690 /// Return the member named key if it exist, defaultValue otherwise. | |
691 Value get( const std::string &key, | |
692 const Value &defaultValue ) const; | |
693 # ifdef JSON_USE_CPPTL | |
694 /// Return the member named key if it exist, defaultValue otherwise. | |
695 Value get( const CppTL::ConstString &key, | |
696 const Value &defaultValue ) const; | |
697 # endif | |
698 /// \brief Remove and return the named member. | |
699 /// | |
700 /// Do nothing if it did not exist. | |
701 /// \return the removed Value, or null. | |
702 /// \pre type() is objectValue or nullValue | |
703 /// \post type() is unchanged | |
704 Value removeMember( const char* key ); | |
705 /// Same as removeMember(const char*) | |
706 Value removeMember( const std::string &key ); | |
707 | |
708 /// Return true if the object has a member named key. | |
709 bool isMember( const char *key ) const; | |
710 /// Return true if the object has a member named key. | |
711 bool isMember( const std::string &key ) const; | |
712 # ifdef JSON_USE_CPPTL | |
713 /// Return true if the object has a member named key. | |
714 bool isMember( const CppTL::ConstString &key ) const; | |
715 # endif | |
716 | |
717 /// \brief Return a list of the member names. | |
718 /// | |
719 /// If null, return an empty list. | |
720 /// \pre type() is objectValue or nullValue | |
721 /// \post if type() was nullValue, it remains nullValue | |
722 Members getMemberNames() const; | |
723 | |
724 //# ifdef JSON_USE_CPPTL | |
725 // EnumMemberNames enumMemberNames() const; | |
726 // EnumValues enumValues() const; | |
727 //# endif | |
728 | |
729 /// Comments must be //... or /* ... */ | |
730 void setComment( const char *comment, | |
731 CommentPlacement placement ); | |
732 /// Comments must be //... or /* ... */ | |
733 void setComment( const std::string &comment, | |
734 CommentPlacement placement ); | |
735 bool hasComment( CommentPlacement placement ) const; | |
736 /// Include delimiters and embedded newlines. | |
737 std::string getComment( CommentPlacement placement ) const; | |
738 | |
739 std::string toStyledString() const; | |
740 | |
741 const_iterator begin() const; | |
742 const_iterator end() const; | |
743 | |
744 iterator begin(); | |
745 iterator end(); | |
746 | |
747 private: | |
748 Value &resolveReference( const char *key, | |
749 bool isStatic ); | |
750 | |
751 # ifdef JSON_VALUE_USE_INTERNAL_MAP | |
752 inline bool isItemAvailable() const | |
753 { | |
754 return itemIsUsed_ == 0; | |
755 } | |
756 | |
757 inline void setItemUsed( bool isUsed = true ) | |
758 { | |
759 itemIsUsed_ = isUsed ? 1 : 0; | |
760 } | |
761 | |
762 inline bool isMemberNameStatic() const | |
763 { | |
764 return memberNameIsStatic_ == 0; | |
765 } | |
766 | |
767 inline void setMemberNameIsStatic( bool isStatic ) | |
768 { | |
769 memberNameIsStatic_ = isStatic ? 1 : 0; | |
770 } | |
771 # endif // # ifdef JSON_VALUE_USE_INTERNAL_MAP | |
772 | |
773 private: | |
774 struct CommentInfo | |
775 { | |
776 CommentInfo(); | |
777 ~CommentInfo(); | |
778 | |
779 void setComment( const char *text ); | |
780 | |
781 char *comment_; | |
782 }; | |
783 | |
784 //struct MemberNamesTransform | |
785 //{ | |
786 // typedef const char *result_type; | |
787 // const char *operator()( const CZString &name ) const | |
788 // { | |
789 // return name.c_str(); | |
790 // } | |
791 //}; | |
792 | |
793 union ValueHolder | |
794 { | |
795 LargestInt int_; | |
796 LargestUInt uint_; | |
797 double real_; | |
798 bool bool_; | |
799 char *string_; | |
800 # ifdef JSON_VALUE_USE_INTERNAL_MAP | |
801 ValueInternalArray *array_; | |
802 ValueInternalMap *map_; | |
803 #else | |
804 ObjectValues *map_; | |
805 # endif | |
806 } value_; | |
807 ValueType type_ : 8; | |
808 int allocated_ : 1; // Notes: if declared as bool, bitfield is useless. | |
809 # ifdef JSON_VALUE_USE_INTERNAL_MAP | |
810 unsigned int itemIsUsed_ : 1; // used by the ValueInternalMap container. | |
811 int memberNameIsStatic_ : 1; // used by the ValueInternalMap container. | |
812 # endif | |
813 CommentInfo *comments_; | |
814 }; | |
815 | |
816 | |
817 /** \brief Experimental and untested: represents an element of the "path" to access a node. | |
818 */ | |
819 class PathArgument | |
820 { | |
821 public: | |
822 friend class Path; | |
823 | |
824 PathArgument(); | |
825 PathArgument( ArrayIndex index ); | |
826 PathArgument( const char *key ); | |
827 PathArgument( const std::string &key ); | |
828 | |
829 private: | |
830 enum Kind | |
831 { | |
832 kindNone = 0, | |
833 kindIndex, | |
834 kindKey | |
835 }; | |
836 std::string key_; | |
837 ArrayIndex index_; | |
838 Kind kind_; | |
839 }; | |
840 | |
841 /** \brief Experimental and untested: represents a "path" to access a node. | |
842 * | |
843 * Syntax: | |
844 * - "." => root node | |
845 * - ".[n]" => elements at index 'n' of root node (an array value) | |
846 * - ".name" => member named 'name' of root node (an object value) | |
847 * - ".name1.name2.name3" | |
848 * - ".[0][1][2].name1[3]" | |
849 * - ".%" => member name is provided as parameter | |
850 * - ".[%]" => index is provied as parameter | |
851 */ | |
852 class Path | |
853 { | |
854 public: | |
855 Path( const std::string &path, | |
856 const PathArgument &a1 = PathArgument(), | |
857 const PathArgument &a2 = PathArgument(), | |
858 const PathArgument &a3 = PathArgument(), | |
859 const PathArgument &a4 = PathArgument(), | |
860 const PathArgument &a5 = PathArgument() ); | |
861 | |
862 const Value &resolve( const Value &root ) const; | |
863 Value resolve( const Value &root, | |
864 const Value &defaultValue ) const; | |
865 /// Creates the "path" to access the specified node and returns a reference on the node. | |
866 Value &make( Value &root ) const; | |
867 | |
868 private: | |
869 typedef std::vector<const PathArgument *> InArgs; | |
870 typedef std::vector<PathArgument> Args; | |
871 | |
872 void makePath( const std::string &path, | |
873 const InArgs &in ); | |
874 void addPathInArg( const std::string &path, | |
875 const InArgs &in, | |
876 InArgs::const_iterator &itInArg, | |
877 PathArgument::Kind kind ); | |
878 void invalidPath( const std::string &path, | |
879 int location ); | |
880 | |
881 Args args_; | |
882 }; | |
883 | |
884 | |
885 | |
886 #ifdef JSON_VALUE_USE_INTERNAL_MAP | |
887 /** \brief Allocator to customize Value internal map. | |
888 * Below is an example of a simple implementation (default implementation actually | |
889 * use memory pool for speed). | |
890 * \code | |
891 class DefaultValueMapAllocator : public ValueMapAllocator | |
892 { | |
893 public: // overridden from ValueMapAllocator | |
894 virtual ValueInternalMap *newMap() | |
895 { | |
896 return new ValueInternalMap(); | |
897 } | |
898 | |
899 virtual ValueInternalMap *newMapCopy( const ValueInternalMap &other ) | |
900 { | |
901 return new ValueInternalMap( other ); | |
902 } | |
903 | |
904 virtual void destructMap( ValueInternalMap *map ) | |
905 { | |
906 delete map; | |
907 } | |
908 | |
909 virtual ValueInternalLink *allocateMapBuckets( unsigned int size ) | |
910 { | |
911 return new ValueInternalLink[size]; | |
912 } | |
913 | |
914 virtual void releaseMapBuckets( ValueInternalLink *links ) | |
915 { | |
916 delete [] links; | |
917 } | |
918 | |
919 virtual ValueInternalLink *allocateMapLink() | |
920 { | |
921 return new ValueInternalLink(); | |
922 } | |
923 | |
924 virtual void releaseMapLink( ValueInternalLink *link ) | |
925 { | |
926 delete link; | |
927 } | |
928 }; | |
929 * \endcode | |
930 */ | |
931 class JSON_API ValueMapAllocator | |
932 { | |
933 public: | |
934 virtual ~ValueMapAllocator(); | |
935 virtual ValueInternalMap *newMap() = 0; | |
936 virtual ValueInternalMap *newMapCopy( const ValueInternalMap &other ) = 0; | |
937 virtual void destructMap( ValueInternalMap *map ) = 0; | |
938 virtual ValueInternalLink *allocateMapBuckets( unsigned int size ) = 0; | |
939 virtual void releaseMapBuckets( ValueInternalLink *links ) = 0; | |
940 virtual ValueInternalLink *allocateMapLink() = 0; | |
941 virtual void releaseMapLink( ValueInternalLink *link ) = 0; | |
942 }; | |
943 | |
944 /** \brief ValueInternalMap hash-map bucket chain link (for internal use only). | |
945 * \internal previous_ & next_ allows for bidirectional traversal. | |
946 */ | |
947 class JSON_API ValueInternalLink | |
948 { | |
949 public: | |
950 enum { itemPerLink = 6 }; // sizeof(ValueInternalLink) = 128 on 32 bits architecture. | |
951 enum InternalFlags { | |
952 flagAvailable = 0, | |
953 flagUsed = 1 | |
954 }; | |
955 | |
956 ValueInternalLink(); | |
957 | |
958 ~ValueInternalLink(); | |
959 | |
960 Value items_[itemPerLink]; | |
961 char *keys_[itemPerLink]; | |
962 ValueInternalLink *previous_; | |
963 ValueInternalLink *next_; | |
964 }; | |
965 | |
966 | |
967 /** \brief A linked page based hash-table implementation used internally by Value. | |
968 * \internal ValueInternalMap is a tradional bucket based hash-table, with a linked | |
969 * list in each bucket to handle collision. There is an addional twist in that | |
970 * each node of the collision linked list is a page containing a fixed amount of | |
971 * value. This provides a better compromise between memory usage and speed. | |
972 * | |
973 * Each bucket is made up of a chained list of ValueInternalLink. The last | |
974 * link of a given bucket can be found in the 'previous_' field of the following bucket. | |
975 * The last link of the last bucket is stored in tailLink_ as it has no following bucket. | |
976 * Only the last link of a bucket may contains 'available' item. The last link always | |
977 * contains at least one element unless is it the bucket one very first link. | |
978 */ | |
979 class JSON_API ValueInternalMap | |
980 { | |
981 friend class ValueIteratorBase; | |
982 friend class Value; | |
983 public: | |
984 typedef unsigned int HashKey; | |
985 typedef unsigned int BucketIndex; | |
986 | |
987 # ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION | |
988 struct IteratorState | |
989 { | |
990 IteratorState() | |
991 : map_(0) | |
992 , link_(0) | |
993 , itemIndex_(0) | |
994 , bucketIndex_(0) | |
995 { | |
996 } | |
997 ValueInternalMap *map_; | |
998 ValueInternalLink *link_; | |
999 BucketIndex itemIndex_; | |
1000 BucketIndex bucketIndex_; | |
1001 }; | |
1002 # endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION | |
1003 | |
1004 ValueInternalMap(); | |
1005 ValueInternalMap( const ValueInternalMap &other ); | |
1006 ValueInternalMap &operator =( const ValueInternalMap &other ); | |
1007 ~ValueInternalMap(); | |
1008 | |
1009 void swap( ValueInternalMap &other ); | |
1010 | |
1011 BucketIndex size() const; | |
1012 | |
1013 void clear(); | |
1014 | |
1015 bool reserveDelta( BucketIndex growth ); | |
1016 | |
1017 bool reserve( BucketIndex newItemCount ); | |
1018 | |
1019 const Value *find( const char *key ) const; | |
1020 | |
1021 Value *find( const char *key ); | |
1022 | |
1023 Value &resolveReference( const char *key, | |
1024 bool isStatic ); | |
1025 | |
1026 void remove( const char *key ); | |
1027 | |
1028 void doActualRemove( ValueInternalLink *link, | |
1029 BucketIndex index, | |
1030 BucketIndex bucketIndex ); | |
1031 | |
1032 ValueInternalLink *&getLastLinkInBucket( BucketIndex bucketIndex ); | |
1033 | |
1034 Value &setNewItem( const char *key, | |
1035 bool isStatic, | |
1036 ValueInternalLink *link, | |
1037 BucketIndex index ); | |
1038 | |
1039 Value &unsafeAdd( const char *key, | |
1040 bool isStatic, | |
1041 HashKey hashedKey ); | |
1042 | |
1043 HashKey hash( const char *key ) const; | |
1044 | |
1045 int compare( const ValueInternalMap &other ) const; | |
1046 | |
1047 private: | |
1048 void makeBeginIterator( IteratorState &it ) const; | |
1049 void makeEndIterator( IteratorState &it ) const; | |
1050 static bool equals( const IteratorState &x, const IteratorState &other ); | |
1051 static void increment( IteratorState &iterator ); | |
1052 static void incrementBucket( IteratorState &iterator ); | |
1053 static void decrement( IteratorState &iterator ); | |
1054 static const char *key( const IteratorState &iterator ); | |
1055 static const char *key( const IteratorState &iterator, bool &isStatic ); | |
1056 static Value &value( const IteratorState &iterator ); | |
1057 static int distance( const IteratorState &x, const IteratorState &y ); | |
1058 | |
1059 private: | |
1060 ValueInternalLink *buckets_; | |
1061 ValueInternalLink *tailLink_; | |
1062 BucketIndex bucketsSize_; | |
1063 BucketIndex itemCount_; | |
1064 }; | |
1065 | |
1066 /** \brief A simplified deque implementation used internally by Value. | |
1067 * \internal | |
1068 * It is based on a list of fixed "page", each page contains a fixed number of items. | |
1069 * Instead of using a linked-list, a array of pointer is used for fast item look-up. | |
1070 * Look-up for an element is as follow: | |
1071 * - compute page index: pageIndex = itemIndex / itemsPerPage | |
1072 * - look-up item in page: pages_[pageIndex][itemIndex % itemsPerPage] | |
1073 * | |
1074 * Insertion is amortized constant time (only the array containing the index of pointers | |
1075 * need to be reallocated when items are appended). | |
1076 */ | |
1077 class JSON_API ValueInternalArray | |
1078 { | |
1079 friend class Value; | |
1080 friend class ValueIteratorBase; | |
1081 public: | |
1082 enum { itemsPerPage = 8 }; // should be a power of 2 for fast divide and modulo. | |
1083 typedef Value::ArrayIndex ArrayIndex; | |
1084 typedef unsigned int PageIndex; | |
1085 | |
1086 # ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION | |
1087 struct IteratorState // Must be a POD | |
1088 { | |
1089 IteratorState() | |
1090 : array_(0) | |
1091 , currentPageIndex_(0) | |
1092 , currentItemIndex_(0) | |
1093 { | |
1094 } | |
1095 ValueInternalArray *array_; | |
1096 Value **currentPageIndex_; | |
1097 unsigned int currentItemIndex_; | |
1098 }; | |
1099 # endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION | |
1100 | |
1101 ValueInternalArray(); | |
1102 ValueInternalArray( const ValueInternalArray &other ); | |
1103 ValueInternalArray &operator =( const ValueInternalArray &other ); | |
1104 ~ValueInternalArray(); | |
1105 void swap( ValueInternalArray &other ); | |
1106 | |
1107 void clear(); | |
1108 void resize( ArrayIndex newSize ); | |
1109 | |
1110 Value &resolveReference( ArrayIndex index ); | |
1111 | |
1112 Value *find( ArrayIndex index ) const; | |
1113 | |
1114 ArrayIndex size() const; | |
1115 | |
1116 int compare( const ValueInternalArray &other ) const; | |
1117 | |
1118 private: | |
1119 static bool equals( const IteratorState &x, const IteratorState &other ); | |
1120 static void increment( IteratorState &iterator ); | |
1121 static void decrement( IteratorState &iterator ); | |
1122 static Value &dereference( const IteratorState &iterator ); | |
1123 static Value &unsafeDereference( const IteratorState &iterator ); | |
1124 static int distance( const IteratorState &x, const IteratorState &y ); | |
1125 static ArrayIndex indexOf( const IteratorState &iterator ); | |
1126 void makeBeginIterator( IteratorState &it ) const; | |
1127 void makeEndIterator( IteratorState &it ) const; | |
1128 void makeIterator( IteratorState &it, ArrayIndex index ) const; | |
1129 | |
1130 void makeIndexValid( ArrayIndex index ); | |
1131 | |
1132 Value **pages_; | |
1133 ArrayIndex size_; | |
1134 PageIndex pageCount_; | |
1135 }; | |
1136 | |
1137 /** \brief Experimental: do not use. Allocator to customize Value internal array. | |
1138 * Below is an example of a simple implementation (actual implementation use | |
1139 * memory pool). | |
1140 \code | |
1141 class DefaultValueArrayAllocator : public ValueArrayAllocator | |
1142 { | |
1143 public: // overridden from ValueArrayAllocator | |
1144 virtual ~DefaultValueArrayAllocator() | |
1145 { | |
1146 } | |
1147 | |
1148 virtual ValueInternalArray *newArray() | |
1149 { | |
1150 return new ValueInternalArray(); | |
1151 } | |
1152 | |
1153 virtual ValueInternalArray *newArrayCopy( const ValueInternalArray &other ) | |
1154 { | |
1155 return new ValueInternalArray( other ); | |
1156 } | |
1157 | |
1158 virtual void destruct( ValueInternalArray *array ) | |
1159 { | |
1160 delete array; | |
1161 } | |
1162 | |
1163 virtual void reallocateArrayPageIndex( Value **&indexes, | |
1164 ValueInternalArray::PageIndex &indexCount, | |
1165 ValueInternalArray::PageIndex minNewIndexCount ) | |
1166 { | |
1167 ValueInternalArray::PageIndex newIndexCount = (indexCount*3)/2 + 1; | |
1168 if ( minNewIndexCount > newIndexCount ) | |
1169 newIndexCount = minNewIndexCount; | |
1170 void *newIndexes = realloc( indexes, sizeof(Value*) * newIndexCount ); | |
1171 if ( !newIndexes ) | |
1172 throw std::bad_alloc(); | |
1173 indexCount = newIndexCount; | |
1174 indexes = static_cast<Value **>( newIndexes ); | |
1175 } | |
1176 virtual void releaseArrayPageIndex( Value **indexes, | |
1177 ValueInternalArray::PageIndex indexCount ) | |
1178 { | |
1179 if ( indexes ) | |
1180 free( indexes ); | |
1181 } | |
1182 | |
1183 virtual Value *allocateArrayPage() | |
1184 { | |
1185 return static_cast<Value *>( malloc( sizeof(Value) * ValueInternalArray::itemsPerPage ) ); | |
1186 } | |
1187 | |
1188 virtual void releaseArrayPage( Value *value ) | |
1189 { | |
1190 if ( value ) | |
1191 free( value ); | |
1192 } | |
1193 }; | |
1194 \endcode | |
1195 */ | |
1196 class JSON_API ValueArrayAllocator | |
1197 { | |
1198 public: | |
1199 virtual ~ValueArrayAllocator(); | |
1200 virtual ValueInternalArray *newArray() = 0; | |
1201 virtual ValueInternalArray *newArrayCopy( const ValueInternalArray &other ) = 0; | |
1202 virtual void destructArray( ValueInternalArray *array ) = 0; | |
1203 /** \brief Reallocate array page index. | |
1204 * Reallocates an array of pointer on each page. | |
1205 * \param indexes [input] pointer on the current index. May be \c NULL. | |
1206 * [output] pointer on the new index of at least | |
1207 * \a minNewIndexCount pages. | |
1208 * \param indexCount [input] current number of pages in the index. | |
1209 * [output] number of page the reallocated index can handle. | |
1210 * \b MUST be >= \a minNewIndexCount. | |
1211 * \param minNewIndexCount Minimum number of page the new index must be able to | |
1212 * handle. | |
1213 */ | |
1214 virtual void reallocateArrayPageIndex( Value **&indexes, | |
1215 ValueInternalArray::PageIndex &indexCount, | |
1216 ValueInternalArray::PageIndex minNewIndexCount ) = 0; | |
1217 virtual void releaseArrayPageIndex( Value **indexes, | |
1218 ValueInternalArray::PageIndex indexCount ) = 0; | |
1219 virtual Value *allocateArrayPage() = 0; | |
1220 virtual void releaseArrayPage( Value *value ) = 0; | |
1221 }; | |
1222 #endif // #ifdef JSON_VALUE_USE_INTERNAL_MAP | |
1223 | |
1224 | |
1225 /** \brief base class for Value iterators. | |
1226 * | |
1227 */ | |
1228 class ValueIteratorBase | |
1229 { | |
1230 public: | |
1231 typedef unsigned int size_t; | |
1232 typedef int difference_type; | |
1233 typedef ValueIteratorBase SelfType; | |
1234 | |
1235 ValueIteratorBase(); | |
1236 #ifndef JSON_VALUE_USE_INTERNAL_MAP | |
1237 explicit ValueIteratorBase( const Value::ObjectValues::iterator ¤t ); | |
1238 #else | |
1239 ValueIteratorBase( const ValueInternalArray::IteratorState &state ); | |
1240 ValueIteratorBase( const ValueInternalMap::IteratorState &state ); | |
1241 #endif | |
1242 | |
1243 bool operator ==( const SelfType &other ) const | |
1244 { | |
1245 return isEqual( other ); | |
1246 } | |
1247 | |
1248 bool operator !=( const SelfType &other ) const | |
1249 { | |
1250 return !isEqual( other ); | |
1251 } | |
1252 | |
1253 difference_type operator -( const SelfType &other ) const | |
1254 { | |
1255 return computeDistance( other ); | |
1256 } | |
1257 | |
1258 /// Return either the index or the member name of the referenced value as a Value. | |
1259 Value key() const; | |
1260 | |
1261 /// Return the index of the referenced Value. -1 if it is not an arrayValue. | |
1262 UInt index() const; | |
1263 | |
1264 /// Return the member name of the referenced Value. "" if it is not an objectValue. | |
1265 const char *memberName() const; | |
1266 | |
1267 protected: | |
1268 Value &deref() const; | |
1269 | |
1270 void increment(); | |
1271 | |
1272 void decrement(); | |
1273 | |
1274 difference_type computeDistance( const SelfType &other ) const; | |
1275 | |
1276 bool isEqual( const SelfType &other ) const; | |
1277 | |
1278 void copy( const SelfType &other ); | |
1279 | |
1280 private: | |
1281 #ifndef JSON_VALUE_USE_INTERNAL_MAP | |
1282 Value::ObjectValues::iterator current_; | |
1283 // Indicates that iterator is for a null value. | |
1284 bool isNull_; | |
1285 #else | |
1286 union | |
1287 { | |
1288 ValueInternalArray::IteratorState array_; | |
1289 ValueInternalMap::IteratorState map_; | |
1290 } iterator_; | |
1291 bool isArray_; | |
1292 #endif | |
1293 }; | |
1294 | |
1295 /** \brief const iterator for object and array value. | |
1296 * | |
1297 */ | |
1298 class ValueConstIterator : public ValueIteratorBase | |
1299 { | |
1300 friend class Value; | |
1301 public: | |
1302 typedef unsigned int size_t; | |
1303 typedef int difference_type; | |
1304 typedef const Value &reference; | |
1305 typedef const Value *pointer; | |
1306 typedef ValueConstIterator SelfType; | |
1307 | |
1308 ValueConstIterator(); | |
1309 private: | |
1310 /*! \internal Use by Value to create an iterator. | |
1311 */ | |
1312 #ifndef JSON_VALUE_USE_INTERNAL_MAP | |
1313 explicit ValueConstIterator( const Value::ObjectValues::iterator ¤t ); | |
1314 #else | |
1315 ValueConstIterator( const ValueInternalArray::IteratorState &state ); | |
1316 ValueConstIterator( const ValueInternalMap::IteratorState &state ); | |
1317 #endif | |
1318 public: | |
1319 SelfType &operator =( const ValueIteratorBase &other ); | |
1320 | |
1321 SelfType operator++( int ) | |
1322 { | |
1323 SelfType temp( *this ); | |
1324 ++*this; | |
1325 return temp; | |
1326 } | |
1327 | |
1328 SelfType operator--( int ) | |
1329 { | |
1330 SelfType temp( *this ); | |
1331 --*this; | |
1332 return temp; | |
1333 } | |
1334 | |
1335 SelfType &operator--() | |
1336 { | |
1337 decrement(); | |
1338 return *this; | |
1339 } | |
1340 | |
1341 SelfType &operator++() | |
1342 { | |
1343 increment(); | |
1344 return *this; | |
1345 } | |
1346 | |
1347 reference operator *() const | |
1348 { | |
1349 return deref(); | |
1350 } | |
1351 }; | |
1352 | |
1353 | |
1354 /** \brief Iterator for object and array value. | |
1355 */ | |
1356 class ValueIterator : public ValueIteratorBase | |
1357 { | |
1358 friend class Value; | |
1359 public: | |
1360 typedef unsigned int size_t; | |
1361 typedef int difference_type; | |
1362 typedef Value &reference; | |
1363 typedef Value *pointer; | |
1364 typedef ValueIterator SelfType; | |
1365 | |
1366 ValueIterator(); | |
1367 ValueIterator( const ValueConstIterator &other ); | |
1368 ValueIterator( const ValueIterator &other ); | |
1369 private: | |
1370 /*! \internal Use by Value to create an iterator. | |
1371 */ | |
1372 #ifndef JSON_VALUE_USE_INTERNAL_MAP | |
1373 explicit ValueIterator( const Value::ObjectValues::iterator ¤t ); | |
1374 #else | |
1375 ValueIterator( const ValueInternalArray::IteratorState &state ); | |
1376 ValueIterator( const ValueInternalMap::IteratorState &state ); | |
1377 #endif | |
1378 public: | |
1379 | |
1380 SelfType &operator =( const SelfType &other ); | |
1381 | |
1382 SelfType operator++( int ) | |
1383 { | |
1384 SelfType temp( *this ); | |
1385 ++*this; | |
1386 return temp; | |
1387 } | |
1388 | |
1389 SelfType operator--( int ) | |
1390 { | |
1391 SelfType temp( *this ); | |
1392 --*this; | |
1393 return temp; | |
1394 } | |
1395 | |
1396 SelfType &operator--() | |
1397 { | |
1398 decrement(); | |
1399 return *this; | |
1400 } | |
1401 | |
1402 SelfType &operator++() | |
1403 { | |
1404 increment(); | |
1405 return *this; | |
1406 } | |
1407 | |
1408 reference operator *() const | |
1409 { | |
1410 return deref(); | |
1411 } | |
1412 }; | |
1413 | |
1414 | |
1415 } // namespace Json | |
1416 | |
1417 | |
1418 #endif // CPPTL_JSON_H_INCLUDED | |
1419 | |
1420 // ////////////////////////////////////////////////////////////////////// | |
1421 // End of content of file: include/json/value.h | |
1422 // ////////////////////////////////////////////////////////////////////// | |
1423 | |
1424 | |
1425 | |
1426 | |
1427 | |
1428 | |
1429 // ////////////////////////////////////////////////////////////////////// | |
1430 // Beginning of content of file: include/json/reader.h | |
1431 // ////////////////////////////////////////////////////////////////////// | |
1432 | |
1433 // Copyright 2007-2010 Baptiste Lepilleur | |
1434 // Distributed under MIT license, or public domain if desired and | |
1435 // recognized in your jurisdiction. | |
1436 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE | |
1437 | |
1438 #ifndef CPPTL_JSON_READER_H_INCLUDED | |
1439 # define CPPTL_JSON_READER_H_INCLUDED | |
1440 | |
1441 #if !defined(JSON_IS_AMALGAMATION) | |
1442 # include "features.h" | |
1443 # include "value.h" | |
1444 #endif // if !defined(JSON_IS_AMALGAMATION) | |
1445 # include <deque> | |
1446 # include <stack> | |
1447 # include <string> | |
1448 # include <iostream> | |
1449 | |
1450 namespace Json { | |
1451 | |
1452 /** \brief Unserialize a <a HREF="http://www.json.org">JSON</a> document into a Value. | |
1453 * | |
1454 */ | |
1455 class JSON_API Reader | |
1456 { | |
1457 public: | |
1458 typedef char Char; | |
1459 typedef const Char *Location; | |
1460 | |
1461 /** \brief Constructs a Reader allowing all features | |
1462 * for parsing. | |
1463 */ | |
1464 Reader(); | |
1465 | |
1466 /** \brief Constructs a Reader allowing the specified feature set | |
1467 * for parsing. | |
1468 */ | |
1469 Reader( const Features &features ); | |
1470 | |
1471 /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> document. | |
1472 * \param document UTF-8 encoded string containing the document to read. | |
1473 * \param root [out] Contains the root value of the document if it was | |
1474 * successfully parsed. | |
1475 * \param collectComments \c true to collect comment and allow writing them back during | |
1476 * serialization, \c false to discard comments. | |
1477 * This parameter is ignored if Features::allowComments_ | |
1478 * is \c false. | |
1479 * \return \c true if the document was successfully parsed, \c false if an error occurred. | |
1480 */ | |
1481 bool parse( const std::string &document, | |
1482 Value &root, | |
1483 bool collectComments = true ); | |
1484 | |
1485 /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> document. | |
1486 * \param beginDoc Pointer on the beginning of the UTF-8 encoded string of the document to read. | |
1487 * \param endDoc Pointer on the end of the UTF-8 encoded string of the document to read. | |
1488 \ Must be >= beginDoc. | |
1489 * \param root [out] Contains the root value of the document if it was | |
1490 * successfully parsed. | |
1491 * \param collectComments \c true to collect comment and allow writing them back during | |
1492 * serialization, \c false to discard comments. | |
1493 * This parameter is ignored if Features::allowComments_ | |
1494 * is \c false. | |
1495 * \return \c true if the document was successfully parsed, \c false if an error occurred. | |
1496 */ | |
1497 bool parse( const char *beginDoc, const char *endDoc, | |
1498 Value &root, | |
1499 bool collectComments = true ); | |
1500 | |
1501 /// \brief Parse from input stream. | |
1502 /// \see Json::operator>>(std::istream&, Json::Value&). | |
1503 bool parse( std::istream &is, | |
1504 Value &root, | |
1505 bool collectComments = true ); | |
1506 | |
1507 /** \brief Returns a user friendly string that list errors in the parsed document. | |
1508 * \return Formatted error message with the list of errors with their location in | |
1509 * the parsed document. An empty string is returned if no error occurred | |
1510 * during parsing. | |
1511 * \deprecated Use getFormattedErrorMessages() instead (typo fix). | |
1512 */ | |
1513 JSONCPP_DEPRECATED("Use getFormattedErrorMessages instead") | |
1514 std::string getFormatedErrorMessages() const; | |
1515 | |
1516 /** \brief Returns a user friendly string that list errors in the parsed document. | |
1517 * \return Formatted error message with the list of errors with their location in | |
1518 * the parsed document. An empty string is returned if no error occurred | |
1519 * during parsing. | |
1520 */ | |
1521 std::string getFormattedErrorMessages() const; | |
1522 | |
1523 private: | |
1524 enum TokenType | |
1525 { | |
1526 tokenEndOfStream = 0, | |
1527 tokenObjectBegin, | |
1528 tokenObjectEnd, | |
1529 tokenArrayBegin, | |
1530 tokenArrayEnd, | |
1531 tokenString, | |
1532 tokenNumber, | |
1533 tokenTrue, | |
1534 tokenFalse, | |
1535 tokenNull, | |
1536 tokenArraySeparator, | |
1537 tokenMemberSeparator, | |
1538 tokenComment, | |
1539 tokenError | |
1540 }; | |
1541 | |
1542 class Token | |
1543 { | |
1544 public: | |
1545 TokenType type_; | |
1546 Location start_; | |
1547 Location end_; | |
1548 }; | |
1549 | |
1550 class ErrorInfo | |
1551 { | |
1552 public: | |
1553 Token token_; | |
1554 std::string message_; | |
1555 Location extra_; | |
1556 }; | |
1557 | |
1558 typedef std::deque<ErrorInfo> Errors; | |
1559 | |
1560 bool expectToken( TokenType type, Token &token, const char *message ); | |
1561 bool readToken( Token &token ); | |
1562 void skipSpaces(); | |
1563 bool match( Location pattern, | |
1564 int patternLength ); | |
1565 bool readComment(); | |
1566 bool readCStyleComment(); | |
1567 bool readCppStyleComment(); | |
1568 bool readString(); | |
1569 void readNumber(); | |
1570 bool readValue(); | |
1571 bool readObject( Token &token ); | |
1572 bool readArray( Token &token ); | |
1573 bool decodeNumber( Token &token ); | |
1574 bool decodeString( Token &token ); | |
1575 bool decodeString( Token &token, std::string &decoded ); | |
1576 bool decodeDouble( Token &token ); | |
1577 bool decodeUnicodeCodePoint( Token &token, | |
1578 Location ¤t, | |
1579 Location end, | |
1580 unsigned int &unicode ); | |
1581 bool decodeUnicodeEscapeSequence( Token &token, | |
1582 Location ¤t, | |
1583 Location end, | |
1584 unsigned int &unicode ); | |
1585 bool addError( const std::string &message, | |
1586 Token &token, | |
1587 Location extra = 0 ); | |
1588 bool recoverFromError( TokenType skipUntilToken ); | |
1589 bool addErrorAndRecover( const std::string &message, | |
1590 Token &token, | |
1591 TokenType skipUntilToken ); | |
1592 void skipUntilSpace(); | |
1593 Value ¤tValue(); | |
1594 Char getNextChar(); | |
1595 void getLocationLineAndColumn( Location location, | |
1596 int &line, | |
1597 int &column ) const; | |
1598 std::string getLocationLineAndColumn( Location location ) const; | |
1599 void addComment( Location begin, | |
1600 Location end, | |
1601 CommentPlacement placement ); | |
1602 void skipCommentTokens( Token &token ); | |
1603 | |
1604 typedef std::stack<Value *> Nodes; | |
1605 Nodes nodes_; | |
1606 Errors errors_; | |
1607 std::string document_; | |
1608 Location begin_; | |
1609 Location end_; | |
1610 Location current_; | |
1611 Location lastValueEnd_; | |
1612 Value *lastValue_; | |
1613 std::string commentsBefore_; | |
1614 Features features_; | |
1615 bool collectComments_; | |
1616 }; | |
1617 | |
1618 /** \brief Read from 'sin' into 'root'. | |
1619 | |
1620 Always keep comments from the input JSON. | |
1621 | |
1622 This can be used to read a file into a particular sub-object. | |
1623 For example: | |
1624 \code | |
1625 Json::Value root; | |
1626 cin >> root["dir"]["file"]; | |
1627 cout << root; | |
1628 \endcode | |
1629 Result: | |
1630 \verbatim | |
1631 { | |
1632 "dir": { | |
1633 "file": { | |
1634 // The input stream JSON would be nested here. | |
1635 } | |
1636 } | |
1637 } | |
1638 \endverbatim | |
1639 \throw std::exception on parse error. | |
1640 \see Json::operator<<() | |
1641 */ | |
1642 std::istream& operator>>( std::istream&, Value& ); | |
1643 | |
1644 } // namespace Json | |
1645 | |
1646 #endif // CPPTL_JSON_READER_H_INCLUDED | |
1647 | |
1648 // ////////////////////////////////////////////////////////////////////// | |
1649 // End of content of file: include/json/reader.h | |
1650 // ////////////////////////////////////////////////////////////////////// | |
1651 | |
1652 | |
1653 | |
1654 | |
1655 | |
1656 | |
1657 // ////////////////////////////////////////////////////////////////////// | |
1658 // Beginning of content of file: include/json/writer.h | |
1659 // ////////////////////////////////////////////////////////////////////// | |
1660 | |
1661 // Copyright 2007-2010 Baptiste Lepilleur | |
1662 // Distributed under MIT license, or public domain if desired and | |
1663 // recognized in your jurisdiction. | |
1664 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE | |
1665 | |
1666 #ifndef JSON_WRITER_H_INCLUDED | |
1667 # define JSON_WRITER_H_INCLUDED | |
1668 | |
1669 #if !defined(JSON_IS_AMALGAMATION) | |
1670 # include "value.h" | |
1671 #endif // if !defined(JSON_IS_AMALGAMATION) | |
1672 # include <vector> | |
1673 # include <string> | |
1674 # include <iostream> | |
1675 | |
1676 namespace Json { | |
1677 | |
1678 class Value; | |
1679 | |
1680 /** \brief Abstract class for writers. | |
1681 */ | |
1682 class JSON_API Writer | |
1683 { | |
1684 public: | |
1685 virtual ~Writer(); | |
1686 | |
1687 virtual std::string write( const Value &root ) = 0; | |
1688 }; | |
1689 | |
1690 /** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format without formatting (not human friendly). | |
1691 * | |
1692 * The JSON document is written in a single line. It is not intended for 'human' consumption, | |
1693 * but may be usefull to support feature such as RPC where bandwith is limited. | |
1694 * \sa Reader, Value | |
1695 */ | |
1696 class JSON_API FastWriter : public Writer | |
1697 { | |
1698 public: | |
1699 FastWriter(); | |
1700 virtual ~FastWriter(){} | |
1701 | |
1702 void enableYAMLCompatibility(); | |
1703 | |
1704 public: // overridden from Writer | |
1705 virtual std::string write( const Value &root ); | |
1706 | |
1707 private: | |
1708 void writeValue( const Value &value ); | |
1709 | |
1710 std::string document_; | |
1711 bool yamlCompatiblityEnabled_; | |
1712 }; | |
1713 | |
1714 /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way. | |
1715 * | |
1716 * The rules for line break and indent are as follow: | |
1717 * - Object value: | |
1718 * - if empty then print {} without indent and line break | |
1719 * - if not empty the print '{', line break & indent, print one value per line | |
1720 * and then unindent and line break and print '}'. | |
1721 * - Array value: | |
1722 * - if empty then print [] without indent and line break | |
1723 * - if the array contains no object value, empty array or some other value types, | |
1724 * and all the values fit on one lines, then print the array on a single line. | |
1725 * - otherwise, it the values do not fit on one line, or the array contains | |
1726 * object or non empty array, then print one value per line. | |
1727 * | |
1728 * If the Value have comments then they are outputed according to their #CommentPlacement. | |
1729 * | |
1730 * \sa Reader, Value, Value::setComment() | |
1731 */ | |
1732 class JSON_API StyledWriter: public Writer | |
1733 { | |
1734 public: | |
1735 StyledWriter(); | |
1736 virtual ~StyledWriter(){} | |
1737 | |
1738 public: // overridden from Writer | |
1739 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format. | |
1740 * \param root Value to serialize. | |
1741 * \return String containing the JSON document that represents the root value. | |
1742 */ | |
1743 virtual std::string write( const Value &root ); | |
1744 | |
1745 private: | |
1746 void writeValue( const Value &value ); | |
1747 void writeArrayValue( const Value &value ); | |
1748 bool isMultineArray( const Value &value ); | |
1749 void pushValue( const std::string &value ); | |
1750 void writeIndent(); | |
1751 void writeWithIndent( const std::string &value ); | |
1752 void indent(); | |
1753 void unindent(); | |
1754 void writeCommentBeforeValue( const Value &root ); | |
1755 void writeCommentAfterValueOnSameLine( const Value &root ); | |
1756 bool hasCommentForValue( const Value &value ); | |
1757 static std::string normalizeEOL( const std::string &text ); | |
1758 | |
1759 typedef std::vector<std::string> ChildValues; | |
1760 | |
1761 ChildValues childValues_; | |
1762 std::string document_; | |
1763 std::string indentString_; | |
1764 int rightMargin_; | |
1765 int indentSize_; | |
1766 bool addChildValues_; | |
1767 }; | |
1768 | |
1769 /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way, | |
1770 to a stream rather than to a string. | |
1771 * | |
1772 * The rules for line break and indent are as follow: | |
1773 * - Object value: | |
1774 * - if empty then print {} without indent and line break | |
1775 * - if not empty the print '{', line break & indent, print one value per line | |
1776 * and then unindent and line break and print '}'. | |
1777 * - Array value: | |
1778 * - if empty then print [] without indent and line break | |
1779 * - if the array contains no object value, empty array or some other value types, | |
1780 * and all the values fit on one lines, then print the array on a single line. | |
1781 * - otherwise, it the values do not fit on one line, or the array contains | |
1782 * object or non empty array, then print one value per line. | |
1783 * | |
1784 * If the Value have comments then they are outputed according to their #CommentPlacement. | |
1785 * | |
1786 * \param indentation Each level will be indented by this amount extra. | |
1787 * \sa Reader, Value, Value::setComment() | |
1788 */ | |
1789 class JSON_API StyledStreamWriter | |
1790 { | |
1791 public: | |
1792 StyledStreamWriter( std::string indentation="\t" ); | |
1793 ~StyledStreamWriter(){} | |
1794 | |
1795 public: | |
1796 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format. | |
1797 * \param out Stream to write to. (Can be ostringstream, e.g.) | |
1798 * \param root Value to serialize. | |
1799 * \note There is no point in deriving from Writer, since write() should not return a value. | |
1800 */ | |
1801 void write( std::ostream &out, const Value &root ); | |
1802 | |
1803 private: | |
1804 void writeValue( const Value &value ); | |
1805 void writeArrayValue( const Value &value ); | |
1806 bool isMultineArray( const Value &value ); | |
1807 void pushValue( const std::string &value ); | |
1808 void writeIndent(); | |
1809 void writeWithIndent( const std::string &value ); | |
1810 void indent(); | |
1811 void unindent(); | |
1812 void writeCommentBeforeValue( const Value &root ); | |
1813 void writeCommentAfterValueOnSameLine( const Value &root ); | |
1814 bool hasCommentForValue( const Value &value ); | |
1815 static std::string normalizeEOL( const std::string &text ); | |
1816 | |
1817 typedef std::vector<std::string> ChildValues; | |
1818 | |
1819 ChildValues childValues_; | |
1820 std::ostream* document_; | |
1821 std::string indentString_; | |
1822 int rightMargin_; | |
1823 std::string indentation_; | |
1824 bool addChildValues_; | |
1825 }; | |
1826 | |
1827 # if defined(JSON_HAS_INT64) | |
1828 std::string JSON_API valueToString( Int value ); | |
1829 std::string JSON_API valueToString( UInt value ); | |
1830 # endif // if defined(JSON_HAS_INT64) | |
1831 std::string JSON_API valueToString( LargestInt value ); | |
1832 std::string JSON_API valueToString( LargestUInt value ); | |
1833 std::string JSON_API valueToString( double value ); | |
1834 std::string JSON_API valueToString( bool value ); | |
1835 std::string JSON_API valueToQuotedString( const char *value ); | |
1836 | |
1837 /// \brief Output using the StyledStreamWriter. | |
1838 /// \see Json::operator>>() | |
1839 std::ostream& operator<<( std::ostream&, const Value &root ); | |
1840 | |
1841 } // namespace Json | |
1842 | |
1843 | |
1844 | |
1845 #endif // JSON_WRITER_H_INCLUDED | |
1846 | |
1847 // ////////////////////////////////////////////////////////////////////// | |
1848 // End of content of file: include/json/writer.h | |
1849 // ////////////////////////////////////////////////////////////////////// | |
1850 | |
1851 | |
1852 | |
1853 | |
1854 | |
1855 #endif //ifndef JSON_AMALGATED_H_INCLUDED |