JSONValue.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. //
  2. // Copyright (c) 2008-2014 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 "BoundingBox.h"
  24. #include "Rect.h"
  25. #include "Ptr.h"
  26. #include "Variant.h"
  27. namespace rapidjson
  28. {
  29. template<typename CharType> struct UTF8;
  30. class CrtAllocator;
  31. template <typename BaseAllocator> class MemoryPoolAllocator;
  32. template <typename Encoding, typename Allocator> class GenericValue;
  33. typedef GenericValue<UTF8<char>, MemoryPoolAllocator<CrtAllocator> > Value;
  34. }
  35. namespace Urho3D
  36. {
  37. class JSONFile;
  38. /// JSON value type.
  39. enum JSONValueType
  40. {
  41. /// Any type (use type in JSON value).
  42. JSON_ANY = 0,
  43. /// Object type (Hash Map).
  44. JSON_OBJECT,
  45. /// Array type.
  46. JSON_ARRAY
  47. };
  48. /// JSON value class.
  49. class URHO3D_API JSONValue
  50. {
  51. public:
  52. /// Construct null value.
  53. JSONValue();
  54. /// Construct with document and JSON value pointers.
  55. JSONValue(JSONFile* file, rapidjson::Value* value);
  56. /// Copy-construct from another value.
  57. JSONValue(const JSONValue& rhs);
  58. /// Destruct.
  59. ~JSONValue();
  60. /// Assignment operator.
  61. JSONValue& operator = (const JSONValue& rhs);
  62. /// Return whether does not refer to JSON value.
  63. bool IsNull() const;
  64. /// Return whether refers to JSON value.
  65. bool NotNull() const;
  66. /// Return true if refers to JSON value.
  67. operator bool () const;
  68. // JSON object value functions
  69. /// Create a child value.
  70. JSONValue CreateChild(const String& name, JSONValueType valueType = JSON_OBJECT);
  71. /// Return a child value by name. Return null if not exist.
  72. JSONValue GetChild(const String& name, JSONValueType valueType = JSON_ANY) const;
  73. /// Set int.
  74. void SetInt(const String& name, int value);
  75. /// Set bool.
  76. void SetBool(const String& name, bool value);
  77. /// Set float.
  78. void SetFloat(const String& name, float value);
  79. /// Set vector2.
  80. void SetVector2(const String& name, const Vector2& value);
  81. /// Set vector3.
  82. void SetVector3(const String& name, const Vector3& value);
  83. /// Set vector4.
  84. void SetVector4(const String& name, const Vector4& value);
  85. /// Set vector variant.
  86. void SetVectorVariant(const String& name, const Variant& value);
  87. /// Set quaternion.
  88. void SetQuaternion(const String& name, const Quaternion& value);
  89. /// Set color.
  90. void SetColor(const String& name, const Color& value);
  91. /// Set string.
  92. void SetString(const String& name, const String& value);
  93. /// Set buffer.
  94. void SetBuffer(const String& name, const void* data, unsigned size);
  95. /// Set buffer.
  96. void SetBuffer(const String& name, const PODVector<unsigned char>& value);
  97. /// Set resource ref.
  98. void SetResourceRef(const String& name, const ResourceRef& value);
  99. /// Set resource ref list.
  100. void SetResourceRefList(const String& name, const ResourceRefList& value);
  101. /// Set int rect.
  102. void SetIntRect(const String& name, const IntRect& value);
  103. /// Set int vector2.
  104. void SetIntVector2(const String& name, const IntVector2& value);
  105. /// Set matrix3.
  106. void SetMatrix3(const String& name, const Matrix3& value);
  107. /// Set matrix3x4.
  108. void SetMatrix3x4(const String& name, const Matrix3x4& value);
  109. /// Set matrix4.
  110. void SetMatrix4(const String& name, const Matrix4& value);
  111. /// Set variant (include type).
  112. void SetVariant(const String& name, const Variant& value);
  113. /// Set variant value.
  114. void SetVariantValue(const String& name, const Variant& value);
  115. /// Is object type.
  116. bool IsObject() const;
  117. /// Return child names (only object and array child name).
  118. Vector<String> GetChildNames() const;
  119. /// Return member value names.
  120. Vector<String> GetValueNames() const;
  121. /// Return int.
  122. int GetInt(const String& name) const;
  123. /// Return bool.
  124. bool GetBool(const String& name) const;
  125. /// Return float.
  126. float GetFloat(const String& name) const;
  127. /// Return vector2.
  128. Vector2 GetVector2(const String& name) const;
  129. /// Return vector3.
  130. Vector3 GetVector3(const String& name) const;
  131. /// Return vector4.
  132. Vector4 GetVector4(const String& name) const;
  133. /// Return vector variant.
  134. Variant GetVectorVariant(const String& name) const;
  135. /// Return quaternion.
  136. Quaternion GetQuaternion(const String& name) const;
  137. /// Return color.
  138. Color GetColor(const String& name) const;
  139. /// Return string.
  140. String GetString(const String& name) const;
  141. /// Return C string.
  142. const char* GetCString(const String& name) const;
  143. /// Return buffer.
  144. PODVector<unsigned char> GetBuffer(const String& name) const;
  145. /// Return buffer.
  146. bool GetBuffer(const String& name, void* dest, unsigned size) const;
  147. /// Return resource ref.
  148. ResourceRef GetResourceRef(const String& name) const;
  149. /// Return resource ref list.
  150. ResourceRefList GetResourceRefList(const String& name) const;
  151. /// Return int rect.
  152. IntRect GetIntRect(const String& name) const;
  153. /// Return int vector2.
  154. IntVector2 GetIntVector2(const String& name) const;
  155. /// Return matrix3.
  156. Matrix3 GetMatrix3(const String& name) const;
  157. /// Return matrix3x4.
  158. Matrix3x4 GetMatrix3x4(const String& name) const;
  159. /// Return matrix4.
  160. Matrix4 GetMatrix4(const String& name) const;
  161. /// Return variant.
  162. Variant GetVariant(const String& name) const;
  163. /// Return variant value.
  164. Variant GetVariantValue(const String& name, VariantType type) const;
  165. // JSON array value functions
  166. /// Create a child value in array.
  167. JSONValue CreateChild(JSONValueType valueType = JSON_OBJECT);
  168. /// Remove a child value in array. Return null if not exist.
  169. JSONValue GetChild(unsigned index, JSONValueType valueType = JSON_ANY) const;
  170. /// Add int.
  171. void AddInt(int value);
  172. /// Add bool.
  173. void AddBool(bool value);
  174. /// Add float.
  175. void AddFloat(float value);
  176. /// Add vector2.
  177. void AddVector2(const Vector2& value);
  178. /// Add vector3.
  179. void AddVector3(const Vector3& value);
  180. /// Add vector4.
  181. void AddVector4(const Vector4& value);
  182. /// Add vector variant.
  183. void AddVectorVariant(const Variant& value);
  184. /// Add quaternion.
  185. void AddQuaternion(const Quaternion& value);
  186. /// Add color.
  187. void AddColor(const Color& value);
  188. /// Add string.
  189. void AddString(const String& value);
  190. /// Add buffer.
  191. void AddBuffer(const PODVector<unsigned char>& value);
  192. /// Add buffer.
  193. void AddBuffer(const void* data, unsigned size);
  194. /// Add resource ref.
  195. void AddResourceRef(const ResourceRef& value);
  196. /// Add resource ref list.
  197. void AddResourceRefList(const ResourceRefList& value);
  198. /// Add int rect.
  199. void AddIntRect(const IntRect& value);
  200. /// Add int vector2.
  201. void AddIntVector2(const IntVector2& value);
  202. /// Add matrix3.
  203. void AddMatrix3(const Matrix3& value);
  204. /// Add matrix3x4.
  205. void AddMatrix3x4(const Matrix3x4& value);
  206. /// Add matrix4.
  207. void AddMatrix4(const Matrix4& value);
  208. /// Add variant.
  209. void AddVariant(const Variant& value);
  210. /// Add variant value.
  211. void AddVariantValue(const Variant& value);
  212. /// Is array type.
  213. bool IsArray() const;
  214. /// Return array size.
  215. unsigned GetSize() const;
  216. /// Return int.
  217. int GetInt(unsigned index) const;
  218. /// Return bool.
  219. bool GetBool(unsigned index) const;
  220. /// Return float.
  221. float GetFloat(unsigned index) const;
  222. /// Return vector2.
  223. Vector2 GetVector2(unsigned index) const;
  224. /// Return vector3.
  225. Vector3 GetVector3(unsigned index) const;
  226. /// Return vector4.
  227. Vector4 GetVector4(unsigned index) const;
  228. /// Return vector variant.
  229. Variant GetVectorVariant(unsigned index) const;
  230. /// Return quaternion.
  231. Quaternion GetQuaternion(unsigned index) const;
  232. /// Return color.
  233. Color GetColor(unsigned index) const;
  234. /// Return string.
  235. String GetString(unsigned index) const;
  236. /// Return C string.
  237. const char* GetCString(unsigned index) const;
  238. /// Return buffer.
  239. PODVector<unsigned char> GetBuffer(unsigned index) const;
  240. /// Return buffer.
  241. bool GetBuffer(unsigned index, void* dest, unsigned size) const;
  242. /// Return resource ref.
  243. ResourceRef GetResourceRef(unsigned index) const;
  244. /// Return resource ref list.
  245. ResourceRefList GetResourceRefList(unsigned index) const;
  246. /// Return int rect.
  247. IntRect GetIntRect(unsigned index) const;
  248. /// Return int vector2.
  249. IntVector2 GetIntVector2(unsigned index) const;
  250. /// Return matrix3.
  251. Matrix3 GetMatrix3(unsigned index) const;
  252. /// Return matrix3x4.
  253. Matrix3x4 GetMatrix3x4(unsigned index) const;
  254. /// Return matrix4.
  255. Matrix4 GetMatrix4(unsigned index) const;
  256. /// Return variant.
  257. Variant GetVariant(unsigned index) const;
  258. /// Return variant.
  259. Variant GetVariantValue(unsigned index, VariantType type) const;
  260. /// Empty JSONValue.
  261. static const JSONValue EMPTY;
  262. private:
  263. /// Set JSON value for object type.
  264. void AddMember(const String& name, rapidjson::Value& jsonValue);
  265. /// Return JSON value by name for object type.
  266. rapidjson::Value& GetMember(const String& name) const;
  267. /// Add JSON value to array type.
  268. void AddMember(rapidjson::Value& jsonValue);
  269. /// Return JSON value by index for array type.
  270. rapidjson::Value& GetMember(unsigned index) const;
  271. /// JSON file.
  272. WeakPtr<JSONFile> file_;
  273. /// Rapid JSON value.
  274. rapidjson::Value* value_;
  275. };
  276. }