JSONValue.h 8.7 KB

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