JSONValue.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 "../Precompiled.h"
  23. #include "../Core/Context.h"
  24. #include "../IO/Log.h"
  25. #include "../Resource/JSONValue.h"
  26. #include "../DebugNew.h"
  27. namespace Urho3D
  28. {
  29. const JSONValue JSONValue::EMPTY;
  30. const JSONArray JSONValue::EMPTY_ARRAY;
  31. const JSONObject JSONValue::EMPTY_OBJECT;
  32. void JSONValue::SetType(JSONValueType valueType)
  33. {
  34. if (valueType == valueType_)
  35. return;
  36. switch (valueType_)
  37. {
  38. case JSON_STRING:
  39. delete stringValue_;
  40. break;
  41. case JSON_ARRAY:
  42. delete arrayValue_;
  43. break;
  44. case JSON_OBJECT:
  45. delete objectValue_;
  46. break;
  47. default:
  48. break;
  49. }
  50. valueType_ = valueType;
  51. switch (valueType_)
  52. {
  53. case JSON_STRING:
  54. stringValue_ = new String();
  55. break;
  56. case JSON_ARRAY:
  57. arrayValue_ = new JSONArray();
  58. break;
  59. case JSON_OBJECT:
  60. objectValue_ = new JSONObject();
  61. break;
  62. default:
  63. break;
  64. }
  65. }
  66. JSONValue& JSONValue::operator =(bool rhs)
  67. {
  68. SetType(JSON_NUMBER);
  69. numberValue_ = rhs;
  70. return *this;
  71. }
  72. JSONValue& JSONValue::operator =(int rhs)
  73. {
  74. SetType(JSON_NUMBER);
  75. numberValue_ = rhs;
  76. return *this;
  77. }
  78. JSONValue& JSONValue::operator =(unsigned rhs)
  79. {
  80. SetType(JSON_NUMBER);
  81. numberValue_ = rhs;
  82. return *this;
  83. }
  84. JSONValue& JSONValue::operator =(float rhs)
  85. {
  86. SetType(JSON_NUMBER);
  87. numberValue_ = rhs;
  88. return *this;
  89. }
  90. JSONValue& JSONValue::operator =(double rhs)
  91. {
  92. SetType(JSON_NUMBER);
  93. numberValue_ = rhs;
  94. return *this;
  95. }
  96. JSONValue& JSONValue::operator =(const String& rhs)
  97. {
  98. SetType(JSON_STRING);
  99. *stringValue_ = rhs;
  100. return *this;
  101. }
  102. JSONValue& JSONValue::operator =(const char* rhs)
  103. {
  104. SetType(JSON_STRING);
  105. *stringValue_ = rhs;
  106. return *this;
  107. }
  108. JSONValue& JSONValue::operator =(const JSONArray& rhs)
  109. {
  110. SetType(JSON_ARRAY);
  111. *arrayValue_ = rhs;
  112. return *this;
  113. }
  114. JSONValue& JSONValue::operator =(const JSONObject& rhs)
  115. {
  116. SetType(JSON_OBJECT);
  117. *objectValue_ = rhs;
  118. return *this;
  119. }
  120. JSONValue& JSONValue::operator =(const JSONValue& rhs)
  121. {
  122. if (this == &rhs)
  123. return *this;
  124. SetType(rhs.valueType_);
  125. switch (valueType_)
  126. {
  127. case JSON_BOOL:
  128. boolValue_ = rhs.boolValue_;
  129. break;
  130. case JSON_NUMBER:
  131. numberValue_ = rhs.numberValue_;
  132. break;
  133. case JSON_STRING:
  134. *stringValue_ = *rhs.stringValue_;
  135. break;
  136. case JSON_ARRAY:
  137. *arrayValue_ = *rhs.arrayValue_;
  138. break;
  139. case JSON_OBJECT:
  140. *objectValue_ = *rhs.objectValue_;
  141. default:
  142. break;
  143. }
  144. return *this;
  145. }
  146. JSONValue& JSONValue::operator [](unsigned index)
  147. {
  148. // Convert to array type
  149. SetType(JSON_ARRAY);
  150. return (*arrayValue_)[index];
  151. }
  152. const JSONValue& JSONValue::operator [](unsigned index) const
  153. {
  154. if (valueType_ != JSON_ARRAY)
  155. return EMPTY;
  156. return (*arrayValue_)[index];
  157. }
  158. JSONValue& JSONValue::At(unsigned index)
  159. {
  160. // Convert to array type
  161. SetType(JSON_ARRAY);
  162. return arrayValue_->At(index);
  163. }
  164. const JSONValue& JSONValue::At(unsigned index) const
  165. {
  166. if (valueType_ != JSON_ARRAY)
  167. return EMPTY;
  168. return arrayValue_->At(index);
  169. }
  170. void JSONValue::Push(const JSONValue& value)
  171. {
  172. // Convert to array type
  173. SetType(JSON_ARRAY);
  174. arrayValue_->Push(value);
  175. }
  176. void JSONValue::Pop()
  177. {
  178. if (valueType_ != JSON_ARRAY)
  179. return;
  180. arrayValue_->Pop();
  181. }
  182. void JSONValue::Insert(unsigned pos, const JSONValue& value)
  183. {
  184. if (valueType_ != JSON_ARRAY)
  185. return;
  186. arrayValue_->Insert(pos, value);
  187. }
  188. void JSONValue::Erase(unsigned pos, unsigned length)
  189. {
  190. if (valueType_ != JSON_ARRAY)
  191. return;
  192. arrayValue_->Erase(pos, length);
  193. }
  194. void JSONValue::Resize(unsigned newSize)
  195. {
  196. // Convert to array type
  197. SetType(JSON_ARRAY);
  198. arrayValue_->Resize(newSize);
  199. }
  200. unsigned JSONValue::Size() const
  201. {
  202. if (valueType_ == JSON_ARRAY)
  203. return arrayValue_->Size();
  204. return 0;
  205. }
  206. JSONValue& JSONValue::operator [](const String& key)
  207. {
  208. // Convert to object type
  209. SetType(JSON_OBJECT);
  210. return (*objectValue_)[key];
  211. }
  212. const JSONValue& JSONValue::operator [](const String& key) const
  213. {
  214. if (valueType_ != JSON_OBJECT)
  215. return EMPTY;
  216. return (*objectValue_)[key];
  217. }
  218. void JSONValue::Set(const String& key, const JSONValue& value)
  219. {
  220. // Convert to object type
  221. SetType(JSON_OBJECT);
  222. objectValue_->Insert(MakePair<String, JSONValue>(key, value));
  223. }
  224. const JSONValue& JSONValue::Get(const String& key) const
  225. {
  226. if (valueType_ != JSON_OBJECT)
  227. return EMPTY;
  228. JSONObject::ConstIterator i = objectValue_->Find(key);
  229. if (i == objectValue_->End())
  230. return EMPTY;
  231. return i->second_;
  232. }
  233. bool JSONValue::Erase(const String& key)
  234. {
  235. if (valueType_ != JSON_OBJECT)
  236. return false;
  237. return objectValue_->Erase(key);
  238. }
  239. bool JSONValue::Contains(const String& key) const
  240. {
  241. if (valueType_ != JSON_OBJECT)
  242. return false;
  243. return objectValue_->Contains(key);
  244. }
  245. void JSONValue::Clear()
  246. {
  247. if (valueType_ == JSON_ARRAY)
  248. arrayValue_->Clear();
  249. else if (valueType_ == JSON_OBJECT)
  250. objectValue_->Clear();
  251. }
  252. }