JSONValue.h 10.0 KB

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