JSONValue.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. //
  2. // Copyright (c) 2008-2015 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 "../Core/Context.h"
  23. #include "../IO/Log.h"
  24. #include "../Resource/JSONFile.h"
  25. #include <rapidjson/document.h>
  26. #include "../DebugNew.h"
  27. using namespace rapidjson;
  28. namespace Urho3D
  29. {
  30. const JSONValue JSONValue::EMPTY;
  31. static rapidjson::Type ToRapidJsonType(JSONValueType valueType)
  32. {
  33. if (valueType == JSON_OBJECT)
  34. return kObjectType;
  35. else if (valueType == JSON_ARRAY)
  36. return kArrayType;
  37. return kNullType;
  38. }
  39. JSONValue::JSONValue() :
  40. file_(0),
  41. value_(0)
  42. {
  43. }
  44. JSONValue::JSONValue(JSONFile* file, rapidjson::Value* value) :
  45. file_(file),
  46. value_(value)
  47. {
  48. }
  49. JSONValue::JSONValue(const JSONValue& rhs) :
  50. file_(rhs.file_),
  51. value_(rhs.value_)
  52. {
  53. }
  54. JSONValue::~JSONValue()
  55. {
  56. }
  57. JSONValue& JSONValue::operator = (const JSONValue& rhs)
  58. {
  59. file_ = rhs.file_;
  60. value_ = rhs.value_;
  61. return *this;
  62. }
  63. bool JSONValue::IsNull() const
  64. {
  65. return value_ == 0;
  66. }
  67. bool JSONValue::NotNull() const
  68. {
  69. return value_ != 0;
  70. }
  71. JSONValue::operator bool() const
  72. {
  73. return NotNull();
  74. }
  75. JSONValue JSONValue::CreateChild(const String& name, JSONValueType valueType)
  76. {
  77. assert(IsObject());
  78. if (!IsObject())
  79. return JSONValue::EMPTY;
  80. Value jsonValue;
  81. if (valueType == JSON_OBJECT)
  82. jsonValue.SetObject();
  83. else if (valueType == JSON_ARRAY)
  84. jsonValue.SetArray();
  85. AddMember(name, jsonValue);
  86. return GetChild(name, valueType);
  87. }
  88. JSONValue JSONValue::GetChild(const String& name, JSONValueType valueType) const
  89. {
  90. assert(IsObject());
  91. if (!value_->HasMember(name.CString()))
  92. return JSONValue::EMPTY;
  93. Value& value = GetMember(name);
  94. if (valueType != JSON_ANY && value.GetType() != ToRapidJsonType(valueType))
  95. return JSONValue::EMPTY;
  96. return JSONValue(file_, &value);
  97. }
  98. void JSONValue::SetInt(const String& name, int value)
  99. {
  100. Value jsonValue;
  101. jsonValue.SetInt(value);
  102. AddMember(name, jsonValue);
  103. }
  104. void JSONValue::SetBool(const String& name, bool value)
  105. {
  106. Value jsonValue;
  107. jsonValue.SetBool(value);
  108. AddMember(name, jsonValue);
  109. }
  110. void JSONValue::SetFloat(const String& name, float value)
  111. {
  112. Value jsonValue;
  113. jsonValue.SetDouble((double)value);
  114. AddMember(name, jsonValue);
  115. }
  116. void JSONValue::SetVector2(const String& name, const Vector2& value)
  117. {
  118. SetString(name, value.ToString());
  119. }
  120. void JSONValue::SetVector3(const String& name, const Vector3& value)
  121. {
  122. SetString(name, value.ToString());
  123. }
  124. void JSONValue::SetVector4(const String& name, const Vector4& value)
  125. {
  126. SetString(name, value.ToString());
  127. }
  128. void JSONValue::SetVectorVariant(const String& name, const Variant& value)
  129. {
  130. VariantType type = value.GetType();
  131. if (type == VAR_FLOAT || type == VAR_VECTOR2 || type == VAR_VECTOR3 || type == VAR_VECTOR4 || type == VAR_MATRIX3 ||
  132. type == VAR_MATRIX3X4 || type == VAR_MATRIX4)
  133. SetString(name, value.ToString());
  134. }
  135. void JSONValue::SetQuaternion(const String& name, const Quaternion& value)
  136. {
  137. SetString(name, value.ToString());
  138. }
  139. void JSONValue::SetColor(const String& name, const Color& value)
  140. {
  141. SetString(name, value.ToString());
  142. }
  143. void JSONValue::SetString(const String& name, const String& value)
  144. {
  145. Value jsonValue;
  146. jsonValue.SetString(value.CString(), value.Length(), file_->GetDocument()->GetAllocator());
  147. AddMember(name, jsonValue);
  148. }
  149. void JSONValue::SetBuffer(const String& name, const void* data, unsigned size)
  150. {
  151. String dataStr;
  152. BufferToString(dataStr, data, size);
  153. SetString(name, dataStr);
  154. }
  155. void JSONValue::SetBuffer(const String& name, const PODVector<unsigned char>& value)
  156. {
  157. if (!value.Size())
  158. SetString(name, String::EMPTY);
  159. else
  160. SetBuffer(name, &value[0], value.Size());
  161. }
  162. void JSONValue::SetResourceRef(const String& name, const ResourceRef& value)
  163. {
  164. Context* context = file_->GetContext();
  165. SetString(name, String(context->GetTypeName(value.type_)) + ";" + value.name_);
  166. }
  167. void JSONValue::SetResourceRefList(const String& name, const ResourceRefList& value)
  168. {
  169. Context* context = file_->GetContext();
  170. String str(context->GetTypeName(value.type_));
  171. for (unsigned i = 0; i < value.names_.Size(); ++i)
  172. {
  173. str += ";";
  174. str += value.names_[i];
  175. }
  176. SetString(name, str);
  177. }
  178. void JSONValue::SetIntRect(const String& name, const IntRect& value)
  179. {
  180. SetString(name, value.ToString());
  181. }
  182. void JSONValue::SetIntVector2(const String& name, const IntVector2& value)
  183. {
  184. SetString(name, value.ToString());
  185. }
  186. void JSONValue::SetMatrix3(const String& name, const Matrix3& value)
  187. {
  188. SetString(name, value.ToString());
  189. }
  190. void JSONValue::SetMatrix3x4(const String& name, const Matrix3x4& value)
  191. {
  192. SetString(name, value.ToString());
  193. }
  194. void JSONValue::SetMatrix4(const String& name, const Matrix4& value)
  195. {
  196. SetString(name, value.ToString());
  197. }
  198. void JSONValue::SetVariant(const String& name, const Variant& value)
  199. {
  200. // Create child object for variant
  201. JSONValue child = CreateChild(name, JSON_OBJECT);
  202. // Set type
  203. child.SetString("type", value.GetTypeName());
  204. // Set value
  205. child.SetVariantValue("value", value);
  206. }
  207. void JSONValue::SetVariantValue(const String& name, const Variant& value)
  208. {
  209. switch (value.GetType())
  210. {
  211. case VAR_RESOURCEREF:
  212. SetResourceRef(name, value.GetResourceRef());
  213. break;
  214. case VAR_RESOURCEREFLIST:
  215. SetResourceRefList(name, value.GetResourceRefList());
  216. break;
  217. case VAR_VARIANTVECTOR:
  218. case VAR_VARIANTMAP:
  219. LOGERROR("Unsupported value type");
  220. break;
  221. default:
  222. SetString(name, value.ToString());
  223. }
  224. }
  225. bool JSONValue::IsObject() const
  226. {
  227. return value_ && value_->IsObject();
  228. }
  229. Vector<String> JSONValue::GetChildNames() const
  230. {
  231. Vector<String> ret;
  232. if (!IsObject())
  233. return ret;
  234. for (Value::ConstMemberIterator i = value_->MemberBegin(); i != value_->MemberEnd(); ++i)
  235. {
  236. // Only reutrn name for child object and array
  237. if (i->value.GetType() == kArrayType || i->value.GetType() == kObjectType)
  238. ret.Push(i->name.GetString());
  239. }
  240. return ret;
  241. }
  242. Vector<String> JSONValue::GetValueNames() const
  243. {
  244. Vector<String> ret;
  245. if (!IsObject())
  246. return ret;
  247. for (Value::ConstMemberIterator i = value_->MemberBegin(); i != value_->MemberEnd(); ++i)
  248. {
  249. if (i->value.GetType() != kArrayType && i->value.GetType() != kObjectType)
  250. ret.Push(i->name.GetString());
  251. }
  252. return ret;
  253. }
  254. int JSONValue::GetInt(const String& name) const
  255. {
  256. return GetMember(name).GetInt();
  257. }
  258. bool JSONValue::GetBool(const String& name) const
  259. {
  260. return GetMember(name).GetBool();
  261. }
  262. float JSONValue::GetFloat(const String& name) const
  263. {
  264. return (float)GetMember(name).GetDouble();
  265. }
  266. Vector2 JSONValue::GetVector2(const String& name) const
  267. {
  268. return ToVector2(GetCString(name));
  269. }
  270. Vector3 JSONValue::GetVector3(const String& name) const
  271. {
  272. return ToVector3(GetCString(name));
  273. }
  274. Vector4 JSONValue::GetVector4(const String& name) const
  275. {
  276. return ToVector4(GetCString(name));
  277. }
  278. Variant JSONValue::GetVectorVariant(const String& name) const
  279. {
  280. return ToVectorVariant(GetCString(name));
  281. }
  282. Quaternion JSONValue::GetQuaternion(const String& name) const
  283. {
  284. return ToQuaternion(GetCString(name));
  285. }
  286. Color JSONValue::GetColor(const String& name) const
  287. {
  288. return ToColor(GetCString(name));
  289. }
  290. String JSONValue::GetString(const String& name) const
  291. {
  292. return GetMember(name).GetString();
  293. }
  294. const char* JSONValue::GetCString(const String& name) const
  295. {
  296. return GetMember(name).GetString();
  297. }
  298. PODVector<unsigned char> JSONValue::GetBuffer(const String& name) const
  299. {
  300. PODVector<unsigned char> buffer;
  301. StringToBuffer(buffer, GetCString(name));
  302. return buffer;
  303. }
  304. bool JSONValue::GetBuffer(const String& name, void* dest, unsigned size) const
  305. {
  306. Vector<String> bytes = GetString(name).Split(' ');
  307. unsigned char* destBytes = (unsigned char*)dest;
  308. if (size < bytes.Size())
  309. return false;
  310. for (unsigned i = 0; i < bytes.Size(); ++i)
  311. destBytes[i] = ToInt(bytes[i]);
  312. return true;
  313. }
  314. ResourceRef JSONValue::GetResourceRef(const String& name) const
  315. {
  316. ResourceRef ret;
  317. Vector<String> values = GetString(name).Split(';');
  318. if (values.Size() == 2)
  319. {
  320. ret.type_ = values[0];
  321. ret.name_ = values[1];
  322. }
  323. return ret;
  324. }
  325. ResourceRefList JSONValue::GetResourceRefList(const String& name) const
  326. {
  327. ResourceRefList ret;
  328. Vector<String> values = GetString(name).Split(';');
  329. if (values.Size() >= 1)
  330. {
  331. ret.type_ = values[0];
  332. ret.names_.Resize(values.Size() - 1);
  333. for (unsigned i = 1; i < values.Size(); ++i)
  334. ret.names_[i - 1] = values[i];
  335. }
  336. return ret;
  337. }
  338. IntRect JSONValue::GetIntRect(const String& name) const
  339. {
  340. return ToIntRect(GetCString(name));
  341. }
  342. IntVector2 JSONValue::GetIntVector2(const String& name) const
  343. {
  344. return ToIntVector2(GetCString(name));
  345. }
  346. Matrix3 JSONValue::GetMatrix3(const String& name) const
  347. {
  348. return ToMatrix3(GetCString(name));
  349. }
  350. Matrix3x4 JSONValue::GetMatrix3x4(const String& name) const
  351. {
  352. return ToMatrix3x4(GetCString(name));
  353. }
  354. Matrix4 JSONValue::GetMatrix4(const String& name) const
  355. {
  356. return ToMatrix4(GetCString(name));
  357. }
  358. Variant JSONValue::GetVariant(const String& name) const
  359. {
  360. // Get child for variant
  361. JSONValue child = GetChild(name, JSON_OBJECT);
  362. if (child.IsNull())
  363. return Variant::EMPTY;
  364. // Get type
  365. VariantType type = Variant::GetTypeFromName(child.GetString("type"));
  366. // Get value
  367. return child.GetVariantValue("value", type);
  368. }
  369. Variant JSONValue::GetVariantValue(const String& name, VariantType type) const
  370. {
  371. Variant ret;
  372. if (type == VAR_RESOURCEREF)
  373. ret = GetResourceRef(name);
  374. else if (type == VAR_RESOURCEREFLIST)
  375. ret = GetResourceRefList(name);
  376. else if (type == VAR_VARIANTVECTOR || type == VAR_VARIANTMAP)
  377. LOGERROR("Unsupported value type");
  378. else
  379. ret.FromString(type, GetCString(name));
  380. return ret;
  381. }
  382. JSONValue JSONValue::CreateChild(JSONValueType valueType)
  383. {
  384. assert(IsArray());
  385. if (!IsArray())
  386. return JSONValue::EMPTY;
  387. Value value(ToRapidJsonType(valueType));
  388. value_->PushBack(value, file_->GetDocument()->GetAllocator());
  389. return GetChild(GetSize() - 1, valueType);
  390. }
  391. JSONValue JSONValue::GetChild(unsigned index, JSONValueType valueType) const
  392. {
  393. if (index >= GetSize())
  394. return JSONValue::EMPTY;
  395. const Value& value = (*value_)[(SizeType)index];
  396. if (valueType != JSON_ANY && value.GetType() != ToRapidJsonType(valueType))
  397. return JSONValue::EMPTY;
  398. return JSONValue(file_, (Value*)&value);
  399. }
  400. void JSONValue::AddInt(int value)
  401. {
  402. Value jsonValue;
  403. jsonValue.SetInt(value);
  404. AddMember(jsonValue);
  405. }
  406. void JSONValue::AddBool(bool value)
  407. {
  408. Value jsonValue;
  409. jsonValue.SetBool(value);
  410. AddMember(jsonValue);
  411. }
  412. void JSONValue::AddFloat(float value)
  413. {
  414. Value jsonValue;
  415. jsonValue.SetDouble((double)value);
  416. AddMember(jsonValue);
  417. }
  418. void JSONValue::AddVector2(const Vector2& value)
  419. {
  420. AddString(value.ToString());
  421. }
  422. void JSONValue::AddVector3(const Vector3& value)
  423. {
  424. AddString(value.ToString());
  425. }
  426. void JSONValue::AddVector4(const Vector4& value)
  427. {
  428. AddString(value.ToString());
  429. }
  430. void JSONValue::AddVectorVariant(const Variant& value)
  431. {
  432. VariantType type = value.GetType();
  433. if (type == VAR_FLOAT || type == VAR_VECTOR2 || type == VAR_VECTOR3 || type == VAR_VECTOR4 || type == VAR_MATRIX3 ||
  434. type == VAR_MATRIX3X4 || type == VAR_MATRIX4)
  435. AddString(value.ToString());
  436. }
  437. void JSONValue::AddQuaternion(const Quaternion& value)
  438. {
  439. AddString(value.ToString());
  440. }
  441. void JSONValue::AddColor(const Color& value)
  442. {
  443. AddString(value.ToString());
  444. }
  445. void JSONValue::AddString(const String& value)
  446. {
  447. Value jsonValue;
  448. jsonValue.SetString(value.CString(), value.Length(), file_->GetDocument()->GetAllocator());
  449. AddMember(jsonValue);
  450. }
  451. void JSONValue::AddBuffer(const PODVector<unsigned char>& value)
  452. {
  453. if (!value.Size())
  454. AddString(String::EMPTY);
  455. else
  456. AddBuffer(&value[0], value.Size());
  457. }
  458. void JSONValue::AddBuffer(const void* data, unsigned size)
  459. {
  460. String dataStr;
  461. BufferToString(dataStr, data, size);
  462. AddString(dataStr);
  463. }
  464. void JSONValue::AddResourceRef(const ResourceRef& value)
  465. {
  466. Context* context = file_->GetContext();
  467. AddString(String(context->GetTypeName(value.type_)) + ";" + value.name_);
  468. }
  469. void JSONValue::AddResourceRefList(const ResourceRefList& value)
  470. {
  471. Context* context = file_->GetContext();
  472. String str(context->GetTypeName(value.type_));
  473. for (unsigned i = 0; i < value.names_.Size(); ++i)
  474. {
  475. str += ";";
  476. str += value.names_[i];
  477. }
  478. AddString(str);
  479. }
  480. void JSONValue::AddIntRect(const IntRect& value)
  481. {
  482. AddString(value.ToString());
  483. }
  484. void JSONValue::AddIntVector2(const IntVector2& value)
  485. {
  486. AddString(value.ToString());
  487. }
  488. void JSONValue::AddMatrix3(const Matrix3& value)
  489. {
  490. AddString(value.ToString());
  491. }
  492. void JSONValue::AddMatrix3x4(const Matrix3x4& value)
  493. {
  494. AddString(value.ToString());
  495. }
  496. void JSONValue::AddMatrix4(const Matrix4& value)
  497. {
  498. AddString(value.ToString());
  499. }
  500. void JSONValue::AddVariant(const Variant& value)
  501. {
  502. // Create child object for variant
  503. JSONValue child = CreateChild(JSON_OBJECT);
  504. // Set type
  505. child.SetString("type", value.GetTypeName());
  506. // Set value
  507. child.SetVariantValue("value", value);
  508. }
  509. void JSONValue::AddVariantValue(const Variant& value)
  510. {
  511. switch (value.GetType())
  512. {
  513. case VAR_RESOURCEREF:
  514. AddResourceRef(value.GetResourceRef());
  515. break;
  516. case VAR_RESOURCEREFLIST:
  517. AddResourceRefList(value.GetResourceRefList());
  518. break;
  519. case VAR_VARIANTVECTOR:
  520. case VAR_VARIANTMAP:
  521. LOGERROR("Unsupported value type");
  522. break;
  523. default:
  524. AddString(value.ToString());
  525. }
  526. }
  527. unsigned JSONValue::GetSize() const
  528. {
  529. if (IsArray())
  530. return (unsigned)value_->Size();
  531. else
  532. return 0;
  533. }
  534. bool JSONValue::IsArray() const
  535. {
  536. return value_ && value_->IsArray();
  537. }
  538. int JSONValue::GetInt(unsigned index) const
  539. {
  540. return GetMember(index).GetInt();
  541. }
  542. bool JSONValue::GetBool(unsigned index) const
  543. {
  544. return GetMember(index).GetBool();
  545. }
  546. float JSONValue::GetFloat(unsigned index) const
  547. {
  548. return (float)GetMember(index).GetDouble();
  549. }
  550. Vector2 JSONValue::GetVector2(unsigned index) const
  551. {
  552. return ToVector2(GetCString(index));
  553. }
  554. Vector3 JSONValue::GetVector3(unsigned index) const
  555. {
  556. return ToVector3(GetCString(index));
  557. }
  558. Vector4 JSONValue::GetVector4(unsigned index) const
  559. {
  560. return ToVector4(GetCString(index));
  561. }
  562. Variant JSONValue::GetVectorVariant(unsigned index) const
  563. {
  564. return ToVectorVariant(GetCString(index));
  565. }
  566. Quaternion JSONValue::GetQuaternion(unsigned index) const
  567. {
  568. return ToQuaternion(GetCString(index));
  569. }
  570. Color JSONValue::GetColor(unsigned index) const
  571. {
  572. return ToColor(GetCString(index));
  573. }
  574. String JSONValue::GetString(unsigned index) const
  575. {
  576. return GetMember(index).GetString();
  577. }
  578. const char* JSONValue::GetCString(unsigned index) const
  579. {
  580. return GetMember(index).GetString();
  581. }
  582. PODVector<unsigned char> JSONValue::GetBuffer(unsigned index) const
  583. {
  584. PODVector<unsigned char> buffer;
  585. StringToBuffer(buffer, GetCString(index));
  586. return buffer;
  587. }
  588. bool JSONValue::GetBuffer(unsigned index, void* dest, unsigned size) const
  589. {
  590. Vector<String> bytes = GetString(index).Split(' ');
  591. unsigned char* destBytes = (unsigned char*)dest;
  592. if (size < bytes.Size())
  593. return false;
  594. for (unsigned i = 0; i < bytes.Size(); ++i)
  595. destBytes[i] = ToInt(bytes[i]);
  596. return true;
  597. }
  598. ResourceRef JSONValue::GetResourceRef(unsigned index) const
  599. {
  600. ResourceRef ret;
  601. Vector<String> values = GetString(index).Split(';');
  602. if (values.Size() == 2)
  603. {
  604. ret.type_ = values[0];
  605. ret.name_ = values[1];
  606. }
  607. return ret;
  608. }
  609. ResourceRefList JSONValue::GetResourceRefList(unsigned index) const
  610. {
  611. ResourceRefList ret;
  612. Vector<String> values = GetString(index).Split(';');
  613. if (values.Size() >= 1)
  614. {
  615. ret.type_ = values[0];
  616. ret.names_.Resize(values.Size() - 1);
  617. for (unsigned i = 1; i < values.Size(); ++i)
  618. ret.names_[i - 1] = values[i];
  619. }
  620. return ret;
  621. }
  622. IntRect JSONValue::GetIntRect(unsigned index) const
  623. {
  624. return ToIntRect(GetCString(index));
  625. }
  626. IntVector2 JSONValue::GetIntVector2(unsigned index) const
  627. {
  628. return ToIntVector2(GetCString(index));
  629. }
  630. Matrix3 JSONValue::GetMatrix3(unsigned index) const
  631. {
  632. return ToMatrix3(GetCString(index));
  633. }
  634. Matrix3x4 JSONValue::GetMatrix3x4(unsigned index) const
  635. {
  636. return ToMatrix3x4(GetCString(index));
  637. }
  638. Matrix4 JSONValue::GetMatrix4(unsigned index) const
  639. {
  640. return ToMatrix4(GetCString(index));
  641. }
  642. Variant JSONValue::GetVariant(unsigned index) const
  643. {
  644. // Get child for variant
  645. JSONValue child = GetChild(index, JSON_OBJECT);
  646. if (child.IsNull())
  647. return Variant::EMPTY;
  648. // Get type
  649. VariantType type = Variant::GetTypeFromName(child.GetString("type"));
  650. // Get value
  651. return child.GetVariantValue("value", type);
  652. }
  653. Variant JSONValue::GetVariantValue(unsigned index, VariantType type) const
  654. {
  655. Variant ret;
  656. if (type == VAR_RESOURCEREF)
  657. ret = GetResourceRef(index);
  658. else if (type == VAR_RESOURCEREFLIST)
  659. ret = GetResourceRefList(index);
  660. else if (type == VAR_VARIANTVECTOR || type == VAR_VARIANTMAP)
  661. LOGERROR("Unsupported value type");
  662. else
  663. ret.FromString(type, GetCString(index));
  664. return ret;
  665. }
  666. void JSONValue::AddMember(const String& name, rapidjson::Value& jsonValue)
  667. {
  668. if (!IsObject())
  669. return;
  670. Value jsonName;
  671. jsonName.SetString(name.CString(), name.Length(), file_->GetDocument()->GetAllocator());
  672. value_->AddMember(jsonName, jsonValue, file_->GetDocument()->GetAllocator());
  673. }
  674. rapidjson::Value& JSONValue::GetMember(const String& name) const
  675. {
  676. assert(IsObject());
  677. return (*value_)[name.CString()];
  678. }
  679. void JSONValue::AddMember(rapidjson::Value& jsonValue)
  680. {
  681. assert(IsArray());
  682. value_->PushBack(jsonValue, file_->GetDocument()->GetAllocator());
  683. }
  684. rapidjson::Value& JSONValue::GetMember(unsigned index) const
  685. {
  686. assert(IsArray());
  687. return (*value_)[(SizeType)index];
  688. }
  689. }