JSONValue.cpp 20 KB

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