JSONValue.cpp 21 KB

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