JSONParser.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 "ContainerTypes.h"
  26. #include "MathTypes.h"
  27. #include "Macros.h"
  28. namespace crown
  29. {
  30. class JSONParser;
  31. class DynamicString;
  32. /// Represents a JSON element.
  33. /// The objects of this class are valid until the parser
  34. /// which has generated them, will exist.
  35. ///
  36. /// @ingroup JSON
  37. class CE_EXPORT JSONElement
  38. {
  39. public:
  40. /// Construct the nil JSONElement.
  41. /// Used to forward-instantiate elements or as a special
  42. /// nil element.
  43. JSONElement();
  44. explicit JSONElement(const char* at);
  45. JSONElement(const JSONElement& other);
  46. JSONElement& operator=(const JSONElement& other);
  47. /// Returns the @a i -th item of the current array.
  48. JSONElement operator[](uint32_t i);
  49. /// @copydoc JSONElement::operator[]
  50. JSONElement index(uint32_t i);
  51. /// Returns the @a i -th item of the current array or
  52. /// the special nil JSONElement() if the index does not exist.
  53. JSONElement index_or_nil(uint32_t i);
  54. /// Returns the element corresponding to key @a k of the
  55. /// current object.
  56. /// @note
  57. /// If the key is not unique in the object scope, the last
  58. /// key in order of appearance will be selected.
  59. JSONElement key(const char* k);
  60. /// Returns the element corresponding to key @a k of the current
  61. /// object, or the special nil JSONElement() if the key does not exist.
  62. JSONElement key_or_nil(const char* k);
  63. /// Returns whether the element has the @a k key.
  64. bool has_key(const char* k) const;
  65. /// Returns true wheter the element is the JSON nil special value.
  66. bool is_nil() const;
  67. /// Returns true wheter the element is a JSON boolean (true or false).
  68. bool is_bool() const;
  69. /// Returns true wheter the element is a JSON number.
  70. bool is_number() const;
  71. /// Returns true whether the element is a JSON string.
  72. bool is_string() const;
  73. /// Returns true whether the element is a JSON array.
  74. bool is_array() const;
  75. /// Returns true whether the element is a JSON object.
  76. bool is_object() const;
  77. /// Returns the size of the element based on the
  78. /// element's type:
  79. /// * nil, bool, number: 1
  80. /// * string: length of the string
  81. /// * array: number of elements in the array
  82. /// * object: number of keys in the object
  83. uint32_t size() const;
  84. /// Returns the boolean value of the element.
  85. bool to_bool() const;
  86. /// Returns the integer value of the element.
  87. int32_t to_int() const;
  88. /// Returns the float value of the element.
  89. float to_float() const;
  90. /// Returns the string value of the element.
  91. void to_string(DynamicString& str) const;
  92. /// Rerutns the Vector3 value of the element.
  93. /// @note Vector3 = [x, y, z]
  94. Vector3 to_vector3() const;
  95. /// Returns the Quaternion value of the element.
  96. /// @note Quaternion = [x, y, z, w]
  97. Quaternion to_quaternion() const;
  98. /// Returns the string id value hashed to string::murmur2_32() of the element.
  99. StringId32 to_string_id() const;
  100. /// Returns the array value of the element.
  101. /// @note
  102. /// Calling this function is way faster than accessing individual
  103. /// array elements by JSONElement::operator[] and it is the very preferred way
  104. /// for retrieving array elemets. However, you have to be sure that the array
  105. /// contains only items of the given @array type.
  106. void to_array(Array<bool>& array) const;
  107. /// @copydoc JSONElement::to_array(Array<bool>&)
  108. void to_array(Array<int16_t>& array) const;
  109. /// @copydoc JSONElement::to_array(Array<bool>&)
  110. void to_array(Array<uint16_t>& array) const;
  111. /// @copydoc JSONElement::to_array(Array<bool>&)
  112. void to_array(Array<int32_t>& array) const;
  113. /// @copydoc JSONElement::to_array(Array<bool>&)
  114. void to_array(Array<uint32_t>& array) const;
  115. /// @copydoc JSONElement::to_array(Array<bool>&)
  116. void to_array(Array<float>& array) const;
  117. /// @copydoc JSONElement::to_array(Array<bool>&)
  118. void to_array(Vector<DynamicString>& array) const;
  119. /// Returns all the keys of the element.
  120. void to_keys(Vector<DynamicString>& keys) const;
  121. private:
  122. const char* m_at;
  123. friend class JSONParser;
  124. };
  125. /// Parses JSON documents.
  126. ///
  127. /// @ingroup JSON
  128. class CE_EXPORT JSONParser
  129. {
  130. public:
  131. /// Read the JSON document contained in the non-null @a s string.
  132. /// @note
  133. /// The @a s string has to remain valid for the whole parser's
  134. /// existence scope.
  135. JSONParser(const char* s);
  136. /// Returns the root element of the JSON document.
  137. JSONElement root();
  138. private:
  139. const char* const m_document;
  140. private:
  141. // Disable copying
  142. JSONParser(const JSONParser&);
  143. JSONParser& operator=(const JSONParser&);
  144. };
  145. } // namespace crown