JSONValue.cpp 21 KB

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