json_parser.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "types.h"
  25. #include "container_types.h"
  26. #include "math_types.h"
  27. #include "matrix4x4.h"
  28. #include "quaternion.h"
  29. #include "macros.h"
  30. #include "resource.h"
  31. namespace crown
  32. {
  33. class JSONParser;
  34. class DynamicString;
  35. class File;
  36. /// Represents a JSON element.
  37. /// The objects of this class are valid until the parser
  38. /// which has generated them, will exist.
  39. ///
  40. /// @ingroup JSON
  41. class JSONElement
  42. {
  43. public:
  44. /// Construct the nil JSONElement.
  45. /// Used to forward-instantiate elements or as a special
  46. /// nil element.
  47. JSONElement();
  48. explicit JSONElement(const char* at);
  49. JSONElement(const JSONElement& other);
  50. JSONElement& operator=(const JSONElement& other);
  51. /// Returns the @a i -th item of the current array.
  52. JSONElement operator[](uint32_t i);
  53. /// @copydoc JSONElement::operator[]
  54. JSONElement index(uint32_t i);
  55. /// Returns the @a i -th item of the current array or
  56. /// the special nil JSONElement() if the index does not exist.
  57. JSONElement index_or_nil(uint32_t i);
  58. /// Returns the element corresponding to key @a k of the
  59. /// current object.
  60. /// @note
  61. /// If the key is not unique in the object scope, the last
  62. /// key in order of appearance will be selected.
  63. JSONElement key(const char* k);
  64. /// Returns the element corresponding to key @a k of the current
  65. /// object, or the special nil JSONElement() if the key does not exist.
  66. JSONElement key_or_nil(const char* k);
  67. /// Returns whether the element has the @a k key.
  68. bool has_key(const char* k) const;
  69. /// Returns true wheter the element is the JSON nil special value.
  70. bool is_nil() const;
  71. /// Returns true wheter the element is a JSON boolean (true or false).
  72. bool is_bool() const;
  73. /// Returns true wheter the element is a JSON number.
  74. bool is_number() const;
  75. /// Returns true whether the element is a JSON string.
  76. bool is_string() const;
  77. /// Returns true whether the element is a JSON array.
  78. bool is_array() const;
  79. /// Returns true whether the element is a JSON object.
  80. bool is_object() const;
  81. /// Returns the size of the element based on the
  82. /// element's type:
  83. /// * nil, bool, number: 1
  84. /// * string: length of the string
  85. /// * array: number of elements in the array
  86. /// * object: number of keys in the object
  87. uint32_t size() const;
  88. /// Returns the boolean value of the element.
  89. bool to_bool(bool def = false) const;
  90. /// Returns the integer value of the element.
  91. int32_t to_int(int32_t def = 0) const;
  92. /// Returns the float value of the element.
  93. float to_float(float def = 0) const;
  94. /// Returns the string value of the element.
  95. void to_string(DynamicString& str, const char* def = "") const;
  96. /// Returns the Vector2 value of the element.
  97. /// @note Vector2 = [x, y]
  98. Vector2 to_vector2(const Vector2& def = Vector2(0, 0)) const;
  99. /// Returns the Vector3 value of the element.
  100. /// @note Vector3 = [x, y, z]
  101. Vector3 to_vector3(const Vector3& def = Vector3(0, 0, 0)) const;
  102. /// Returns the Vector4 value of the element.
  103. /// @note Vector4 = [x, y, z, w]
  104. Vector4 to_vector4(const Vector4& def = Vector4(0, 0, 0, 0)) const;
  105. /// Returns the Quaternion value of the element.
  106. /// @note Quaternion = [x, y, z, w]
  107. Quaternion to_quaternion(const Quaternion& def = quaternion::IDENTITY) const;
  108. /// Returns the Matrix4x4 value of the element.
  109. /// @note Matrix4x4 = [x, x, x, x, y, y, y, y, z, z, z, z, t, t, t, t]
  110. Matrix4x4 to_matrix4x4(const Matrix4x4& def = matrix4x4::IDENTITY) const;
  111. /// Returns the string id value hashed to string::murmur2_32() of the element.
  112. StringId32 to_string_id(const StringId32 def = 0) const;
  113. /// Returns the resource id value of the element.
  114. /// If @a type is NULL then the string element is assumed to already contain extension.
  115. ResourceId to_resource_id(const char* type) const;
  116. /// Returns the array value of the element.
  117. /// @note
  118. /// Calling this function is way faster than accessing individual
  119. /// array elements by JSONElement::operator[] and it is the very preferred way
  120. /// for retrieving array elemets. However, you have to be sure that the array
  121. /// contains only items of the given @array type.
  122. void to_array(Array<bool>& array) const;
  123. /// @copydoc JSONElement::to_array(Array<bool>&)
  124. void to_array(Array<int16_t>& array) const;
  125. /// @copydoc JSONElement::to_array(Array<bool>&)
  126. void to_array(Array<uint16_t>& array) const;
  127. /// @copydoc JSONElement::to_array(Array<bool>&)
  128. void to_array(Array<int32_t>& array) const;
  129. /// @copydoc JSONElement::to_array(Array<bool>&)
  130. void to_array(Array<uint32_t>& array) const;
  131. /// @copydoc JSONElement::to_array(Array<bool>&)
  132. void to_array(Array<float>& array) const;
  133. /// @copydoc JSONElement::to_array(Array<bool>&)
  134. void to_array(Vector<DynamicString>& array) const;
  135. /// Returns all the keys of the element.
  136. void to_keys(Vector<DynamicString>& keys) const;
  137. private:
  138. const char* m_at;
  139. friend class JSONParser;
  140. };
  141. /// Parses JSON documents.
  142. ///
  143. /// @ingroup JSON
  144. class JSONParser
  145. {
  146. public:
  147. /// Reads the JSON document contained in the non-null @a s string.
  148. /// @note
  149. /// The @a s string has to remain valid for the whole parser's
  150. /// existence scope.
  151. JSONParser(const char* s);
  152. /// Reads the JSON document from file @a f.
  153. JSONParser(File& f);
  154. ~JSONParser();
  155. /// Returns the root element of the JSON document.
  156. JSONElement root();
  157. private:
  158. bool m_file;
  159. const char* m_document;
  160. private:
  161. // Disable copying
  162. JSONParser(const JSONParser&);
  163. JSONParser& operator=(const JSONParser&);
  164. };
  165. } // namespace crown