JSONValue.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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 == JSON_OBJECT)
  35. return kObjectType;
  36. else if (valueType == JSON_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 == JSON_OBJECT)
  83. jsonValue.SetObject();
  84. else if (valueType == JSON_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 != JSON_ANY && 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, JSON_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. Vector<String> JSONValue::GetChildNames() const
  231. {
  232. Vector<String> ret;
  233. if (!IsObject())
  234. return ret;
  235. for (Value::ConstMemberIterator i = value_->MemberBegin(); i != value_->MemberEnd(); ++i)
  236. {
  237. // Only reutrn name for child object and array
  238. if (i->value.GetType() == kArrayType || i->value.GetType() == kObjectType)
  239. ret.Push(i->name.GetString());
  240. }
  241. return ret;
  242. }
  243. int JSONValue::GetInt(const String& name) const
  244. {
  245. return GetMember(name).GetInt();
  246. }
  247. bool JSONValue::GetBool(const String& name) const
  248. {
  249. return GetMember(name).GetBool();
  250. }
  251. float JSONValue::GetFloat(const String& name) const
  252. {
  253. return (float)GetMember(name).GetDouble();
  254. }
  255. Vector2 JSONValue::GetVector2(const String& name) const
  256. {
  257. return ToVector2(GetCString(name));
  258. }
  259. Vector3 JSONValue::GetVector3(const String& name) const
  260. {
  261. return ToVector3(GetCString(name));
  262. }
  263. Vector4 JSONValue::GetVector4(const String& name) const
  264. {
  265. return ToVector4(GetCString(name));
  266. }
  267. Variant JSONValue::GetVectorVariant(const String& name) const
  268. {
  269. return ToVectorVariant(GetCString(name));
  270. }
  271. Quaternion JSONValue::GetQuaternion(const String& name) const
  272. {
  273. return ToQuaternion(GetCString(name));
  274. }
  275. Color JSONValue::GetColor(const String& name) const
  276. {
  277. return ToColor(GetCString(name));
  278. }
  279. String JSONValue::GetString(const String& name) const
  280. {
  281. return GetMember(name).GetString();
  282. }
  283. const char* JSONValue::GetCString(const String& name) const
  284. {
  285. return GetMember(name).GetString();
  286. }
  287. PODVector<unsigned char> JSONValue::GetBuffer(const String& name) const
  288. {
  289. PODVector<unsigned char> buffer;
  290. StringToBuffer(buffer, GetCString(name));
  291. return buffer;
  292. }
  293. bool JSONValue::GetBuffer(const String& name, void* dest, unsigned size) const
  294. {
  295. Vector<String> bytes = GetString(name).Split(' ');
  296. unsigned char* destBytes = (unsigned char*)dest;
  297. if (size < bytes.Size())
  298. return false;
  299. for (unsigned i = 0; i < bytes.Size(); ++i)
  300. destBytes[i] = ToInt(bytes[i]);
  301. return true;
  302. }
  303. ResourceRef JSONValue::GetResourceRef(const String& name) const
  304. {
  305. ResourceRef ret;
  306. Vector<String> values = GetString(name).Split(';');
  307. if (values.Size() == 2)
  308. {
  309. ret.type_ = values[0];
  310. ret.name_ = values[1];
  311. }
  312. return ret;
  313. }
  314. ResourceRefList JSONValue::GetResourceRefList(const String& name) const
  315. {
  316. ResourceRefList ret;
  317. Vector<String> values = GetString(name).Split(';');
  318. if (values.Size() >= 1)
  319. {
  320. ret.type_ = values[0];
  321. ret.names_.Resize(values.Size() - 1);
  322. for (unsigned i = 1; i < values.Size(); ++i)
  323. ret.names_[i - 1] = values[i];
  324. }
  325. return ret;
  326. }
  327. IntRect JSONValue::GetIntRect(const String& name) const
  328. {
  329. return ToIntRect(GetCString(name));
  330. }
  331. IntVector2 JSONValue::GetIntVector2(const String& name) const
  332. {
  333. return ToIntVector2(GetCString(name));
  334. }
  335. Matrix3 JSONValue::GetMatrix3(const String& name) const
  336. {
  337. return ToMatrix3(GetCString(name));
  338. }
  339. Matrix3x4 JSONValue::GetMatrix3x4(const String& name) const
  340. {
  341. return ToMatrix3x4(GetCString(name));
  342. }
  343. Matrix4 JSONValue::GetMatrix4(const String& name) const
  344. {
  345. return ToMatrix4(GetCString(name));
  346. }
  347. Variant JSONValue::GetVariant(const String& name) const
  348. {
  349. // Get child for variant
  350. JSONValue child = GetChild(name, JSON_OBJECT);
  351. if (child.IsNull())
  352. return Variant::EMPTY;
  353. // Get type
  354. VariantType type = Variant::GetTypeFromName(child.GetString("type"));
  355. // Get value
  356. return child.GetVariantValue("value", type);
  357. }
  358. Variant JSONValue::GetVariantValue(const String& name, VariantType type) const
  359. {
  360. Variant ret;
  361. if (type == VAR_RESOURCEREF)
  362. ret = GetResourceRef(name);
  363. else if (type == VAR_RESOURCEREFLIST)
  364. ret = GetResourceRefList(name);
  365. else if (type == VAR_VARIANTVECTOR || type == VAR_VARIANTMAP)
  366. LOGERROR("Unsupported value type");
  367. else
  368. ret.FromString(type, GetCString(name));
  369. return ret;
  370. }
  371. JSONValue JSONValue::CreateChild(JSONValueType valueType)
  372. {
  373. assert(IsArray());
  374. if (!IsArray())
  375. return JSONValue::EMPTY;
  376. Value value(ToRapidJsonType(valueType));
  377. value_->PushBack(value, file_->GetDocument()->GetAllocator());
  378. return GetChild(GetSize() - 1, valueType);
  379. }
  380. JSONValue JSONValue::GetChild(unsigned index, JSONValueType valueType) const
  381. {
  382. if (index >= GetSize())
  383. return JSONValue::EMPTY;
  384. const Value& value = (*value_)[(SizeType)index];
  385. if (valueType != JSON_ANY && value.GetType() != ToRapidJsonType(valueType))
  386. return JSONValue::EMPTY;
  387. return JSONValue(file_, (Value*)&value);
  388. }
  389. void JSONValue::AddInt(int value)
  390. {
  391. Value jsonValue;
  392. jsonValue.SetInt(value);
  393. AddMember(jsonValue);
  394. }
  395. void JSONValue::AddBool(bool value)
  396. {
  397. Value jsonValue;
  398. jsonValue.SetBool(value);
  399. AddMember(jsonValue);
  400. }
  401. void JSONValue::AddFloat(float value)
  402. {
  403. Value jsonValue;
  404. jsonValue.SetDouble((double)value);
  405. AddMember(jsonValue);
  406. }
  407. void JSONValue::AddVector2(const Vector2& value)
  408. {
  409. AddString(value.ToString());
  410. }
  411. void JSONValue::AddVector3(const Vector3& value)
  412. {
  413. AddString(value.ToString());
  414. }
  415. void JSONValue::AddVector4(const Vector4& value)
  416. {
  417. AddString(value.ToString());
  418. }
  419. void JSONValue::AddVectorVariant(const Variant& value)
  420. {
  421. VariantType type = value.GetType();
  422. if (type == VAR_FLOAT || type == VAR_VECTOR2 || type == VAR_VECTOR3 || type == VAR_VECTOR4 || type == VAR_MATRIX3 ||
  423. type == VAR_MATRIX3X4 || type == VAR_MATRIX4)
  424. AddString(value.ToString());
  425. }
  426. void JSONValue::AddQuaternion(const Quaternion& value)
  427. {
  428. AddString(value.ToString());
  429. }
  430. void JSONValue::AddColor(const Color& value)
  431. {
  432. AddString(value.ToString());
  433. }
  434. void JSONValue::AddString(const String& value)
  435. {
  436. Value jsonValue;
  437. jsonValue.SetString(value.CString(), value.Length(), file_->GetDocument()->GetAllocator());
  438. AddMember(jsonValue);
  439. }
  440. void JSONValue::AddBuffer(const PODVector<unsigned char>& value)
  441. {
  442. if (!value.Size())
  443. AddString(String::EMPTY);
  444. else
  445. AddBuffer(&value[0], value.Size());
  446. }
  447. void JSONValue::AddBuffer(const void* data, unsigned size)
  448. {
  449. String dataStr;
  450. BufferToString(dataStr, data, size);
  451. AddString(dataStr);
  452. }
  453. void JSONValue::AddResourceRef(const ResourceRef& value)
  454. {
  455. Context* context = file_->GetContext();
  456. AddString(String(context->GetTypeName(value.type_)) + ";" + value.name_);
  457. }
  458. void JSONValue::AddResourceRefList(const ResourceRefList& value)
  459. {
  460. Context* context = file_->GetContext();
  461. String str(context->GetTypeName(value.type_));
  462. for (unsigned i = 0; i < value.names_.Size(); ++i)
  463. {
  464. str += ";";
  465. str += value.names_[i];
  466. }
  467. AddString(str);
  468. }
  469. void JSONValue::AddIntRect(const IntRect& value)
  470. {
  471. AddString(value.ToString());
  472. }
  473. void JSONValue::AddIntVector2(const IntVector2& value)
  474. {
  475. AddString(value.ToString());
  476. }
  477. void JSONValue::AddMatrix3(const Matrix3& value)
  478. {
  479. AddString(value.ToString());
  480. }
  481. void JSONValue::AddMatrix3x4(const Matrix3x4& value)
  482. {
  483. AddString(value.ToString());
  484. }
  485. void JSONValue::AddMatrix4(const Matrix4& value)
  486. {
  487. AddString(value.ToString());
  488. }
  489. void JSONValue::AddVariant(const Variant& value)
  490. {
  491. // Create child object for variant
  492. JSONValue child = CreateChild(JSON_OBJECT);
  493. // Set type
  494. child.SetString("type", value.GetTypeName());
  495. // Set value
  496. child.SetVariantValue("value", value);
  497. }
  498. void JSONValue::AddVariantValue(const Variant& value)
  499. {
  500. switch (value.GetType())
  501. {
  502. case VAR_RESOURCEREF:
  503. AddResourceRef(value.GetResourceRef());
  504. break;
  505. case VAR_RESOURCEREFLIST:
  506. AddResourceRefList(value.GetResourceRefList());
  507. break;
  508. case VAR_VARIANTVECTOR:
  509. case VAR_VARIANTMAP:
  510. LOGERROR("Unsupported value type");
  511. break;
  512. default:
  513. AddString(value.ToString());
  514. }
  515. }
  516. unsigned JSONValue::GetSize() const
  517. {
  518. if (IsArray())
  519. return (unsigned)value_->Size();
  520. else
  521. return 0;
  522. }
  523. bool JSONValue::IsArray() const
  524. {
  525. return value_ && value_->IsArray();
  526. }
  527. int JSONValue::GetInt(unsigned index) const
  528. {
  529. return GetMember(index).GetInt();
  530. }
  531. bool JSONValue::GetBool(unsigned index) const
  532. {
  533. return GetMember(index).GetBool();
  534. }
  535. float JSONValue::GetFloat(unsigned index) const
  536. {
  537. return (float)GetMember(index).GetDouble();
  538. }
  539. Vector2 JSONValue::GetVector2(unsigned index) const
  540. {
  541. return ToVector2(GetCString(index));
  542. }
  543. Vector3 JSONValue::GetVector3(unsigned index) const
  544. {
  545. return ToVector3(GetCString(index));
  546. }
  547. Vector4 JSONValue::GetVector4(unsigned index) const
  548. {
  549. return ToVector4(GetCString(index));
  550. }
  551. Variant JSONValue::GetVectorVariant(unsigned index) const
  552. {
  553. return ToVectorVariant(GetCString(index));
  554. }
  555. Quaternion JSONValue::GetQuaternion(unsigned index) const
  556. {
  557. return ToQuaternion(GetCString(index));
  558. }
  559. Color JSONValue::GetColor(unsigned index) const
  560. {
  561. return ToColor(GetCString(index));
  562. }
  563. String JSONValue::GetString(unsigned index) const
  564. {
  565. return GetMember(index).GetString();
  566. }
  567. const char* JSONValue::GetCString(unsigned index) const
  568. {
  569. return GetMember(index).GetString();
  570. }
  571. PODVector<unsigned char> JSONValue::GetBuffer(unsigned index) const
  572. {
  573. PODVector<unsigned char> buffer;
  574. StringToBuffer(buffer, GetCString(index));
  575. return buffer;
  576. }
  577. bool JSONValue::GetBuffer(unsigned index, void* dest, unsigned size) const
  578. {
  579. Vector<String> bytes = GetString(index).Split(' ');
  580. unsigned char* destBytes = (unsigned char*)dest;
  581. if (size < bytes.Size())
  582. return false;
  583. for (unsigned i = 0; i < bytes.Size(); ++i)
  584. destBytes[i] = ToInt(bytes[i]);
  585. return true;
  586. }
  587. ResourceRef JSONValue::GetResourceRef(unsigned index) const
  588. {
  589. ResourceRef ret;
  590. Vector<String> values = GetString(index).Split(';');
  591. if (values.Size() == 2)
  592. {
  593. ret.type_ = values[0];
  594. ret.name_ = values[1];
  595. }
  596. return ret;
  597. }
  598. ResourceRefList JSONValue::GetResourceRefList(unsigned index) const
  599. {
  600. ResourceRefList ret;
  601. Vector<String> values = GetString(index).Split(';');
  602. if (values.Size() >= 1)
  603. {
  604. ret.type_ = values[0];
  605. ret.names_.Resize(values.Size() - 1);
  606. for (unsigned i = 1; i < values.Size(); ++i)
  607. ret.names_[i - 1] = values[i];
  608. }
  609. return ret;
  610. }
  611. IntRect JSONValue::GetIntRect(unsigned index) const
  612. {
  613. return ToIntRect(GetCString(index));
  614. }
  615. IntVector2 JSONValue::GetIntVector2(unsigned index) const
  616. {
  617. return ToIntVector2(GetCString(index));
  618. }
  619. Matrix3 JSONValue::GetMatrix3(unsigned index) const
  620. {
  621. return ToMatrix3(GetCString(index));
  622. }
  623. Matrix3x4 JSONValue::GetMatrix3x4(unsigned index) const
  624. {
  625. return ToMatrix3x4(GetCString(index));
  626. }
  627. Matrix4 JSONValue::GetMatrix4(unsigned index) const
  628. {
  629. return ToMatrix4(GetCString(index));
  630. }
  631. Variant JSONValue::GetVariant(unsigned index) const
  632. {
  633. // Get child for variant
  634. JSONValue child = GetChild(index, JSON_OBJECT);
  635. if (child.IsNull())
  636. return Variant::EMPTY;
  637. // Get type
  638. VariantType type = Variant::GetTypeFromName(child.GetString("type"));
  639. // Get value
  640. return child.GetVariantValue("value", type);
  641. }
  642. Variant JSONValue::GetVariantValue(unsigned index, VariantType type) const
  643. {
  644. Variant ret;
  645. if (type == VAR_RESOURCEREF)
  646. ret = GetResourceRef(index);
  647. else if (type == VAR_RESOURCEREFLIST)
  648. ret = GetResourceRefList(index);
  649. else if (type == VAR_VARIANTVECTOR || type == VAR_VARIANTMAP)
  650. LOGERROR("Unsupported value type");
  651. else
  652. ret.FromString(type, GetCString(index));
  653. return ret;
  654. }
  655. void JSONValue::AddMember(const String& name, rapidjson::Value& jsonValue)
  656. {
  657. if (!IsObject())
  658. return;
  659. Value jsonName;
  660. jsonName.SetString(name.CString(), name.Length(), file_->GetDocument()->GetAllocator());
  661. value_->AddMember(jsonName, jsonValue, file_->GetDocument()->GetAllocator());
  662. }
  663. rapidjson::Value& JSONValue::GetMember(const String& name) const
  664. {
  665. assert(IsObject());
  666. return (*value_)[name.CString()];
  667. }
  668. void JSONValue::AddMember(rapidjson::Value& jsonValue)
  669. {
  670. assert(IsArray());
  671. value_->PushBack(jsonValue, file_->GetDocument()->GetAllocator());
  672. }
  673. rapidjson::Value& JSONValue::GetMember(unsigned index) const
  674. {
  675. assert(IsArray());
  676. return (*value_)[(SizeType)index];
  677. }
  678. }