JSONValue.cpp 21 KB

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