json_value.cpp 38 KB

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