JSONValue.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. //
  2. // Copyright (c) 2008-2020 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. /// \file
  23. #pragma once
  24. #include "../Core/Variant.h"
  25. namespace Urho3D
  26. {
  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. using JSONArray = Vector<JSONValue>;
  58. /// JSON object type.
  59. using JSONObject = HashMap<String, JSONValue>;
  60. /// JSON object iterator.
  61. using JSONObjectIterator = JSONObject::Iterator;
  62. /// Constant JSON object iterator.
  63. using ConstJSONObjectIterator = JSONObject::ConstIterator;
  64. /// JSON value class.
  65. class URHO3D_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) : // NOLINT(google-explicit-constructor)
  75. type_(0)
  76. {
  77. *this = value;
  78. }
  79. /// Construct with a integer.
  80. JSONValue(int value) : // NOLINT(google-explicit-constructor)
  81. type_(0)
  82. {
  83. *this = value;
  84. }
  85. /// Construct with a unsigned integer.
  86. JSONValue(unsigned value) : // NOLINT(google-explicit-constructor)
  87. type_(0)
  88. {
  89. *this = value;
  90. }
  91. /// Construct with a float.
  92. JSONValue(float value) : // NOLINT(google-explicit-constructor)
  93. type_(0)
  94. {
  95. *this = value;
  96. }
  97. /// Construct with a double.
  98. JSONValue(double value) : // NOLINT(google-explicit-constructor)
  99. type_(0)
  100. {
  101. *this = value;
  102. }
  103. /// Construct with a string.
  104. JSONValue(const String& value) : // NOLINT(google-explicit-constructor)
  105. type_(0)
  106. {
  107. *this = value;
  108. }
  109. /// Construct with a C string.
  110. JSONValue(const char* value) : // NOLINT(google-explicit-constructor)
  111. type_(0)
  112. {
  113. *this = value;
  114. }
  115. /// Construct with a JSON array.
  116. JSONValue(const JSONArray& value) : // NOLINT(google-explicit-constructor)
  117. type_(0)
  118. {
  119. *this = value;
  120. }
  121. /// Construct with a JSON object.
  122. JSONValue(const JSONObject& value) : // NOLINT(google-explicit-constructor)
  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. /// @property
  160. JSONValueType GetValueType() const;
  161. /// Return number type.
  162. /// @property
  163. JSONNumberType GetNumberType() const;
  164. /// Return value type's name.
  165. /// @property
  166. String GetValueTypeName() const;
  167. /// Return number type's name.
  168. /// @property
  169. String GetNumberTypeName() const;
  170. /// Check is null.
  171. /// @property{get_isNull}
  172. bool IsNull() const { return GetValueType() == JSON_NULL; }
  173. /// Check is boolean.
  174. /// @property{get_isBool}
  175. bool IsBool() const { return GetValueType() == JSON_BOOL; }
  176. /// Check is number.
  177. /// @property{get_isNumber}
  178. bool IsNumber() const { return GetValueType() == JSON_NUMBER; }
  179. /// Check is string.
  180. /// @property{get_isString}
  181. bool IsString() const { return GetValueType() == JSON_STRING; }
  182. /// Check is array.
  183. /// @property{get_isArray}
  184. bool IsArray() const { return GetValueType() == JSON_ARRAY; }
  185. /// Check is object.
  186. /// @property{get_isObject}
  187. bool IsObject() const { return GetValueType() == JSON_OBJECT; }
  188. /// Return boolean value.
  189. bool GetBool(bool defaultValue = false) const { return IsBool() ? boolValue_ : defaultValue;}
  190. /// Return integer value.
  191. int GetInt(int defaultValue = 0) const { return IsNumber() ? (int)numberValue_ : defaultValue; }
  192. /// Return unsigned integer value.
  193. unsigned GetUInt(unsigned defaultValue = 0) const { return IsNumber() ? (unsigned)numberValue_ : defaultValue; }
  194. /// Return float value.
  195. float GetFloat(float defaultValue = 0.0f) const { return IsNumber() ? (float)numberValue_ : defaultValue; }
  196. /// Return double value.
  197. double GetDouble(double defaultValue = 0.0) const { return IsNumber() ? numberValue_ : defaultValue; }
  198. /// Return string value. The 'defaultValue' may potentially be returned as is, so it is the responsibility of the caller to ensure the 'defaultValue' remains valid while the return value is being referenced.
  199. const String& GetString(const String& defaultValue = String::EMPTY) const { return IsString() ? *stringValue_ : defaultValue;}
  200. /// Return C string value. Default to empty string literal.
  201. const char* GetCString(const char* defaultValue = "") const { return IsString() ? stringValue_->CString() : defaultValue;}
  202. /// Return JSON array value.
  203. const JSONArray& GetArray() const { return IsArray() ? *arrayValue_ : emptyArray; }
  204. /// Return JSON object value.
  205. const JSONObject& GetObject() const { return IsObject() ? *objectValue_ : emptyObject; }
  206. // JSON array functions
  207. /// Return JSON value at index.
  208. JSONValue& operator [](unsigned index);
  209. /// Return JSON value at index.
  210. const JSONValue& operator [](unsigned index) const;
  211. /// Add JSON value at end.
  212. void Push(const JSONValue& value);
  213. /// Remove the last JSON value.
  214. void Pop();
  215. /// Insert an JSON value at position.
  216. void Insert(unsigned pos, const JSONValue& value);
  217. /// Erase a range of JSON values.
  218. void Erase(unsigned pos, unsigned length = 1);
  219. /// Resize array.
  220. void Resize(unsigned newSize);
  221. /// Return size of array or number of keys in object.
  222. /// @property
  223. unsigned Size() const;
  224. // JSON object functions
  225. /// Return JSON value with key.
  226. JSONValue& operator [](const String& key);
  227. /// Return JSON value with key.
  228. const JSONValue& operator [](const String& key) const;
  229. /// Set JSON value with key.
  230. void Set(const String& key, const JSONValue& value);
  231. /// Return JSON value with key.
  232. const JSONValue& Get(const String& key) const;
  233. /// Erase a pair by key.
  234. bool Erase(const String& key);
  235. /// Return whether contains a pair with key.
  236. bool Contains(const String& key) const;
  237. /// Return iterator to the beginning.
  238. JSONObjectIterator Begin();
  239. /// Return iterator to the beginning.
  240. ConstJSONObjectIterator Begin() const;
  241. /// Return iterator to the end.
  242. JSONObjectIterator End();
  243. /// Return iterator to the beginning.
  244. ConstJSONObjectIterator End() const;
  245. /// Clear array or object.
  246. void Clear();
  247. /// Set value type and number type, internal function.
  248. void SetType(JSONValueType valueType, JSONNumberType numberType = JSONNT_NAN);
  249. /// Set variant, context must provide for resource ref.
  250. void SetVariant(const Variant& variant, Context* context = nullptr);
  251. /// Return a variant.
  252. Variant GetVariant() const;
  253. /// Set variant value, context must provide for resource ref.
  254. void SetVariantValue(const Variant& variant, Context* context = nullptr);
  255. /// Return a variant with type.
  256. Variant GetVariantValue(VariantType type) const;
  257. /// Set variant map, context must provide for resource ref.
  258. void SetVariantMap(const VariantMap& variantMap, Context* context = nullptr);
  259. /// Return a variant map.
  260. VariantMap GetVariantMap() const;
  261. /// Set variant vector, context must provide for resource ref.
  262. void SetVariantVector(const VariantVector& variantVector, Context* context = nullptr);
  263. /// Return a variant vector.
  264. VariantVector GetVariantVector() const;
  265. /// Empty JSON value.
  266. static const JSONValue EMPTY;
  267. /// Empty JSON array.
  268. static const JSONArray emptyArray;
  269. /// Empty JSON object.
  270. static const JSONObject emptyObject;
  271. /// Return name corresponding to a value type.
  272. static String GetValueTypeName(JSONValueType type);
  273. /// Return name corresponding to a number type.
  274. static String GetNumberTypeName(JSONNumberType type);
  275. /// Return a value type from name; null if unrecognized.
  276. static JSONValueType GetValueTypeFromName(const String& typeName);
  277. /// Return a value type from name; null if unrecognized.
  278. static JSONValueType GetValueTypeFromName(const char* typeName);
  279. /// Return a number type from name; NaN if unrecognized.
  280. static JSONNumberType GetNumberTypeFromName(const String& typeName);
  281. /// Return a value type from name; NaN if unrecognized.
  282. static JSONNumberType GetNumberTypeFromName(const char* typeName);
  283. private:
  284. /// type.
  285. unsigned type_;
  286. // https://github.com/doxygen/doxygen/issues/7623
  287. union
  288. {
  289. /// Boolean value.
  290. /// @nobind
  291. bool boolValue_;
  292. /// Number value.
  293. /// @nobind
  294. double numberValue_;
  295. /// String value.
  296. /// @nobind
  297. String* stringValue_;
  298. /// Array value.
  299. /// @nobind
  300. JSONArray* arrayValue_;
  301. /// Object value.
  302. /// @nobind
  303. JSONObject* objectValue_;
  304. };
  305. };
  306. }