JSONValue.h 10 KB

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