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