JSONValue.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:addmember
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Context.h"
  24. #include "Log.h"
  25. #include "JSONFile.h"
  26. #include <rapidjson/document.h>
  27. #include "DebugNew.h"
  28. using namespace rapidjson;
  29. namespace Urho3D
  30. {
  31. const JSONValue JSONValue::EMPTY;
  32. static rapidjson::Type ToRapidJsonType(JSONValueType valueType)
  33. {
  34. if (valueType == JVT_OBJECT)
  35. return kObjectType;
  36. else if (valueType == JVT_ARRAY)
  37. return kArrayType;
  38. return kNullType;
  39. }
  40. JSONValue::JSONValue() :
  41. file_(0),
  42. value_(0)
  43. {
  44. }
  45. JSONValue::JSONValue(JSONFile* file, rapidjson::Value* value) :
  46. file_(file),
  47. value_(value)
  48. {
  49. }
  50. JSONValue::JSONValue(const JSONValue& rhs) :
  51. file_(rhs.file_),
  52. value_(rhs.value_)
  53. {
  54. }
  55. JSONValue::~JSONValue()
  56. {
  57. }
  58. JSONValue& JSONValue::operator = (const JSONValue& rhs)
  59. {
  60. file_ = rhs.file_;
  61. value_ = rhs.value_;
  62. return *this;
  63. }
  64. bool JSONValue::IsNull() const
  65. {
  66. return value_ == 0;
  67. }
  68. bool JSONValue::NotNull() const
  69. {
  70. return value_ != 0;
  71. }
  72. JSONValue::operator bool() const
  73. {
  74. return NotNull();
  75. }
  76. JSONValue JSONValue::CreateChild(const String& name, JSONValueType valueType)
  77. {
  78. assert(IsObject());
  79. if (!IsObject())
  80. return JSONValue::EMPTY;
  81. Value jsonValue;
  82. if (valueType == JVT_OBJECT)
  83. jsonValue.SetObject();
  84. else if (valueType == JVT_ARRAY)
  85. jsonValue.SetArray();
  86. AddMember(name, jsonValue);
  87. return GetChild(name, valueType);
  88. }
  89. JSONValue JSONValue::GetChild(const String& name, JSONValueType valueType) const
  90. {
  91. assert(IsObject());
  92. if (!value_->HasMember(name.CString()))
  93. return JSONValue::EMPTY;
  94. Value& value = GetMember(name);
  95. if (valueType != JVT_UNKNOWN && value.GetType() != ToRapidJsonType(valueType))
  96. return JSONValue::EMPTY;
  97. return JSONValue(file_, &value);
  98. }
  99. void JSONValue::SetInt(const String& name, int value)
  100. {
  101. Value jsonValue;
  102. jsonValue.SetInt(value);
  103. AddMember(name, jsonValue);
  104. }
  105. void JSONValue::SetBool(const String& name, bool value)
  106. {
  107. Value jsonValue;
  108. jsonValue.SetBool(value);
  109. AddMember(name, jsonValue);
  110. }
  111. void JSONValue::SetFloat(const String& name, float value)
  112. {
  113. Value jsonValue;
  114. jsonValue.SetDouble((double)value);
  115. AddMember(name, jsonValue);
  116. }
  117. void JSONValue::SetVector2(const String& name, const Vector2& value)
  118. {
  119. SetString(name, value.ToString());
  120. }
  121. void JSONValue::SetVector3(const String& name, const Vector3& value)
  122. {
  123. SetString(name, value.ToString());
  124. }
  125. void JSONValue::SetVector4(const String& name, const Vector4& value)
  126. {
  127. SetString(name, value.ToString());
  128. }
  129. void JSONValue::SetVectorVariant(const String& name, const Variant& value)
  130. {
  131. VariantType type = value.GetType();
  132. if (type == VAR_FLOAT || type == VAR_VECTOR2 || type == VAR_VECTOR3 || type == VAR_VECTOR4 || type == VAR_MATRIX3 ||
  133. type == VAR_MATRIX3X4 || type == VAR_MATRIX4)
  134. SetString(name, value.ToString());
  135. }
  136. void JSONValue::SetQuaternion(const String& name, const Quaternion& value)
  137. {
  138. SetString(name, value.ToString());
  139. }
  140. void JSONValue::SetColor(const String& name, const Color& value)
  141. {
  142. SetString(name, value.ToString());
  143. }
  144. void JSONValue::SetString(const String& name, const String& value)
  145. {
  146. Value jsonValue;
  147. jsonValue.SetString(value.CString(), value.Length(), file_->GetDocument()->GetAllocator());
  148. AddMember(name, jsonValue);
  149. }
  150. void JSONValue::SetBuffer(const String& name, const void* data, unsigned size)
  151. {
  152. String dataStr;
  153. BufferToString(dataStr, data, size);
  154. SetString(name, dataStr);
  155. }
  156. void JSONValue::SetBuffer(const String& name, const PODVector<unsigned char>& value)
  157. {
  158. if (!value.Size())
  159. SetString(name, String::EMPTY);
  160. else
  161. SetBuffer(name, &value[0], value.Size());
  162. }
  163. void JSONValue::SetResourceRef(const String& name, const ResourceRef& value)
  164. {
  165. Context* context = file_->GetContext();
  166. SetString(name, String(context->GetTypeName(value.type_)) + ";" + value.name_);
  167. }
  168. void JSONValue::SetResourceRefList(const String& name, const ResourceRefList& value)
  169. {
  170. Context* context = file_->GetContext();
  171. String str(context->GetTypeName(value.type_));
  172. for (unsigned i = 0; i < value.names_.Size(); ++i)
  173. {
  174. str += ";";
  175. str += value.names_[i];
  176. }
  177. SetString(name, str);
  178. }
  179. void JSONValue::SetIntRect(const String& name, const IntRect& value)
  180. {
  181. SetString(name, value.ToString());
  182. }
  183. void JSONValue::SetIntVector2(const String& name, const IntVector2& value)
  184. {
  185. SetString(name, value.ToString());
  186. }
  187. void JSONValue::SetMatrix3(const String& name, const Matrix3& value)
  188. {
  189. SetString(name, value.ToString());
  190. }
  191. void JSONValue::SetMatrix3x4(const String& name, const Matrix3x4& value)
  192. {
  193. SetString(name, value.ToString());
  194. }
  195. void JSONValue::SetMatrix4(const String& name, const Matrix4& value)
  196. {
  197. SetString(name, value.ToString());
  198. }
  199. void JSONValue::SetVariant(const String& name, const Variant& value)
  200. {
  201. // Create child object for variant
  202. JSONValue child = CreateChild(name, JVT_OBJECT);
  203. // Set type
  204. child.SetString("type", value.GetTypeName());
  205. // Set value
  206. child.SetVariantValue("value", value);
  207. }
  208. void JSONValue::SetVariantValue(const String& name, const Variant& value)
  209. {
  210. switch (value.GetType())
  211. {
  212. case VAR_RESOURCEREF:
  213. SetResourceRef(name, value.GetResourceRef());
  214. break;
  215. case VAR_RESOURCEREFLIST:
  216. SetResourceRefList(name, value.GetResourceRefList());
  217. break;
  218. case VAR_VARIANTVECTOR:
  219. case VAR_VARIANTMAP:
  220. LOGERROR("Unsupported value type");
  221. break;
  222. default:
  223. SetString(name, value.ToString());
  224. }
  225. }
  226. bool JSONValue::IsObject() const
  227. {
  228. return value_ && value_->IsObject();
  229. }
  230. int JSONValue::GetInt(const String& name) const
  231. {
  232. return GetMember(name).GetInt();
  233. }
  234. bool JSONValue::GetBool(const String& name) const
  235. {
  236. return GetMember(name).GetBool();
  237. }
  238. float JSONValue::GetFloat(const String& name) const
  239. {
  240. return (float)GetMember(name).GetDouble();
  241. }
  242. Vector2 JSONValue::GetVector2(const String& name) const
  243. {
  244. return ToVector2(GetCString(name));
  245. }
  246. Vector3 JSONValue::GetVector3(const String& name) const
  247. {
  248. return ToVector3(GetCString(name));
  249. }
  250. Vector4 JSONValue::GetVector4(const String& name) const
  251. {
  252. return ToVector4(GetCString(name));
  253. }
  254. Variant JSONValue::GetVectorVariant(const String& name) const
  255. {
  256. return ToVectorVariant(GetCString(name));
  257. }
  258. Quaternion JSONValue::GetQuaternion(const String& name) const
  259. {
  260. return ToQuaternion(GetCString(name));
  261. }
  262. Color JSONValue::GetColor(const String& name) const
  263. {
  264. return ToColor(GetCString(name));
  265. }
  266. String JSONValue::GetString(const String& name) const
  267. {
  268. return GetMember(name).GetString();
  269. }
  270. const char* JSONValue::GetCString(const String& name) const
  271. {
  272. return GetMember(name).GetString();
  273. }
  274. PODVector<unsigned char> JSONValue::GetBuffer(const String& name) const
  275. {
  276. PODVector<unsigned char> buffer;
  277. StringToBuffer(buffer, GetCString(name));
  278. return buffer;
  279. }
  280. bool JSONValue::GetBuffer(const String& name, void* dest, unsigned size) const
  281. {
  282. Vector<String> bytes = GetString(name).Split(' ');
  283. unsigned char* destBytes = (unsigned char*)dest;
  284. if (size < bytes.Size())
  285. return false;
  286. for (unsigned i = 0; i < bytes.Size(); ++i)
  287. destBytes[i] = ToInt(bytes[i]);
  288. return true;
  289. }
  290. ResourceRef JSONValue::GetResourceRef(const String& name) const
  291. {
  292. ResourceRef ret;
  293. Vector<String> values = GetString(name).Split(';');
  294. if (values.Size() == 2)
  295. {
  296. ret.type_ = values[0];
  297. ret.name_ = values[1];
  298. }
  299. return ret;
  300. }
  301. ResourceRefList JSONValue::GetResourceRefList(const String& name) const
  302. {
  303. ResourceRefList ret;
  304. Vector<String> values = GetString(name).Split(';');
  305. if (values.Size() >= 1)
  306. {
  307. ret.type_ = values[0];
  308. ret.names_.Resize(values.Size() - 1);
  309. for (unsigned i = 1; i < values.Size(); ++i)
  310. ret.names_[i - 1] = values[i];
  311. }
  312. return ret;
  313. }
  314. IntRect JSONValue::GetIntRect(const String& name) const
  315. {
  316. return ToIntRect(GetCString(name));
  317. }
  318. IntVector2 JSONValue::GetIntVector2(const String& name) const
  319. {
  320. return ToIntVector2(GetCString(name));
  321. }
  322. Matrix3 JSONValue::GetMatrix3(const String& name) const
  323. {
  324. return ToMatrix3(GetCString(name));
  325. }
  326. Matrix3x4 JSONValue::GetMatrix3x4(const String& name) const
  327. {
  328. return ToMatrix3x4(GetCString(name));
  329. }
  330. Matrix4 JSONValue::GetMatrix4(const String& name) const
  331. {
  332. return ToMatrix4(GetCString(name));
  333. }
  334. Variant JSONValue::GetVariant(const String& name) const
  335. {
  336. // Get child for variant
  337. JSONValue child = GetChild(name, JVT_OBJECT);
  338. if (child.IsNull())
  339. return Variant::EMPTY;
  340. // Get type
  341. VariantType type = Variant::GetTypeFromName(child.GetString("type"));
  342. // Get value
  343. return child.GetVariantValue("value", type);
  344. }
  345. Variant JSONValue::GetVariantValue(const String& name, VariantType type) const
  346. {
  347. Variant ret;
  348. if (type == VAR_RESOURCEREF)
  349. ret = GetResourceRef(name);
  350. else if (type == VAR_RESOURCEREFLIST)
  351. ret = GetResourceRefList(name);
  352. else if (type == VAR_VARIANTVECTOR || type == VAR_VARIANTMAP)
  353. LOGERROR("Unsupported value type");
  354. else
  355. ret.FromString(type, GetCString(name));
  356. return ret;
  357. }
  358. JSONValue JSONValue::CreateChild(JSONValueType valueType)
  359. {
  360. assert(IsArray());
  361. if (!IsArray())
  362. return JSONValue::EMPTY;
  363. Value value(ToRapidJsonType(valueType));
  364. value_->PushBack(value, file_->GetDocument()->GetAllocator());
  365. return GetChild(GetSize() - 1, valueType);
  366. }
  367. JSONValue JSONValue::GetChild(unsigned index, JSONValueType valueType) const
  368. {
  369. if (index >= GetSize())
  370. return JSONValue::EMPTY;
  371. const Value& value = (*value_)[(SizeType)index];
  372. if (valueType != JVT_UNKNOWN && value.GetType() != ToRapidJsonType(valueType))
  373. return JSONValue::EMPTY;
  374. return JSONValue(file_, (Value*)&value);
  375. }
  376. void JSONValue::PushInt(int value)
  377. {
  378. Value jsonValue;
  379. jsonValue.SetInt(value);
  380. PushMember(jsonValue);
  381. }
  382. void JSONValue::PushBool(bool value)
  383. {
  384. Value jsonValue;
  385. jsonValue.SetBool(value);
  386. PushMember(jsonValue);
  387. }
  388. void JSONValue::PushFloat(float value)
  389. {
  390. Value jsonValue;
  391. jsonValue.SetDouble((double)value);
  392. PushMember(jsonValue);
  393. }
  394. void JSONValue::PushVector2(const Vector2& value)
  395. {
  396. PushString(value.ToString());
  397. }
  398. void JSONValue::PushVector3(const Vector3& value)
  399. {
  400. PushString(value.ToString());
  401. }
  402. void JSONValue::PushVector4(const Vector4& value)
  403. {
  404. PushString(value.ToString());
  405. }
  406. void JSONValue::PushVectorVariant(const Variant& value)
  407. {
  408. VariantType type = value.GetType();
  409. if (type == VAR_FLOAT || type == VAR_VECTOR2 || type == VAR_VECTOR3 || type == VAR_VECTOR4 || type == VAR_MATRIX3 ||
  410. type == VAR_MATRIX3X4 || type == VAR_MATRIX4)
  411. PushString(value.ToString());
  412. }
  413. void JSONValue::PushQuaternion(const Quaternion& value)
  414. {
  415. PushString(value.ToString());
  416. }
  417. void JSONValue::PushColor(const Color& value)
  418. {
  419. PushString(value.ToString());
  420. }
  421. void JSONValue::PushString(const String& value)
  422. {
  423. Value jsonValue;
  424. jsonValue.SetString(value.CString(), value.Length(), file_->GetDocument()->GetAllocator());
  425. PushMember(jsonValue);
  426. }
  427. void JSONValue::PushBuffer(const PODVector<unsigned char>& value)
  428. {
  429. if (!value.Size())
  430. PushString(String::EMPTY);
  431. else
  432. PushBuffer(&value[0], value.Size());
  433. }
  434. void JSONValue::PushBuffer(const void* data, unsigned size)
  435. {
  436. String dataStr;
  437. BufferToString(dataStr, data, size);
  438. PushString(dataStr);
  439. }
  440. void JSONValue::PushResourceRef(const ResourceRef& value)
  441. {
  442. Context* context = file_->GetContext();
  443. PushString(String(context->GetTypeName(value.type_)) + ";" + value.name_);
  444. }
  445. void JSONValue::PushResourceRefList(const ResourceRefList& value)
  446. {
  447. Context* context = file_->GetContext();
  448. String str(context->GetTypeName(value.type_));
  449. for (unsigned i = 0; i < value.names_.Size(); ++i)
  450. {
  451. str += ";";
  452. str += value.names_[i];
  453. }
  454. PushString(str);
  455. }
  456. void JSONValue::PushIntRect(const IntRect& value)
  457. {
  458. PushString(value.ToString());
  459. }
  460. void JSONValue::PushIntVector2(const IntVector2& value)
  461. {
  462. PushString(value.ToString());
  463. }
  464. void JSONValue::PushMatrix3(const Matrix3& value)
  465. {
  466. PushString(value.ToString());
  467. }
  468. void JSONValue::PushMatrix3x4(const Matrix3x4& value)
  469. {
  470. PushString(value.ToString());
  471. }
  472. void JSONValue::PushMatrix4(const Matrix4& value)
  473. {
  474. PushString(value.ToString());
  475. }
  476. void JSONValue::PushVariant(const Variant& value)
  477. {
  478. // Create child object for variant
  479. JSONValue child = CreateChild(JVT_OBJECT);
  480. // Set type
  481. child.SetString("type", value.GetTypeName());
  482. // Set value
  483. child.SetVariantValue("value", value);
  484. }
  485. void JSONValue::PushVariantValue(const Variant& value)
  486. {
  487. switch (value.GetType())
  488. {
  489. case VAR_RESOURCEREF:
  490. PushResourceRef(value.GetResourceRef());
  491. break;
  492. case VAR_RESOURCEREFLIST:
  493. PushResourceRefList(value.GetResourceRefList());
  494. break;
  495. case VAR_VARIANTVECTOR:
  496. case VAR_VARIANTMAP:
  497. LOGERROR("Unsupported value type");
  498. break;
  499. default:
  500. PushString(value.ToString());
  501. }
  502. }
  503. unsigned JSONValue::GetSize() const
  504. {
  505. if (IsArray())
  506. return (unsigned)value_->Size();
  507. else
  508. return 0;
  509. }
  510. bool JSONValue::IsArray() const
  511. {
  512. return value_ && value_->IsArray();
  513. }
  514. int JSONValue::GetInt(unsigned index) const
  515. {
  516. return GetMember(index).GetInt();
  517. }
  518. bool JSONValue::GetBool(unsigned index) const
  519. {
  520. return GetMember(index).GetBool();
  521. }
  522. float JSONValue::GetFloat(unsigned index) const
  523. {
  524. return (float)GetMember(index).GetDouble();
  525. }
  526. Vector2 JSONValue::GetVector2(unsigned index) const
  527. {
  528. return ToVector2(GetCString(index));
  529. }
  530. Vector3 JSONValue::GetVector3(unsigned index) const
  531. {
  532. return ToVector3(GetCString(index));
  533. }
  534. Vector4 JSONValue::GetVector4(unsigned index) const
  535. {
  536. return ToVector4(GetCString(index));
  537. }
  538. Variant JSONValue::GetVectorVariant(unsigned index) const
  539. {
  540. return ToVectorVariant(GetCString(index));
  541. }
  542. Quaternion JSONValue::GetQuaternion(unsigned index) const
  543. {
  544. return ToQuaternion(GetCString(index));
  545. }
  546. Color JSONValue::GetColor(unsigned index) const
  547. {
  548. return ToColor(GetCString(index));
  549. }
  550. String JSONValue::GetString(unsigned index) const
  551. {
  552. return GetMember(index).GetString();
  553. }
  554. const char* JSONValue::GetCString(unsigned index) const
  555. {
  556. return GetMember(index).GetString();
  557. }
  558. PODVector<unsigned char> JSONValue::GetBuffer(unsigned index) const
  559. {
  560. PODVector<unsigned char> buffer;
  561. StringToBuffer(buffer, GetCString(index));
  562. return buffer;
  563. }
  564. bool JSONValue::GetBuffer(unsigned index, void* dest, unsigned size) const
  565. {
  566. Vector<String> bytes = GetString(index).Split(' ');
  567. unsigned char* destBytes = (unsigned char*)dest;
  568. if (size < bytes.Size())
  569. return false;
  570. for (unsigned i = 0; i < bytes.Size(); ++i)
  571. destBytes[i] = ToInt(bytes[i]);
  572. return true;
  573. }
  574. ResourceRef JSONValue::GetResourceRef(unsigned index) const
  575. {
  576. ResourceRef ret;
  577. Vector<String> values = GetString(index).Split(';');
  578. if (values.Size() == 2)
  579. {
  580. ret.type_ = values[0];
  581. ret.name_ = values[1];
  582. }
  583. return ret;
  584. }
  585. ResourceRefList JSONValue::GetResourceRefList(unsigned index) const
  586. {
  587. ResourceRefList ret;
  588. Vector<String> values = GetString(index).Split(';');
  589. if (values.Size() >= 1)
  590. {
  591. ret.type_ = values[0];
  592. ret.names_.Resize(values.Size() - 1);
  593. for (unsigned i = 1; i < values.Size(); ++i)
  594. ret.names_[i - 1] = values[i];
  595. }
  596. return ret;
  597. }
  598. IntRect JSONValue::GetIntRect(unsigned index) const
  599. {
  600. return ToIntRect(GetCString(index));
  601. }
  602. IntVector2 JSONValue::GetIntVector2(unsigned index) const
  603. {
  604. return ToIntVector2(GetCString(index));
  605. }
  606. Matrix3 JSONValue::GetMatrix3(unsigned index) const
  607. {
  608. return ToMatrix3(GetCString(index));
  609. }
  610. Matrix3x4 JSONValue::GetMatrix3x4(unsigned index) const
  611. {
  612. return ToMatrix3x4(GetCString(index));
  613. }
  614. Matrix4 JSONValue::GetMatrix4(unsigned index) const
  615. {
  616. return ToMatrix4(GetCString(index));
  617. }
  618. Variant JSONValue::GetVariant(unsigned index) const
  619. {
  620. // Get child for variant
  621. JSONValue child = GetChild(index, JVT_OBJECT);
  622. if (child.IsNull())
  623. return Variant::EMPTY;
  624. // Get type
  625. VariantType type = Variant::GetTypeFromName(child.GetString("type"));
  626. // Get value
  627. return child.GetVariantValue("value", type);
  628. }
  629. Variant JSONValue::GetVariantValue(unsigned index, VariantType type) const
  630. {
  631. Variant ret;
  632. if (type == VAR_RESOURCEREF)
  633. ret = GetResourceRef(index);
  634. else if (type == VAR_RESOURCEREFLIST)
  635. ret = GetResourceRefList(index);
  636. else if (type == VAR_VARIANTVECTOR || type == VAR_VARIANTMAP)
  637. LOGERROR("Unsupported value type");
  638. else
  639. ret.FromString(type, GetCString(index));
  640. return ret;
  641. }
  642. void JSONValue::AddMember(const String& name, rapidjson::Value& jsonValue)
  643. {
  644. if (!IsObject())
  645. return;
  646. Value jsonName;
  647. jsonName.SetString(name.CString(), name.Length(), file_->GetDocument()->GetAllocator());
  648. value_->AddMember(jsonName, jsonValue, file_->GetDocument()->GetAllocator());
  649. }
  650. rapidjson::Value& JSONValue::GetMember(const String& name) const
  651. {
  652. assert(IsObject());
  653. return (*value_)[name.CString()];
  654. }
  655. void JSONValue::PushMember(rapidjson::Value& jsonValue)
  656. {
  657. assert(IsArray());
  658. value_->PushBack(jsonValue, file_->GetDocument()->GetAllocator());
  659. }
  660. rapidjson::Value& JSONValue::GetMember(unsigned index) const
  661. {
  662. assert(IsArray());
  663. return (*value_)[(SizeType)index];
  664. }
  665. }