诸暨麻将添加redis
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1720 lines
38 KiB

  1. # include "stdafx.h"
  2. #include <iostream>
  3. #include "value.h"
  4. #include "writer.h"
  5. #include <utility>
  6. #include <stdexcept>
  7. #include <cstring>
  8. #include <cassert>
  9. #ifdef JSON_USE_CPPTL
  10. # include <cpptl/conststring.h>
  11. #endif
  12. #include <cstddef> // size_t
  13. #ifndef JSON_USE_SIMPLE_INTERNAL_ALLOCATOR
  14. # include "json_batchallocator.h"
  15. #endif // #ifndef JSON_USE_SIMPLE_INTERNAL_ALLOCATOR
  16. #define JSON_ASSERT_UNREACHABLE assert( false )
  17. #define JSON_ASSERT( condition ) assert( condition ); // @todo <= change this into an exception throw
  18. #define JSON_ASSERT_MESSAGE( condition, message ) if (!( condition )) throw std::runtime_error( message );
  19. namespace Json {
  20. const Value Value::null;
  21. const Int Value::minInt = Int( ~(UInt(-1)/2) );
  22. const Int Value::maxInt = Int( UInt(-1)/2 );
  23. const UInt Value::maxUInt = UInt(-1);
  24. // A "safe" implementation of strdup. Allow null pointer to be passed.
  25. // Also avoid warning on msvc80.
  26. //
  27. //inline char *safeStringDup( const char *czstring )
  28. //{
  29. // if ( czstring )
  30. // {
  31. // const size_t length = (unsigned int)( strlen(czstring) + 1 );
  32. // char *newString = static_cast<char *>( malloc( length ) );
  33. // memcpy( newString, czstring, length );
  34. // return newString;
  35. // }
  36. // return 0;
  37. //}
  38. //
  39. //inline char *safeStringDup( const std::string &str )
  40. //{
  41. // if ( !str.empty() )
  42. // {
  43. // const size_t length = str.length();
  44. // char *newString = static_cast<char *>( malloc( length + 1 ) );
  45. // memcpy( newString, str.c_str(), length );
  46. // newString[length] = 0;
  47. // return newString;
  48. // }
  49. // return 0;
  50. //}
  51. ValueAllocator::~ValueAllocator()
  52. {
  53. }
  54. class DefaultValueAllocator : public ValueAllocator
  55. {
  56. public:
  57. virtual ~DefaultValueAllocator()
  58. {
  59. }
  60. virtual char *makeMemberName( const char *memberName )
  61. {
  62. return duplicateStringValue( memberName );
  63. }
  64. virtual void releaseMemberName( char *memberName )
  65. {
  66. releaseStringValue( memberName );
  67. }
  68. virtual char *duplicateStringValue( const char *value,
  69. unsigned int length = unknown )
  70. {
  71. //@todo invesgate this old optimization
  72. //if ( !value || value[0] == 0 )
  73. // return 0;
  74. if ( length == unknown )
  75. length = (unsigned int)strlen(value);
  76. char *newString = static_cast<char *>( malloc( length + 1 ) );
  77. memcpy( newString, value, length );
  78. newString[length] = 0;
  79. return newString;
  80. }
  81. virtual void releaseStringValue( char *value )
  82. {
  83. if ( value )
  84. free( value );
  85. }
  86. };
  87. static ValueAllocator *&valueAllocator()
  88. {
  89. static DefaultValueAllocator defaultAllocator;
  90. static ValueAllocator *valueAllocator = &defaultAllocator;
  91. return valueAllocator;
  92. }
  93. static struct DummyValueAllocatorInitializer {
  94. DummyValueAllocatorInitializer()
  95. {
  96. valueAllocator(); // ensure valueAllocator() statics are initialized before main().
  97. }
  98. } dummyValueAllocatorInitializer;
  99. // //////////////////////////////////////////////////////////////////
  100. // //////////////////////////////////////////////////////////////////
  101. // //////////////////////////////////////////////////////////////////
  102. // ValueInternals...
  103. // //////////////////////////////////////////////////////////////////
  104. // //////////////////////////////////////////////////////////////////
  105. // //////////////////////////////////////////////////////////////////
  106. #ifdef JSON_VALUE_USE_INTERNAL_MAP
  107. # include "json_internalarray.inl"
  108. # include "json_internalmap.inl"
  109. #endif // JSON_VALUE_USE_INTERNAL_MAP
  110. # include "json_valueiterator.inl"
  111. // //////////////////////////////////////////////////////////////////
  112. // //////////////////////////////////////////////////////////////////
  113. // //////////////////////////////////////////////////////////////////
  114. // class Value::CommentInfo
  115. // //////////////////////////////////////////////////////////////////
  116. // //////////////////////////////////////////////////////////////////
  117. // //////////////////////////////////////////////////////////////////
  118. Value::CommentInfo::CommentInfo()
  119. : comment_( 0 )
  120. {
  121. }
  122. Value::CommentInfo::~CommentInfo()
  123. {
  124. if ( comment_ )
  125. valueAllocator()->releaseStringValue( comment_ );
  126. }
  127. void
  128. Value::CommentInfo::setComment( const char *text )
  129. {
  130. if ( comment_ )
  131. valueAllocator()->releaseStringValue( comment_ );
  132. JSON_ASSERT( text );
  133. JSON_ASSERT_MESSAGE( text[0]=='\0' || text[0]=='/', "Comments must start with /");
  134. // It seems that /**/ style comments are acceptable as well.
  135. comment_ = valueAllocator()->duplicateStringValue( text );
  136. }
  137. // //////////////////////////////////////////////////////////////////
  138. // //////////////////////////////////////////////////////////////////
  139. // //////////////////////////////////////////////////////////////////
  140. // class Value::CZString
  141. // //////////////////////////////////////////////////////////////////
  142. // //////////////////////////////////////////////////////////////////
  143. // //////////////////////////////////////////////////////////////////
  144. # ifndef JSON_VALUE_USE_INTERNAL_MAP
  145. // Notes: index_ indicates if the string was allocated when
  146. // a string is stored.
  147. Value::CZString::CZString( int index )
  148. : cstr_( 0 )
  149. , index_( index )
  150. {
  151. }
  152. Value::CZString::CZString( const char *cstr, DuplicationPolicy allocate )
  153. : cstr_( allocate == duplicate ? valueAllocator()->makeMemberName(cstr)
  154. : cstr )
  155. , index_( allocate )
  156. {
  157. }
  158. Value::CZString::CZString( const CZString &other )
  159. : cstr_( other.index_ != noDuplication && other.cstr_ != 0
  160. ? valueAllocator()->makeMemberName( other.cstr_ )
  161. : other.cstr_ )
  162. , index_( other.cstr_ ? (other.index_ == noDuplication ? noDuplication : duplicate)
  163. : other.index_ )
  164. {
  165. }
  166. Value::CZString::~CZString()
  167. {
  168. if ( cstr_ && index_ == duplicate )
  169. valueAllocator()->releaseMemberName( const_cast<char *>( cstr_ ) );
  170. }
  171. void
  172. Value::CZString::swap( CZString &other )
  173. {
  174. std::swap( cstr_, other.cstr_ );
  175. std::swap( index_, other.index_ );
  176. }
  177. Value::CZString &
  178. Value::CZString::operator =( const CZString &other )
  179. {
  180. CZString temp( other );
  181. swap( temp );
  182. return *this;
  183. }
  184. bool
  185. Value::CZString::operator<( const CZString &other ) const
  186. {
  187. if ( cstr_ )
  188. return strcmp( cstr_, other.cstr_ ) < 0;
  189. return index_ < other.index_;
  190. }
  191. bool
  192. Value::CZString::operator==( const CZString &other ) const
  193. {
  194. if ( cstr_ )
  195. return strcmp( cstr_, other.cstr_ ) == 0;
  196. return index_ == other.index_;
  197. }
  198. int
  199. Value::CZString::index() const
  200. {
  201. return index_;
  202. }
  203. const char *
  204. Value::CZString::c_str() const
  205. {
  206. return cstr_;
  207. }
  208. bool
  209. Value::CZString::isStaticString() const
  210. {
  211. return index_ == noDuplication;
  212. }
  213. #endif // ifndef JSON_VALUE_USE_INTERNAL_MAP
  214. // //////////////////////////////////////////////////////////////////
  215. // //////////////////////////////////////////////////////////////////
  216. // //////////////////////////////////////////////////////////////////
  217. // class Value::Value
  218. // //////////////////////////////////////////////////////////////////
  219. // //////////////////////////////////////////////////////////////////
  220. // //////////////////////////////////////////////////////////////////
  221. /*! \internal Default constructor initialization must be equivalent to:
  222. * memset( this, 0, sizeof(Value) )
  223. * This optimization is used in ValueInternalMap fast allocator.
  224. */
  225. Value::Value( ValueType type )
  226. : type_( type )
  227. , allocated_( 0 )
  228. , comments_( 0 )
  229. # ifdef JSON_VALUE_USE_INTERNAL_MAP
  230. , itemIsUsed_( 0 )
  231. #endif
  232. {
  233. switch ( type )
  234. {
  235. case nullValue:
  236. break;
  237. case intValue:
  238. case uintValue:
  239. value_.int_ = 0;
  240. break;
  241. case realValue:
  242. value_.real_ = 0.0;
  243. break;
  244. case stringValue:
  245. value_.string_ = 0;
  246. break;
  247. #ifndef JSON_VALUE_USE_INTERNAL_MAP
  248. case arrayValue:
  249. case objectValue:
  250. value_.map_ = new ObjectValues();
  251. break;
  252. #else
  253. case arrayValue:
  254. value_.array_ = arrayAllocator()->newArray();
  255. break;
  256. case objectValue:
  257. value_.map_ = mapAllocator()->newMap();
  258. break;
  259. #endif
  260. case booleanValue:
  261. value_.bool_ = false;
  262. break;
  263. default:
  264. JSON_ASSERT_UNREACHABLE;
  265. }
  266. }
  267. Value::Value( Int value )
  268. : type_( intValue )
  269. , comments_( 0 )
  270. # ifdef JSON_VALUE_USE_INTERNAL_MAP
  271. , itemIsUsed_( 0 )
  272. #endif
  273. {
  274. value_.int_ = value;
  275. }
  276. Value::Value( UInt value )
  277. : type_( uintValue )
  278. , comments_( 0 )
  279. # ifdef JSON_VALUE_USE_INTERNAL_MAP
  280. , itemIsUsed_( 0 )
  281. #endif
  282. {
  283. value_.uint_ = value;
  284. }
  285. Value::Value( double value )
  286. : type_( realValue )
  287. , comments_( 0 )
  288. # ifdef JSON_VALUE_USE_INTERNAL_MAP
  289. , itemIsUsed_( 0 )
  290. #endif
  291. {
  292. value_.real_ = value;
  293. }
  294. Value::Value( const char *value )
  295. : type_( stringValue )
  296. , allocated_( true )
  297. , comments_( 0 )
  298. # ifdef JSON_VALUE_USE_INTERNAL_MAP
  299. , itemIsUsed_( 0 )
  300. #endif
  301. {
  302. value_.string_ = valueAllocator()->duplicateStringValue( value );
  303. }
  304. Value::Value( const char *beginValue,
  305. const char *endValue )
  306. : type_( stringValue )
  307. , allocated_( true )
  308. , comments_( 0 )
  309. # ifdef JSON_VALUE_USE_INTERNAL_MAP
  310. , itemIsUsed_( 0 )
  311. #endif
  312. {
  313. value_.string_ = valueAllocator()->duplicateStringValue( beginValue,
  314. UInt(endValue - beginValue) );
  315. }
  316. Value::Value( const std::string &value )
  317. : type_( stringValue )
  318. , allocated_( true )
  319. , comments_( 0 )
  320. # ifdef JSON_VALUE_USE_INTERNAL_MAP
  321. , itemIsUsed_( 0 )
  322. #endif
  323. {
  324. value_.string_ = valueAllocator()->duplicateStringValue( value.c_str(),
  325. (unsigned int)value.length() );
  326. }
  327. Value::Value( const StaticString &value )
  328. : type_( stringValue )
  329. , allocated_( false )
  330. , comments_( 0 )
  331. # ifdef JSON_VALUE_USE_INTERNAL_MAP
  332. , itemIsUsed_( 0 )
  333. #endif
  334. {
  335. value_.string_ = const_cast<char *>( value.c_str() );
  336. }
  337. # ifdef JSON_USE_CPPTL
  338. Value::Value( const CppTL::ConstString &value )
  339. : type_( stringValue )
  340. , allocated_( true )
  341. , comments_( 0 )
  342. # ifdef JSON_VALUE_USE_INTERNAL_MAP
  343. , itemIsUsed_( 0 )
  344. #endif
  345. {
  346. value_.string_ = valueAllocator()->duplicateStringValue( value, value.length() );
  347. }
  348. # endif
  349. Value::Value( bool value )
  350. : type_( booleanValue )
  351. , comments_( 0 )
  352. # ifdef JSON_VALUE_USE_INTERNAL_MAP
  353. , itemIsUsed_( 0 )
  354. #endif
  355. {
  356. value_.bool_ = value;
  357. }
  358. Value::Value( const Value &other )
  359. : type_( other.type_ )
  360. , comments_( 0 )
  361. # ifdef JSON_VALUE_USE_INTERNAL_MAP
  362. , itemIsUsed_( 0 )
  363. #endif
  364. {
  365. switch ( type_ )
  366. {
  367. case nullValue:
  368. case intValue:
  369. case uintValue:
  370. case realValue:
  371. case booleanValue:
  372. value_ = other.value_;
  373. break;
  374. case stringValue:
  375. if ( other.value_.string_ )
  376. {
  377. value_.string_ = valueAllocator()->duplicateStringValue( other.value_.string_ );
  378. allocated_ = true;
  379. }
  380. else
  381. value_.string_ = 0;
  382. break;
  383. #ifndef JSON_VALUE_USE_INTERNAL_MAP
  384. case arrayValue:
  385. case objectValue:
  386. value_.map_ = new ObjectValues( *other.value_.map_ );
  387. break;
  388. #else
  389. case arrayValue:
  390. value_.array_ = arrayAllocator()->newArrayCopy( *other.value_.array_ );
  391. break;
  392. case objectValue:
  393. value_.map_ = mapAllocator()->newMapCopy( *other.value_.map_ );
  394. break;
  395. #endif
  396. default:
  397. JSON_ASSERT_UNREACHABLE;
  398. }
  399. if ( other.comments_ )
  400. {
  401. comments_ = new CommentInfo[numberOfCommentPlacement];
  402. for ( int comment =0; comment < numberOfCommentPlacement; ++comment )
  403. {
  404. const CommentInfo &otherComment = other.comments_[comment];
  405. if ( otherComment.comment_ )
  406. comments_[comment].setComment( otherComment.comment_ );
  407. }
  408. }
  409. }
  410. Value::~Value()
  411. {
  412. switch ( type_ )
  413. {
  414. case nullValue:
  415. case intValue:
  416. case uintValue:
  417. case realValue:
  418. case booleanValue:
  419. break;
  420. case stringValue:
  421. if ( allocated_ )
  422. valueAllocator()->releaseStringValue( value_.string_ );
  423. break;
  424. #ifndef JSON_VALUE_USE_INTERNAL_MAP
  425. case arrayValue:
  426. case objectValue:
  427. delete value_.map_;
  428. break;
  429. #else
  430. case arrayValue:
  431. arrayAllocator()->destructArray( value_.array_ );
  432. break;
  433. case objectValue:
  434. mapAllocator()->destructMap( value_.map_ );
  435. break;
  436. #endif
  437. default:
  438. JSON_ASSERT_UNREACHABLE;
  439. }
  440. if ( comments_ )
  441. delete[] comments_;
  442. }
  443. Value &
  444. Value::operator=( const Value &other )
  445. {
  446. Value temp( other );
  447. swap( temp );
  448. return *this;
  449. }
  450. void
  451. Value::swap( Value &other )
  452. {
  453. ValueType temp = type_;
  454. type_ = other.type_;
  455. other.type_ = temp;
  456. std::swap( value_, other.value_ );
  457. int temp2 = allocated_;
  458. allocated_ = other.allocated_;
  459. other.allocated_ = temp2;
  460. }
  461. ValueType
  462. Value::type() const
  463. {
  464. return type_;
  465. }
  466. int
  467. Value::compare( const Value &other )
  468. {
  469. /*
  470. int typeDelta = other.type_ - type_;
  471. switch ( type_ )
  472. {
  473. case nullValue:
  474. return other.type_ == type_;
  475. case intValue:
  476. if ( other.type_.isNumeric()
  477. case uintValue:
  478. case realValue:
  479. case booleanValue:
  480. break;
  481. case stringValue,
  482. break;
  483. case arrayValue:
  484. delete value_.array_;
  485. break;
  486. case objectValue:
  487. delete value_.map_;
  488. default:
  489. JSON_ASSERT_UNREACHABLE;
  490. }
  491. */
  492. return 0; // unreachable
  493. }
  494. bool
  495. Value::operator <( const Value &other ) const
  496. {
  497. int typeDelta = type_ - other.type_;
  498. if ( typeDelta )
  499. return typeDelta < 0 ? true : false;
  500. switch ( type_ )
  501. {
  502. case nullValue:
  503. return false;
  504. case intValue:
  505. return value_.int_ < other.value_.int_;
  506. case uintValue:
  507. return value_.uint_ < other.value_.uint_;
  508. case realValue:
  509. return value_.real_ < other.value_.real_;
  510. case booleanValue:
  511. return value_.bool_ < other.value_.bool_;
  512. case stringValue:
  513. return ( value_.string_ == 0 && other.value_.string_ )
  514. || ( other.value_.string_
  515. && value_.string_
  516. && strcmp( value_.string_, other.value_.string_ ) < 0 );
  517. #ifndef JSON_VALUE_USE_INTERNAL_MAP
  518. case arrayValue:
  519. case objectValue:
  520. {
  521. int delta = int( value_.map_->size() - other.value_.map_->size() );
  522. if ( delta )
  523. return delta < 0;
  524. return (*value_.map_) < (*other.value_.map_);
  525. }
  526. #else
  527. case arrayValue:
  528. return value_.array_->compare( *(other.value_.array_) ) < 0;
  529. case objectValue:
  530. return value_.map_->compare( *(other.value_.map_) ) < 0;
  531. #endif
  532. default:
  533. JSON_ASSERT_UNREACHABLE;
  534. }
  535. return 0; // unreachable
  536. }
  537. bool
  538. Value::operator <=( const Value &other ) const
  539. {
  540. return !(other > *this);
  541. }
  542. bool
  543. Value::operator >=( const Value &other ) const
  544. {
  545. return !(*this < other);
  546. }
  547. bool
  548. Value::operator >( const Value &other ) const
  549. {
  550. return other < *this;
  551. }
  552. bool
  553. Value::operator ==( const Value &other ) const
  554. {
  555. //if ( type_ != other.type_ )
  556. // GCC 2.95.3 says:
  557. // attempt to take address of bit-field structure member `Json::Value::type_'
  558. // Beats me, but a temp solves the problem.
  559. int temp = other.type_;
  560. if ( type_ != temp )
  561. return false;
  562. switch ( type_ )
  563. {
  564. case nullValue:
  565. return true;
  566. case intValue:
  567. return value_.int_ == other.value_.int_;
  568. case uintValue:
  569. return value_.uint_ == other.value_.uint_;
  570. case realValue:
  571. return value_.real_ == other.value_.real_;
  572. case booleanValue:
  573. return value_.bool_ == other.value_.bool_;
  574. case stringValue:
  575. return ( value_.string_ == other.value_.string_ )
  576. || ( other.value_.string_
  577. && value_.string_
  578. && strcmp( value_.string_, other.value_.string_ ) == 0 );
  579. #ifndef JSON_VALUE_USE_INTERNAL_MAP
  580. case arrayValue:
  581. case objectValue:
  582. return value_.map_->size() == other.value_.map_->size()
  583. && (*value_.map_) == (*other.value_.map_);
  584. #else
  585. case arrayValue:
  586. return value_.array_->compare( *(other.value_.array_) ) == 0;
  587. case objectValue:
  588. return value_.map_->compare( *(other.value_.map_) ) == 0;
  589. #endif
  590. default:
  591. JSON_ASSERT_UNREACHABLE;
  592. }
  593. return 0; // unreachable
  594. }
  595. bool
  596. Value::operator !=( const Value &other ) const
  597. {
  598. return !( *this == other );
  599. }
  600. const char *
  601. Value::asCString() const
  602. {
  603. JSON_ASSERT( type_ == stringValue );
  604. return value_.string_;
  605. }
  606. std::string
  607. Value::asString() const
  608. {
  609. switch ( type_ )
  610. {
  611. case nullValue:
  612. return "";
  613. case stringValue:
  614. return value_.string_ ? value_.string_ : "";
  615. case booleanValue:
  616. return value_.bool_ ? "true" : "false";
  617. case intValue:
  618. case uintValue:
  619. case realValue:
  620. case arrayValue:
  621. case objectValue:
  622. JSON_ASSERT_MESSAGE( false, "Type is not convertible to string" );
  623. default:
  624. JSON_ASSERT_UNREACHABLE;
  625. }
  626. return ""; // unreachable
  627. }
  628. # ifdef JSON_USE_CPPTL
  629. CppTL::ConstString
  630. Value::asConstString() const
  631. {
  632. return CppTL::ConstString( asString().c_str() );
  633. }
  634. # endif
  635. Value::Int
  636. Value::asInt() const
  637. {
  638. switch ( type_ )
  639. {
  640. case nullValue:
  641. return 0;
  642. case intValue:
  643. return value_.int_;
  644. case uintValue:
  645. JSON_ASSERT_MESSAGE( value_.uint_ < (unsigned)maxInt, "integer out of signed integer range" );
  646. return value_.uint_;
  647. case realValue:
  648. JSON_ASSERT_MESSAGE( value_.real_ >= minInt && value_.real_ <= maxInt, "Real out of signed integer range" );
  649. return Int( value_.real_ );
  650. case booleanValue:
  651. return value_.bool_ ? 1 : 0;
  652. case stringValue:
  653. case arrayValue:
  654. case objectValue:
  655. JSON_ASSERT_MESSAGE( false, "Type is not convertible to int" );
  656. default:
  657. JSON_ASSERT_UNREACHABLE;
  658. }
  659. return 0; // unreachable;
  660. }
  661. Value::UInt
  662. Value::asUInt() const
  663. {
  664. switch ( type_ )
  665. {
  666. case nullValue:
  667. return 0;
  668. case intValue:
  669. JSON_ASSERT_MESSAGE( value_.int_ >= 0, "Negative integer can not be converted to unsigned integer" );
  670. return value_.int_;
  671. case uintValue:
  672. return value_.uint_;
  673. case realValue:
  674. JSON_ASSERT_MESSAGE( value_.real_ >= 0 && value_.real_ <= maxUInt, "Real out of unsigned integer range" );
  675. return UInt( value_.real_ );
  676. case booleanValue:
  677. return value_.bool_ ? 1 : 0;
  678. case stringValue:
  679. case arrayValue:
  680. case objectValue:
  681. JSON_ASSERT_MESSAGE( false, "Type is not convertible to uint" );
  682. default:
  683. JSON_ASSERT_UNREACHABLE;
  684. }
  685. return 0; // unreachable;
  686. }
  687. double
  688. Value::asDouble() const
  689. {
  690. switch ( type_ )
  691. {
  692. case nullValue:
  693. return 0.0;
  694. case intValue:
  695. return value_.int_;
  696. case uintValue:
  697. return value_.uint_;
  698. case realValue:
  699. return value_.real_;
  700. case booleanValue:
  701. return value_.bool_ ? 1.0 : 0.0;
  702. case stringValue:
  703. case arrayValue:
  704. case objectValue:
  705. JSON_ASSERT_MESSAGE( false, "Type is not convertible to double" );
  706. default:
  707. JSON_ASSERT_UNREACHABLE;
  708. }
  709. return 0; // unreachable;
  710. }
  711. bool
  712. Value::asBool() const
  713. {
  714. switch ( type_ )
  715. {
  716. case nullValue:
  717. return false;
  718. case intValue:
  719. case uintValue:
  720. return value_.int_ != 0;
  721. case realValue:
  722. return value_.real_ != 0.0;
  723. case booleanValue:
  724. return value_.bool_;
  725. case stringValue:
  726. return value_.string_ && value_.string_[0] != 0;
  727. case arrayValue:
  728. case objectValue:
  729. return value_.map_->size() != 0;
  730. default:
  731. JSON_ASSERT_UNREACHABLE;
  732. }
  733. return false; // unreachable;
  734. }
  735. bool
  736. Value::isConvertibleTo( ValueType other ) const
  737. {
  738. switch ( type_ )
  739. {
  740. case nullValue:
  741. return true;
  742. case intValue:
  743. return ( other == nullValue && value_.int_ == 0 )
  744. || other == intValue
  745. || ( other == uintValue && value_.int_ >= 0 )
  746. || other == realValue
  747. || other == stringValue
  748. || other == booleanValue;
  749. case uintValue:
  750. return ( other == nullValue && value_.uint_ == 0 )
  751. || ( other == intValue && value_.uint_ <= (unsigned)maxInt )
  752. || other == uintValue
  753. || other == realValue
  754. || other == stringValue
  755. || other == booleanValue;
  756. case realValue:
  757. return ( other == nullValue && value_.real_ == 0.0 )
  758. || ( other == intValue && value_.real_ >= minInt && value_.real_ <= maxInt )
  759. || ( other == uintValue && value_.real_ >= 0 && value_.real_ <= maxUInt )
  760. || other == realValue
  761. || other == stringValue
  762. || other == booleanValue;
  763. case booleanValue:
  764. return ( other == nullValue && value_.bool_ == false )
  765. || other == intValue
  766. || other == uintValue
  767. || other == realValue
  768. || other == stringValue
  769. || other == booleanValue;
  770. case stringValue:
  771. return other == stringValue
  772. || ( other == nullValue && (!value_.string_ || value_.string_[0] == 0) );
  773. case arrayValue:
  774. return other == arrayValue
  775. || ( other == nullValue && value_.map_->size() == 0 );
  776. case objectValue:
  777. return other == objectValue
  778. || ( other == nullValue && value_.map_->size() == 0 );
  779. default:
  780. JSON_ASSERT_UNREACHABLE;
  781. }
  782. return false; // unreachable;
  783. }
  784. /// Number of values in array or object
  785. Value::UInt
  786. Value::size() const
  787. {
  788. switch ( type_ )
  789. {
  790. case nullValue:
  791. case intValue:
  792. case uintValue:
  793. case realValue:
  794. case booleanValue:
  795. case stringValue:
  796. return 0;
  797. #ifndef JSON_VALUE_USE_INTERNAL_MAP
  798. case arrayValue: // size of the array is highest index + 1
  799. if ( !value_.map_->empty() )
  800. {
  801. ObjectValues::const_iterator itLast = value_.map_->end();
  802. --itLast;
  803. return (*itLast).first.index()+1;
  804. }
  805. return 0;
  806. case objectValue:
  807. return Int( value_.map_->size() );
  808. #else
  809. case arrayValue:
  810. return Int( value_.array_->size() );
  811. case objectValue:
  812. return Int( value_.map_->size() );
  813. #endif
  814. default:
  815. JSON_ASSERT_UNREACHABLE;
  816. }
  817. return 0; // unreachable;
  818. }
  819. bool
  820. Value::empty() const
  821. {
  822. if ( isNull() || isArray() || isObject() )
  823. return size() == 0u;
  824. else
  825. return false;
  826. }
  827. bool
  828. Value::operator!() const
  829. {
  830. return isNull();
  831. }
  832. void
  833. Value::clear()
  834. {
  835. JSON_ASSERT( type_ == nullValue || type_ == arrayValue || type_ == objectValue );
  836. switch ( type_ )
  837. {
  838. #ifndef JSON_VALUE_USE_INTERNAL_MAP
  839. case arrayValue:
  840. case objectValue:
  841. value_.map_->clear();
  842. break;
  843. #else
  844. case arrayValue:
  845. value_.array_->clear();
  846. break;
  847. case objectValue:
  848. value_.map_->clear();
  849. break;
  850. #endif
  851. default:
  852. break;
  853. }
  854. }
  855. void
  856. Value::resize( UInt newSize )
  857. {
  858. JSON_ASSERT( type_ == nullValue || type_ == arrayValue );
  859. if ( type_ == nullValue )
  860. *this = Value( arrayValue );
  861. #ifndef JSON_VALUE_USE_INTERNAL_MAP
  862. UInt oldSize = size();
  863. if ( newSize == 0 )
  864. clear();
  865. else if ( newSize > oldSize )
  866. (*this)[ newSize - 1 ];
  867. else
  868. {
  869. for ( UInt index = newSize; index < oldSize; ++index )
  870. value_.map_->erase( index );
  871. assert( size() == newSize );
  872. }
  873. #else
  874. value_.array_->resize( newSize );
  875. #endif
  876. }
  877. Value &
  878. Value::operator[]( UInt index )
  879. {
  880. JSON_ASSERT( type_ == nullValue || type_ == arrayValue );
  881. if ( type_ == nullValue )
  882. *this = Value( arrayValue );
  883. #ifndef JSON_VALUE_USE_INTERNAL_MAP
  884. CZString key( index );
  885. ObjectValues::iterator it = value_.map_->lower_bound( key );
  886. if ( it != value_.map_->end() && (*it).first == key )
  887. return (*it).second;
  888. ObjectValues::value_type defaultValue( key, null );
  889. it = value_.map_->insert( it, defaultValue );
  890. return (*it).second;
  891. #else
  892. return value_.array_->resolveReference( index );
  893. #endif
  894. }
  895. const Value &
  896. Value::operator[]( UInt index ) const
  897. {
  898. JSON_ASSERT( type_ == nullValue || type_ == arrayValue );
  899. if ( type_ == nullValue )
  900. return null;
  901. #ifndef JSON_VALUE_USE_INTERNAL_MAP
  902. CZString key( index );
  903. ObjectValues::const_iterator it = value_.map_->find( key );
  904. if ( it == value_.map_->end() )
  905. return null;
  906. return (*it).second;
  907. #else
  908. Value *value = value_.array_->find( index );
  909. return value ? *value : null;
  910. #endif
  911. }
  912. Value &
  913. Value::operator[]( const char *key )
  914. {
  915. return resolveReference( key, false );
  916. }
  917. Value &
  918. Value::resolveReference( const char *key,
  919. bool isStatic )
  920. {
  921. JSON_ASSERT( type_ == nullValue || type_ == objectValue );
  922. if ( type_ == nullValue )
  923. *this = Value( objectValue );
  924. #ifndef JSON_VALUE_USE_INTERNAL_MAP
  925. CZString actualKey( key, isStatic ? CZString::noDuplication
  926. : CZString::duplicateOnCopy );
  927. ObjectValues::iterator it = value_.map_->lower_bound( actualKey );
  928. if ( it != value_.map_->end() && (*it).first == actualKey )
  929. return (*it).second;
  930. ObjectValues::value_type defaultValue( actualKey, null );
  931. it = value_.map_->insert( it, defaultValue );
  932. Value &value = (*it).second;
  933. return value;
  934. #else
  935. return value_.map_->resolveReference( key, isStatic );
  936. #endif
  937. }
  938. Value
  939. Value::get( UInt index,
  940. const Value &defaultValue ) const
  941. {
  942. const Value *value = &((*this)[index]);
  943. return value == &null ? defaultValue : *value;
  944. }
  945. bool
  946. Value::isValidIndex( UInt index ) const
  947. {
  948. return index < size();
  949. }
  950. const Value &
  951. Value::operator[]( const char *key ) const
  952. {
  953. JSON_ASSERT( type_ == nullValue || type_ == objectValue );
  954. if ( type_ == nullValue )
  955. return null;
  956. #ifndef JSON_VALUE_USE_INTERNAL_MAP
  957. CZString actualKey( key, CZString::noDuplication );
  958. ObjectValues::const_iterator it = value_.map_->find( actualKey );
  959. if ( it == value_.map_->end() )
  960. return null;
  961. return (*it).second;
  962. #else
  963. const Value *value = value_.map_->find( key );
  964. return value ? *value : null;
  965. #endif
  966. }
  967. Value &
  968. Value::operator[]( const std::string &key )
  969. {
  970. return (*this)[ key.c_str() ];
  971. }
  972. const Value &
  973. Value::operator[]( const std::string &key ) const
  974. {
  975. return (*this)[ key.c_str() ];
  976. }
  977. Value &
  978. Value::operator[]( const StaticString &key )
  979. {
  980. return resolveReference( key, true );
  981. }
  982. # ifdef JSON_USE_CPPTL
  983. Value &
  984. Value::operator[]( const CppTL::ConstString &key )
  985. {
  986. return (*this)[ key.c_str() ];
  987. }
  988. const Value &
  989. Value::operator[]( const CppTL::ConstString &key ) const
  990. {
  991. return (*this)[ key.c_str() ];
  992. }
  993. # endif
  994. Value &
  995. Value::append( const Value &value )
  996. {
  997. return (*this)[size()] = value;
  998. }
  999. Value
  1000. Value::get( const char *key,
  1001. const Value &defaultValue ) const
  1002. {
  1003. const Value *value = &((*this)[key]);
  1004. return value == &null ? defaultValue : *value;
  1005. }
  1006. Value
  1007. Value::get( const std::string &key,
  1008. const Value &defaultValue ) const
  1009. {
  1010. return get( key.c_str(), defaultValue );
  1011. }
  1012. Value
  1013. Value::removeMember( const char* key )
  1014. {
  1015. JSON_ASSERT( type_ == nullValue || type_ == objectValue );
  1016. if ( type_ == nullValue )
  1017. return null;
  1018. #ifndef JSON_VALUE_USE_INTERNAL_MAP
  1019. CZString actualKey( key, CZString::noDuplication );
  1020. ObjectValues::iterator it = value_.map_->find( actualKey );
  1021. if ( it == value_.map_->end() )
  1022. return null;
  1023. Value old(it->second);
  1024. value_.map_->erase(it);
  1025. return old;
  1026. #else
  1027. Value *value = value_.map_->find( key );
  1028. if (value){
  1029. Value old(*value);
  1030. value_.map_.remove( key );
  1031. return old;
  1032. } else {
  1033. return null;
  1034. }
  1035. #endif
  1036. }
  1037. Value
  1038. Value::removeMember( const std::string &key )
  1039. {
  1040. return removeMember( key.c_str() );
  1041. }
  1042. # ifdef JSON_USE_CPPTL
  1043. Value
  1044. Value::get( const CppTL::ConstString &key,
  1045. const Value &defaultValue ) const
  1046. {
  1047. return get( key.c_str(), defaultValue );
  1048. }
  1049. # endif
  1050. bool
  1051. Value::isMember( const char *key ) const
  1052. {
  1053. const Value *value = &((*this)[key]);
  1054. return value != &null;
  1055. }
  1056. bool
  1057. Value::isMember( const std::string &key ) const
  1058. {
  1059. return isMember( key.c_str() );
  1060. }
  1061. # ifdef JSON_USE_CPPTL
  1062. bool
  1063. Value::isMember( const CppTL::ConstString &key ) const
  1064. {
  1065. return isMember( key.c_str() );
  1066. }
  1067. #endif
  1068. Value::Members
  1069. Value::getMemberNames() const
  1070. {
  1071. JSON_ASSERT( type_ == nullValue || type_ == objectValue );
  1072. if ( type_ == nullValue )
  1073. return Value::Members();
  1074. Members members;
  1075. members.reserve( value_.map_->size() );
  1076. #ifndef JSON_VALUE_USE_INTERNAL_MAP
  1077. ObjectValues::const_iterator it = value_.map_->begin();
  1078. ObjectValues::const_iterator itEnd = value_.map_->end();
  1079. for ( ; it != itEnd; ++it )
  1080. members.push_back( std::string( (*it).first.c_str() ) );
  1081. #else
  1082. ValueInternalMap::IteratorState it;
  1083. ValueInternalMap::IteratorState itEnd;
  1084. value_.map_->makeBeginIterator( it );
  1085. value_.map_->makeEndIterator( itEnd );
  1086. for ( ; !ValueInternalMap::equals( it, itEnd ); ValueInternalMap::increment(it) )
  1087. members.push_back( std::string( ValueInternalMap::key( it ) ) );
  1088. #endif
  1089. return members;
  1090. }
  1091. //
  1092. //# ifdef JSON_USE_CPPTL
  1093. //EnumMemberNames
  1094. //Value::enumMemberNames() const
  1095. //{
  1096. // if ( type_ == objectValue )
  1097. // {
  1098. // return CppTL::Enum::any( CppTL::Enum::transform(
  1099. // CppTL::Enum::keys( *(value_.map_), CppTL::Type<const CZString &>() ),
  1100. // MemberNamesTransform() ) );
  1101. // }
  1102. // return EnumMemberNames();
  1103. //}
  1104. //
  1105. //
  1106. //EnumValues
  1107. //Value::enumValues() const
  1108. //{
  1109. // if ( type_ == objectValue || type_ == arrayValue )
  1110. // return CppTL::Enum::anyValues( *(value_.map_),
  1111. // CppTL::Type<const Value &>() );
  1112. // return EnumValues();
  1113. //}
  1114. //
  1115. //# endif
  1116. bool
  1117. Value::isNull() const
  1118. {
  1119. return type_ == nullValue;
  1120. }
  1121. bool
  1122. Value::isBool() const
  1123. {
  1124. return type_ == booleanValue;
  1125. }
  1126. bool
  1127. Value::isInt() const
  1128. {
  1129. return type_ == intValue;
  1130. }
  1131. bool
  1132. Value::isUInt() const
  1133. {
  1134. return type_ == uintValue;
  1135. }
  1136. bool
  1137. Value::isIntegral() const
  1138. {
  1139. return type_ == intValue
  1140. || type_ == uintValue
  1141. || type_ == booleanValue;
  1142. }
  1143. bool
  1144. Value::isDouble() const
  1145. {
  1146. return type_ == realValue;
  1147. }
  1148. bool
  1149. Value::isNumeric() const
  1150. {
  1151. return isIntegral() || isDouble();
  1152. }
  1153. bool
  1154. Value::isString() const
  1155. {
  1156. return type_ == stringValue;
  1157. }
  1158. bool
  1159. Value::isArray() const
  1160. {
  1161. return type_ == nullValue || type_ == arrayValue;
  1162. }
  1163. bool
  1164. Value::isObject() const
  1165. {
  1166. return type_ == nullValue || type_ == objectValue;
  1167. }
  1168. void
  1169. Value::setComment( const char *comment,
  1170. CommentPlacement placement )
  1171. {
  1172. if ( !comments_ )
  1173. comments_ = new CommentInfo[numberOfCommentPlacement];
  1174. comments_[placement].setComment( comment );
  1175. }
  1176. void
  1177. Value::setComment( const std::string &comment,
  1178. CommentPlacement placement )
  1179. {
  1180. setComment( comment.c_str(), placement );
  1181. }
  1182. bool
  1183. Value::hasComment( CommentPlacement placement ) const
  1184. {
  1185. return comments_ != 0 && comments_[placement].comment_ != 0;
  1186. }
  1187. std::string
  1188. Value::getComment( CommentPlacement placement ) const
  1189. {
  1190. if ( hasComment(placement) )
  1191. return comments_[placement].comment_;
  1192. return "";
  1193. }
  1194. std::string
  1195. Value::toStyledString() const
  1196. {
  1197. StyledWriter writer;
  1198. return writer.write( *this );
  1199. }
  1200. Value::const_iterator
  1201. Value::begin() const
  1202. {
  1203. switch ( type_ )
  1204. {
  1205. #ifdef JSON_VALUE_USE_INTERNAL_MAP
  1206. case arrayValue:
  1207. if ( value_.array_ )
  1208. {
  1209. ValueInternalArray::IteratorState it;
  1210. value_.array_->makeBeginIterator( it );
  1211. return const_iterator( it );
  1212. }
  1213. break;
  1214. case objectValue:
  1215. if ( value_.map_ )
  1216. {
  1217. ValueInternalMap::IteratorState it;
  1218. value_.map_->makeBeginIterator( it );
  1219. return const_iterator( it );
  1220. }
  1221. break;
  1222. #else
  1223. case arrayValue:
  1224. case objectValue:
  1225. if ( value_.map_ )
  1226. return const_iterator( value_.map_->begin() );
  1227. break;
  1228. #endif
  1229. default:
  1230. break;
  1231. }
  1232. return const_iterator();
  1233. }
  1234. Value::const_iterator
  1235. Value::end() const
  1236. {
  1237. switch ( type_ )
  1238. {
  1239. #ifdef JSON_VALUE_USE_INTERNAL_MAP
  1240. case arrayValue:
  1241. if ( value_.array_ )
  1242. {
  1243. ValueInternalArray::IteratorState it;
  1244. value_.array_->makeEndIterator( it );
  1245. return const_iterator( it );
  1246. }
  1247. break;
  1248. case objectValue:
  1249. if ( value_.map_ )
  1250. {
  1251. ValueInternalMap::IteratorState it;
  1252. value_.map_->makeEndIterator( it );
  1253. return const_iterator( it );
  1254. }
  1255. break;
  1256. #else
  1257. case arrayValue:
  1258. case objectValue:
  1259. if ( value_.map_ )
  1260. return const_iterator( value_.map_->end() );
  1261. break;
  1262. #endif
  1263. default:
  1264. break;
  1265. }
  1266. return const_iterator();
  1267. }
  1268. Value::iterator
  1269. Value::begin()
  1270. {
  1271. switch ( type_ )
  1272. {
  1273. #ifdef JSON_VALUE_USE_INTERNAL_MAP
  1274. case arrayValue:
  1275. if ( value_.array_ )
  1276. {
  1277. ValueInternalArray::IteratorState it;
  1278. value_.array_->makeBeginIterator( it );
  1279. return iterator( it );
  1280. }
  1281. break;
  1282. case objectValue:
  1283. if ( value_.map_ )
  1284. {
  1285. ValueInternalMap::IteratorState it;
  1286. value_.map_->makeBeginIterator( it );
  1287. return iterator( it );
  1288. }
  1289. break;
  1290. #else
  1291. case arrayValue:
  1292. case objectValue:
  1293. if ( value_.map_ )
  1294. return iterator( value_.map_->begin() );
  1295. break;
  1296. #endif
  1297. default:
  1298. break;
  1299. }
  1300. return iterator();
  1301. }
  1302. Value::iterator
  1303. Value::end()
  1304. {
  1305. switch ( type_ )
  1306. {
  1307. #ifdef JSON_VALUE_USE_INTERNAL_MAP
  1308. case arrayValue:
  1309. if ( value_.array_ )
  1310. {
  1311. ValueInternalArray::IteratorState it;
  1312. value_.array_->makeEndIterator( it );
  1313. return iterator( it );
  1314. }
  1315. break;
  1316. case objectValue:
  1317. if ( value_.map_ )
  1318. {
  1319. ValueInternalMap::IteratorState it;
  1320. value_.map_->makeEndIterator( it );
  1321. return iterator( it );
  1322. }
  1323. break;
  1324. #else
  1325. case arrayValue:
  1326. case objectValue:
  1327. if ( value_.map_ )
  1328. return iterator( value_.map_->end() );
  1329. break;
  1330. #endif
  1331. default:
  1332. break;
  1333. }
  1334. return iterator();
  1335. }
  1336. // class PathArgument
  1337. // //////////////////////////////////////////////////////////////////
  1338. PathArgument::PathArgument()
  1339. : kind_( kindNone )
  1340. {
  1341. }
  1342. PathArgument::PathArgument( Value::UInt index )
  1343. : index_( index )
  1344. , kind_( kindIndex )
  1345. {
  1346. }
  1347. PathArgument::PathArgument( const char *key )
  1348. : key_( key )
  1349. , kind_( kindKey )
  1350. {
  1351. }
  1352. PathArgument::PathArgument( const std::string &key )
  1353. : key_( key.c_str() )
  1354. , kind_( kindKey )
  1355. {
  1356. }
  1357. // class Path
  1358. // //////////////////////////////////////////////////////////////////
  1359. Path::Path( const std::string &path,
  1360. const PathArgument &a1,
  1361. const PathArgument &a2,
  1362. const PathArgument &a3,
  1363. const PathArgument &a4,
  1364. const PathArgument &a5 )
  1365. {
  1366. InArgs in;
  1367. in.push_back( &a1 );
  1368. in.push_back( &a2 );
  1369. in.push_back( &a3 );
  1370. in.push_back( &a4 );
  1371. in.push_back( &a5 );
  1372. makePath( path, in );
  1373. }
  1374. void
  1375. Path::makePath( const std::string &path,
  1376. const InArgs &in )
  1377. {
  1378. const char *current = path.c_str();
  1379. const char *end = current + path.length();
  1380. InArgs::const_iterator itInArg = in.begin();
  1381. while ( current != end )
  1382. {
  1383. if ( *current == '[' )
  1384. {
  1385. ++current;
  1386. if ( *current == '%' )
  1387. addPathInArg( path, in, itInArg, PathArgument::kindIndex );
  1388. else
  1389. {
  1390. Value::UInt index = 0;
  1391. for ( ; current != end && *current >= '0' && *current <= '9'; ++current )
  1392. index = index * 10 + Value::UInt(*current - '0');
  1393. args_.push_back( index );
  1394. }
  1395. if ( current == end || *current++ != ']' )
  1396. invalidPath( path, int(current - path.c_str()) );
  1397. }
  1398. else if ( *current == '%' )
  1399. {
  1400. addPathInArg( path, in, itInArg, PathArgument::kindKey );
  1401. ++current;
  1402. }
  1403. else if ( *current == '.' )
  1404. {
  1405. ++current;
  1406. }
  1407. else
  1408. {
  1409. const char *beginName = current;
  1410. while ( current != end && !strchr( "[.", *current ) )
  1411. ++current;
  1412. args_.push_back( std::string( beginName, current ) );
  1413. }
  1414. }
  1415. }
  1416. void
  1417. Path::addPathInArg( const std::string &path,
  1418. const InArgs &in,
  1419. InArgs::const_iterator &itInArg,
  1420. PathArgument::Kind kind )
  1421. {
  1422. if ( itInArg == in.end() )
  1423. {
  1424. // Error: missing argument %d
  1425. }
  1426. else if ( (*itInArg)->kind_ != kind )
  1427. {
  1428. // Error: bad argument type
  1429. }
  1430. else
  1431. {
  1432. args_.push_back( **itInArg );
  1433. }
  1434. }
  1435. void
  1436. Path::invalidPath( const std::string &path,
  1437. int location )
  1438. {
  1439. // Error: invalid path.
  1440. }
  1441. const Value &
  1442. Path::resolve( const Value &root ) const
  1443. {
  1444. const Value *node = &root;
  1445. for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it )
  1446. {
  1447. const PathArgument &arg = *it;
  1448. if ( arg.kind_ == PathArgument::kindIndex )
  1449. {
  1450. if ( !node->isArray() || node->isValidIndex( arg.index_ ) )
  1451. {
  1452. // Error: unable to resolve path (array value expected at position...
  1453. }
  1454. node = &((*node)[arg.index_]);
  1455. }
  1456. else if ( arg.kind_ == PathArgument::kindKey )
  1457. {
  1458. if ( !node->isObject() )
  1459. {
  1460. // Error: unable to resolve path (object value expected at position...)
  1461. }
  1462. node = &((*node)[arg.key_]);
  1463. if ( node == &Value::null )
  1464. {
  1465. // Error: unable to resolve path (object has no member named '' at position...)
  1466. }
  1467. }
  1468. }
  1469. return *node;
  1470. }
  1471. Value
  1472. Path::resolve( const Value &root,
  1473. const Value &defaultValue ) const
  1474. {
  1475. const Value *node = &root;
  1476. for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it )
  1477. {
  1478. const PathArgument &arg = *it;
  1479. if ( arg.kind_ == PathArgument::kindIndex )
  1480. {
  1481. if ( !node->isArray() || node->isValidIndex( arg.index_ ) )
  1482. return defaultValue;
  1483. node = &((*node)[arg.index_]);
  1484. }
  1485. else if ( arg.kind_ == PathArgument::kindKey )
  1486. {
  1487. if ( !node->isObject() )
  1488. return defaultValue;
  1489. node = &((*node)[arg.key_]);
  1490. if ( node == &Value::null )
  1491. return defaultValue;
  1492. }
  1493. }
  1494. return *node;
  1495. }
  1496. Value &
  1497. Path::make( Value &root ) const
  1498. {
  1499. Value *node = &root;
  1500. for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it )
  1501. {
  1502. const PathArgument &arg = *it;
  1503. if ( arg.kind_ == PathArgument::kindIndex )
  1504. {
  1505. if ( !node->isArray() )
  1506. {
  1507. // Error: node is not an array at position ...
  1508. }
  1509. node = &((*node)[arg.index_]);
  1510. }
  1511. else if ( arg.kind_ == PathArgument::kindKey )
  1512. {
  1513. if ( !node->isObject() )
  1514. {
  1515. // Error: node is not an object at position...
  1516. }
  1517. node = &((*node)[arg.key_]);
  1518. }
  1519. }
  1520. return *node;
  1521. }
  1522. } // namespace Json