JSONValue.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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:
  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. #pragma once
  23. #include <Atomic/Core/Variant.h>
  24. namespace Atomic
  25. {
  26. class Context;
  27. /// JSON value type.
  28. enum JSONValueType
  29. {
  30. /// JSON null type.
  31. JSON_NULL = 0,
  32. /// JSON boolean type.
  33. JSON_BOOL,
  34. /// JSON number type.
  35. JSON_NUMBER,
  36. /// JSON string type.
  37. JSON_STRING,
  38. /// JSON array type.
  39. JSON_ARRAY,
  40. /// JSON object type.
  41. JSON_OBJECT,
  42. };
  43. /// JSON number type.
  44. enum JSONNumberType
  45. {
  46. /// Not a number.
  47. JSONNT_NAN = 0,
  48. /// Integer.
  49. JSONNT_INT,
  50. /// Unsigned integer.
  51. JSONNT_UINT,
  52. /// Float or double.
  53. JSONNT_FLOAT_DOUBLE,
  54. };
  55. class JSONValue;
  56. /// JSON array type.
  57. typedef Vector<JSONValue> JSONArray;
  58. /// JSON object type.
  59. typedef HashMap<String, JSONValue> JSONObject;
  60. /// JSON object iterator.
  61. typedef JSONObject::Iterator JSONObjectIterator;
  62. /// Constant JSON object iterator.
  63. typedef JSONObject::ConstIterator ConstJSONObjectIterator;
  64. /// JSON value class.
  65. class ATOMIC_API JSONValue
  66. {
  67. public:
  68. /// Construct null value.
  69. JSONValue() :
  70. type_(0)
  71. {
  72. }
  73. /// Construct with a boolean.
  74. JSONValue(bool value) :
  75. type_(0)
  76. {
  77. *this = value;
  78. }
  79. /// Construct with a integer.
  80. JSONValue(int value) :
  81. type_(0)
  82. {
  83. *this = value;
  84. }
  85. /// Construct with a unsigned integer.
  86. JSONValue(unsigned value) :
  87. type_(0)
  88. {
  89. *this = value;
  90. }
  91. /// Construct with a float.
  92. JSONValue(float value) :
  93. type_(0)
  94. {
  95. *this = value;
  96. }
  97. /// Construct with a double.
  98. JSONValue(double value) :
  99. type_(0)
  100. {
  101. *this = value;
  102. }
  103. /// Construct with a string.
  104. JSONValue(const String& value) :
  105. type_(0)
  106. {
  107. *this = value;
  108. }
  109. /// Construct with a C string.
  110. JSONValue(const char* value) :
  111. type_(0)
  112. {
  113. *this = value;
  114. }
  115. /// Construct with a JSON array.
  116. JSONValue(const JSONArray& value) :
  117. type_(0)
  118. {
  119. *this = value;
  120. }
  121. /// Construct with a JSON object.
  122. JSONValue(const JSONObject& value) :
  123. type_(0)
  124. {
  125. *this = value;
  126. }
  127. /// Copy-construct from another JSON value.
  128. JSONValue(const JSONValue& value) :
  129. type_(0)
  130. {
  131. *this = value;
  132. }
  133. /// Destruct.
  134. ~JSONValue()
  135. {
  136. SetType(JSON_NULL);
  137. }
  138. /// Assign from a boolean.
  139. JSONValue& operator =(bool rhs);
  140. /// Assign from an integer.
  141. JSONValue& operator =(int rhs);
  142. /// Assign from an unsigned integer.
  143. JSONValue& operator =(unsigned rhs);
  144. /// Assign from a float.
  145. JSONValue& operator =(float rhs);
  146. /// Assign from a double.
  147. JSONValue& operator =(double rhs);
  148. /// Assign from a string.
  149. JSONValue& operator =(const String& rhs);
  150. /// Assign from a C string.
  151. JSONValue& operator =(const char* rhs);
  152. /// Assign from a JSON array.
  153. JSONValue& operator =(const JSONArray& rhs);
  154. /// Assign from a JSON object.
  155. JSONValue& operator =(const JSONObject& rhs);
  156. /// Assign from another JSON value.
  157. JSONValue& operator =(const JSONValue& rhs);
  158. /// Return value type.
  159. JSONValueType GetValueType() const;
  160. /// Return number type.
  161. JSONNumberType GetNumberType() const;
  162. /// Check is null.
  163. bool IsNull() const { return GetValueType() == JSON_NULL; }
  164. /// Check is boolean.
  165. bool IsBool() const { return GetValueType() == JSON_BOOL; }
  166. /// Check is number.
  167. bool IsNumber() const { return GetValueType() == JSON_NUMBER; }
  168. /// Check is string.
  169. bool IsString() const { return GetValueType() == JSON_STRING; }
  170. /// Check is array.
  171. bool IsArray() const { return GetValueType() == JSON_ARRAY; }
  172. /// Check is object.
  173. bool IsObject() const { return GetValueType() == JSON_OBJECT; }
  174. /// Return boolean value.
  175. bool GetBool() const { return IsBool() ? boolValue_ : false;}
  176. /// Return integer value.
  177. int GetInt() const { return IsNumber() ? (int)numberValue_ : 0; }
  178. /// Return unsigned integer value.
  179. unsigned GetUInt() const { return IsNumber() ? (unsigned)numberValue_ : 0; }
  180. /// Return float value.
  181. float GetFloat() const { return IsNumber() ? (float)numberValue_ : 0.0f; }
  182. /// Return double value.
  183. double GetDouble() const { return IsNumber() ? numberValue_ : 0.0; }
  184. /// Return string value.
  185. const String& GetString() const { return IsString() ? *stringValue_ : String::EMPTY;}
  186. /// Return C string value.
  187. const char* GetCString() const { return IsString() ? stringValue_->CString() : 0;}
  188. /// Return JSON array value.
  189. const JSONArray& GetArray() const { return IsArray() ? *arrayValue_ : emptyArray; }
  190. /// Return JSON object value.
  191. const JSONObject& GetObject() const { return IsObject() ? *objectValue_ : emptyObject; }
  192. // JSON array functions
  193. /// Return JSON value at index.
  194. JSONValue& operator [](unsigned index);
  195. /// Return JSON value at index.
  196. const JSONValue& operator [](unsigned index) const;
  197. /// Add JSON value at end.
  198. void Push(const JSONValue& value);
  199. /// Remove the last JSON value.
  200. void Pop();
  201. /// Insert an JSON value at position.
  202. void Insert(unsigned pos, const JSONValue& value);
  203. /// Erase a range of JSON values.
  204. void Erase(unsigned pos, unsigned length = 1);
  205. /// Resize array.
  206. void Resize(unsigned newSize);
  207. /// Return size of array.
  208. unsigned Size() const;
  209. // JSON object functions
  210. /// Return JSON value with key.
  211. JSONValue& operator [](const String& key);
  212. /// Return JSON value with key.
  213. const JSONValue& operator [](const String& key) const;
  214. /// Set JSON value with key.
  215. void Set(const String& key, const JSONValue& value);
  216. /// Return JSON value with key.
  217. const JSONValue& Get(const String& key) const;
  218. /// Erase a pair by key.
  219. bool Erase(const String& key);
  220. /// Return whether contains a pair with key.
  221. bool Contains(const String& key) const;
  222. /// Return iterator to the beginning.
  223. JSONObjectIterator Begin();
  224. /// Return iterator to the beginning.
  225. ConstJSONObjectIterator Begin() const;
  226. /// Return iterator to the end.
  227. JSONObjectIterator End();
  228. /// Return iterator to the beginning.
  229. ConstJSONObjectIterator End() const;
  230. /// Clear array or object.
  231. void Clear();
  232. /// Internal functions.
  233. void SetType(JSONValueType valueType, JSONNumberType numberType = JSONNT_NAN);
  234. void SetVariant(const Variant& variant, Context* context = 0);
  235. void GetVariant(Variant& variant) const;
  236. void SetVariantValue(const Variant& variant, Context* context = 0);
  237. void GetVariantValue(Variant& variant, VariantType type) const;
  238. void SetVariantMap(const VariantMap& variantMap, Context* context = 0);
  239. void GetVariantMap(VariantMap& variantMap) const;
  240. void SetVariantVector(const VariantVector& variantVector, Context* context = 0);
  241. void GetVariantVector(VariantVector& variantVector) const;
  242. /// Empty JSON value.
  243. static const JSONValue EMPTY;
  244. /// Empty JSON array.
  245. static const JSONArray emptyArray;
  246. /// Empty JSON object.
  247. static const JSONObject emptyObject;
  248. private:
  249. /// type.
  250. unsigned type_;
  251. union
  252. {
  253. /// Boolean value.
  254. bool boolValue_;
  255. /// Number value.
  256. double numberValue_;
  257. /// String value.
  258. String* stringValue_;
  259. /// Array value.
  260. JSONArray* arrayValue_;
  261. /// Object value.
  262. JSONObject* objectValue_;
  263. };
  264. };
  265. }