JSONValue.cpp 20 KB

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