JSONValue.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. //
  2. // Copyright (c) 2008-2016 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 "../Core/StringUtils.h"
  25. #include "../IO/Log.h"
  26. #include "../Resource/JSONValue.h"
  27. #include "../DebugNew.h"
  28. namespace Urho3D
  29. {
  30. static const char* valueTypeNames[] =
  31. {
  32. "Null",
  33. "Bool",
  34. "Number",
  35. "String",
  36. "Array",
  37. "Object",
  38. 0
  39. };
  40. static const char* numberTypeNames[] =
  41. {
  42. "NaN",
  43. "Int",
  44. "Unsigned",
  45. "Real",
  46. 0
  47. };
  48. const JSONValue JSONValue::EMPTY;
  49. const JSONArray JSONValue::emptyArray;
  50. const JSONObject JSONValue::emptyObject;
  51. JSONValue& JSONValue::operator =(bool rhs)
  52. {
  53. SetType(JSON_BOOL);
  54. boolValue_ = rhs;
  55. return *this;
  56. }
  57. JSONValue& JSONValue::operator =(int rhs)
  58. {
  59. SetType(JSON_NUMBER, JSONNT_INT);
  60. numberValue_ = rhs;
  61. return *this;
  62. }
  63. JSONValue& JSONValue::operator =(unsigned rhs)
  64. {
  65. SetType(JSON_NUMBER, JSONNT_UINT);
  66. numberValue_ = rhs;
  67. return *this;
  68. }
  69. JSONValue& JSONValue::operator =(float rhs)
  70. {
  71. SetType(JSON_NUMBER, JSONNT_FLOAT_DOUBLE);
  72. numberValue_ = rhs;
  73. return *this;
  74. }
  75. JSONValue& JSONValue::operator =(double rhs)
  76. {
  77. SetType(JSON_NUMBER, JSONNT_FLOAT_DOUBLE);
  78. numberValue_ = rhs;
  79. return *this;
  80. }
  81. JSONValue& JSONValue::operator =(const String& rhs)
  82. {
  83. SetType(JSON_STRING);
  84. *stringValue_ = rhs;
  85. return *this;
  86. }
  87. JSONValue& JSONValue::operator =(const char* rhs)
  88. {
  89. SetType(JSON_STRING);
  90. *stringValue_ = rhs;
  91. return *this;
  92. }
  93. JSONValue& JSONValue::operator =(const JSONArray& rhs)
  94. {
  95. SetType(JSON_ARRAY);
  96. *arrayValue_ = rhs;
  97. return *this;
  98. }
  99. JSONValue& JSONValue::operator =(const JSONObject& rhs)
  100. {
  101. SetType(JSON_OBJECT);
  102. *objectValue_ = rhs;
  103. return *this;
  104. }
  105. JSONValue& JSONValue::operator =(const JSONValue& rhs)
  106. {
  107. if (this == &rhs)
  108. return *this;
  109. SetType(rhs.GetValueType(), rhs.GetNumberType());
  110. switch (GetValueType())
  111. {
  112. case JSON_BOOL:
  113. boolValue_ = rhs.boolValue_;
  114. break;
  115. case JSON_NUMBER:
  116. numberValue_ = rhs.numberValue_;
  117. break;
  118. case JSON_STRING:
  119. *stringValue_ = *rhs.stringValue_;
  120. break;
  121. case JSON_ARRAY:
  122. *arrayValue_ = *rhs.arrayValue_;
  123. break;
  124. case JSON_OBJECT:
  125. *objectValue_ = *rhs.objectValue_;
  126. default:
  127. break;
  128. }
  129. return *this;
  130. }
  131. JSONValueType JSONValue::GetValueType() const
  132. {
  133. return (JSONValueType)(type_ >> 16);
  134. }
  135. JSONNumberType JSONValue::GetNumberType() const
  136. {
  137. return (JSONNumberType)(type_ & 0xffff);
  138. }
  139. String JSONValue::GetValueTypeName() const
  140. {
  141. return GetValueTypeName(GetValueType());
  142. }
  143. String JSONValue::GetNumberTypeName() const
  144. {
  145. return GetNumberTypeName(GetNumberType());
  146. }
  147. JSONValue& JSONValue::operator [](unsigned index)
  148. {
  149. // Convert to array type
  150. SetType(JSON_ARRAY);
  151. return (*arrayValue_)[index];
  152. }
  153. const JSONValue& JSONValue::operator [](unsigned index) const
  154. {
  155. if (GetValueType() != JSON_ARRAY)
  156. return EMPTY;
  157. return (*arrayValue_)[index];
  158. }
  159. void JSONValue::Push(const JSONValue& value)
  160. {
  161. // Convert to array type
  162. SetType(JSON_ARRAY);
  163. arrayValue_->Push(value);
  164. }
  165. void JSONValue::Pop()
  166. {
  167. if (GetValueType() != JSON_ARRAY)
  168. return;
  169. arrayValue_->Pop();
  170. }
  171. void JSONValue::Insert(unsigned pos, const JSONValue& value)
  172. {
  173. if (GetValueType() != JSON_ARRAY)
  174. return;
  175. arrayValue_->Insert(pos, value);
  176. }
  177. void JSONValue::Erase(unsigned pos, unsigned length)
  178. {
  179. if (GetValueType() != JSON_ARRAY)
  180. return;
  181. arrayValue_->Erase(pos, length);
  182. }
  183. void JSONValue::Resize(unsigned newSize)
  184. {
  185. // Convert to array type
  186. SetType(JSON_ARRAY);
  187. arrayValue_->Resize(newSize);
  188. }
  189. unsigned JSONValue::Size() const
  190. {
  191. if (GetValueType() == JSON_ARRAY)
  192. return arrayValue_->Size();
  193. return 0;
  194. }
  195. JSONValue& JSONValue::operator [](const String& key)
  196. {
  197. // Convert to object type
  198. SetType(JSON_OBJECT);
  199. return (*objectValue_)[key];
  200. }
  201. const JSONValue& JSONValue::operator [](const String& key) const
  202. {
  203. if (GetValueType() != JSON_OBJECT)
  204. return EMPTY;
  205. return (*objectValue_)[key];
  206. }
  207. void JSONValue::Set(const String& key, const JSONValue& value)
  208. {
  209. // Convert to object type
  210. SetType(JSON_OBJECT);
  211. (*objectValue_)[key] = value;
  212. }
  213. const JSONValue& JSONValue::Get(const String& key) const
  214. {
  215. if (GetValueType() != JSON_OBJECT)
  216. return EMPTY;
  217. JSONObject::ConstIterator i = objectValue_->Find(key);
  218. if (i == objectValue_->End())
  219. return EMPTY;
  220. return i->second_;
  221. }
  222. bool JSONValue::Erase(const String& key)
  223. {
  224. if (GetValueType() != JSON_OBJECT)
  225. return false;
  226. return objectValue_->Erase(key);
  227. }
  228. bool JSONValue::Contains(const String& key) const
  229. {
  230. if (GetValueType() != JSON_OBJECT)
  231. return false;
  232. return objectValue_->Contains(key);
  233. }
  234. JSONObjectIterator JSONValue::Begin()
  235. {
  236. // Convert to object type.
  237. SetType(JSON_OBJECT);
  238. return objectValue_->Begin();
  239. }
  240. ConstJSONObjectIterator JSONValue::Begin() const
  241. {
  242. if (GetValueType() != JSON_OBJECT)
  243. return emptyObject.Begin();
  244. return objectValue_->Begin();
  245. }
  246. JSONObjectIterator JSONValue::End()
  247. {
  248. // Convert to object type.
  249. SetType(JSON_OBJECT);
  250. return objectValue_->Begin();
  251. }
  252. ConstJSONObjectIterator JSONValue::End() const
  253. {
  254. if (GetValueType() != JSON_OBJECT)
  255. return emptyObject.End();
  256. return objectValue_->End();
  257. }
  258. void JSONValue::Clear()
  259. {
  260. if (GetValueType() == JSON_ARRAY)
  261. arrayValue_->Clear();
  262. else if (GetValueType() == JSON_OBJECT)
  263. objectValue_->Clear();
  264. }
  265. void JSONValue::SetType(JSONValueType valueType, JSONNumberType numberType)
  266. {
  267. int type = (valueType << 16) | numberType;
  268. if (type == type_)
  269. return;
  270. switch (GetValueType())
  271. {
  272. case JSON_STRING:
  273. delete stringValue_;
  274. break;
  275. case JSON_ARRAY:
  276. delete arrayValue_;
  277. break;
  278. case JSON_OBJECT:
  279. delete objectValue_;
  280. break;
  281. default:
  282. break;
  283. }
  284. type_ = type;
  285. switch (GetValueType())
  286. {
  287. case JSON_STRING:
  288. stringValue_ = new String();
  289. break;
  290. case JSON_ARRAY:
  291. arrayValue_ = new JSONArray();
  292. break;
  293. case JSON_OBJECT:
  294. objectValue_ = new JSONObject();
  295. break;
  296. default:
  297. break;
  298. }
  299. }
  300. void JSONValue::SetVariant(const Variant& variant, Context* context)
  301. {
  302. if (!IsNull())
  303. {
  304. URHO3D_LOGWARNING("JsonValue is not null");
  305. }
  306. (*this)["type"] = variant.GetTypeName();
  307. (*this)["value"].SetVariantValue(variant, context);
  308. }
  309. Variant JSONValue::GetVariant() const
  310. {
  311. VariantType type = Variant::GetTypeFromName((*this)["type"].GetString());
  312. return (*this)["value"].GetVariantValue(type);
  313. }
  314. void JSONValue::SetVariantValue(const Variant& variant, Context* context)
  315. {
  316. if (!IsNull())
  317. {
  318. URHO3D_LOGWARNING("JsonValue is not null");
  319. }
  320. switch (variant.GetType())
  321. {
  322. case VAR_BOOL:
  323. *this = variant.GetBool();
  324. return;
  325. case VAR_INT:
  326. *this = variant.GetInt();
  327. return;
  328. case VAR_FLOAT:
  329. *this = variant.GetFloat();
  330. return;
  331. case VAR_DOUBLE:
  332. *this = variant.GetDouble();
  333. return;
  334. case VAR_STRING:
  335. *this = variant.GetString();
  336. return;
  337. case VAR_VARIANTVECTOR:
  338. SetVariantVector(variant.GetVariantVector(), context);
  339. return;
  340. case VAR_VARIANTMAP:
  341. SetVariantMap(variant.GetVariantMap(), context);
  342. return;
  343. case VAR_RESOURCEREF:
  344. {
  345. if (!context)
  346. {
  347. URHO3D_LOGERROR("Context must not be null for ResourceRef");
  348. return;
  349. }
  350. const ResourceRef& ref = variant.GetResourceRef();
  351. *this = String(context->GetTypeName(ref.type_)) + ";" + ref.name_;
  352. }
  353. return;
  354. case VAR_RESOURCEREFLIST:
  355. {
  356. if (!context)
  357. {
  358. URHO3D_LOGERROR("Context must not be null for ResourceRefList");
  359. return;
  360. }
  361. const ResourceRefList& refList = variant.GetResourceRefList();
  362. String str(context->GetTypeName(refList.type_));
  363. for (unsigned i = 0; i < refList.names_.Size(); ++i)
  364. {
  365. str += ";";
  366. str += refList.names_[i];
  367. }
  368. *this = str;
  369. }
  370. return;
  371. case VAR_STRINGVECTOR:
  372. {
  373. const StringVector& vector = variant.GetStringVector();
  374. Resize(vector.Size());
  375. for (unsigned i = 0; i < vector.Size(); ++i)
  376. (*this)[i] = vector[i];
  377. }
  378. return;
  379. default:
  380. *this = variant.ToString();
  381. }
  382. }
  383. Variant JSONValue::GetVariantValue(VariantType type) const
  384. {
  385. Variant variant;
  386. switch (type)
  387. {
  388. case VAR_BOOL:
  389. variant = GetBool();
  390. break;
  391. case VAR_INT:
  392. variant = GetInt();
  393. break;
  394. case VAR_FLOAT:
  395. variant = GetFloat();
  396. break;
  397. case VAR_DOUBLE:
  398. variant = GetDouble();
  399. break;
  400. case VAR_STRING:
  401. variant = GetString();
  402. break;
  403. case VAR_VARIANTVECTOR:
  404. variant = GetVariantVector();
  405. break;
  406. case VAR_VARIANTMAP:
  407. variant = GetVariantMap();
  408. break;
  409. case VAR_RESOURCEREF:
  410. {
  411. ResourceRef ref;
  412. Vector<String> values = GetString().Split(';');
  413. if (values.Size() == 2)
  414. {
  415. ref.type_ = values[0];
  416. ref.name_ = values[1];
  417. }
  418. variant = ref;
  419. }
  420. break;
  421. case VAR_RESOURCEREFLIST:
  422. {
  423. ResourceRefList refList;
  424. Vector<String> values = GetString().Split(';', true);
  425. if (values.Size() >= 1)
  426. {
  427. refList.type_ = values[0];
  428. refList.names_.Resize(values.Size() - 1);
  429. for (unsigned i = 1; i < values.Size(); ++i)
  430. refList.names_[i - 1] = values[i];
  431. }
  432. variant = refList;
  433. }
  434. break;
  435. case VAR_STRINGVECTOR:
  436. {
  437. StringVector vector;
  438. for (unsigned i = 0; i < Size(); ++i)
  439. vector.Push((*this)[i].GetString());
  440. variant = vector;
  441. }
  442. break;
  443. default:
  444. variant.FromString(type, GetString());
  445. }
  446. return variant;
  447. }
  448. void JSONValue::SetVariantMap(const VariantMap& variantMap, Context* context)
  449. {
  450. SetType(JSON_OBJECT);
  451. for (VariantMap::ConstIterator i = variantMap.Begin(); i != variantMap.End(); ++i)
  452. (*this)[i->first_.ToString()].SetVariant(i->second_);
  453. }
  454. VariantMap JSONValue::GetVariantMap() const
  455. {
  456. VariantMap variantMap;
  457. if (!IsObject())
  458. {
  459. URHO3D_LOGERROR("JSONValue is not a object");
  460. return variantMap;
  461. }
  462. for (ConstJSONObjectIterator i = Begin(); i != End(); ++i)
  463. {
  464. StringHash key(ToUInt(i->first_));
  465. Variant variant = i->second_.GetVariant();
  466. variantMap[key] = variant;
  467. }
  468. return variantMap;
  469. }
  470. void JSONValue::SetVariantVector(const VariantVector& variantVector, Context* context)
  471. {
  472. SetType(JSON_ARRAY);
  473. arrayValue_->Reserve(variantVector.Size());
  474. for (unsigned i = 0; i < variantVector.Size(); ++i)
  475. {
  476. JSONValue val;
  477. val.SetVariant(variantVector[i], context);
  478. arrayValue_->Push(val);
  479. }
  480. }
  481. VariantVector JSONValue::GetVariantVector() const
  482. {
  483. VariantVector variantVector;
  484. if (!IsArray())
  485. {
  486. URHO3D_LOGERROR("JSONValue is not a array");
  487. return variantVector;
  488. }
  489. for (unsigned i = 0; i < Size(); ++i)
  490. {
  491. Variant variant = (*this)[i].GetVariant();
  492. variantVector.Push(variant);
  493. }
  494. return variantVector;
  495. }
  496. String JSONValue::GetValueTypeName(JSONValueType type)
  497. {
  498. return valueTypeNames[type];
  499. }
  500. String JSONValue::GetNumberTypeName(JSONNumberType type)
  501. {
  502. return numberTypeNames[type];
  503. }
  504. JSONValueType JSONValue::GetValueTypeFromName(const String& typeName)
  505. {
  506. return GetValueTypeFromName(typeName.CString());
  507. }
  508. JSONValueType JSONValue::GetValueTypeFromName(const char* typeName)
  509. {
  510. return (JSONValueType)GetStringListIndex(typeName, valueTypeNames, JSON_NULL);
  511. }
  512. JSONNumberType JSONValue::GetNumberTypeFromName(const String& typeName)
  513. {
  514. return GetNumberTypeFromName(typeName.CString());
  515. }
  516. JSONNumberType JSONValue::GetNumberTypeFromName(const char* typeName)
  517. {
  518. return (JSONNumberType)GetStringListIndex(typeName, numberTypeNames, JSONNT_NAN);
  519. }
  520. }