json_value.cpp 41 KB

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