json_parser.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #pragma once
  6. #include "types.h"
  7. #include "container_types.h"
  8. #include "math_types.h"
  9. #include "matrix4x4.h"
  10. #include "quaternion.h"
  11. #include "macros.h"
  12. #include "resource_id.h"
  13. namespace crown
  14. {
  15. class JSONParser;
  16. class DynamicString;
  17. class File;
  18. /// Represents a JSON element.
  19. /// The objects of this class are valid until the parser
  20. /// which has generated them, will exist.
  21. ///
  22. /// @ingroup JSON
  23. class JSONElement
  24. {
  25. public:
  26. /// Construct the nil JSONElement.
  27. /// Used to forward-instantiate elements or as a special
  28. /// nil element.
  29. JSONElement();
  30. explicit JSONElement(const char* at);
  31. JSONElement(const JSONElement& other);
  32. JSONElement& operator=(const JSONElement& other);
  33. /// Returns the @a i -th item of the current array.
  34. JSONElement operator[](uint32_t i);
  35. /// @copydoc JSONElement::operator[]
  36. JSONElement index(uint32_t i);
  37. /// Returns the @a i -th item of the current array or
  38. /// the special nil JSONElement() if the index does not exist.
  39. JSONElement index_or_nil(uint32_t i);
  40. /// Returns the element corresponding to key @a k of the
  41. /// current object.
  42. /// @note
  43. /// If the key is not unique in the object scope, the last
  44. /// key in order of appearance will be selected.
  45. JSONElement key(const char* k);
  46. /// Returns the element corresponding to key @a k of the current
  47. /// object, or the special nil JSONElement() if the key does not exist.
  48. JSONElement key_or_nil(const char* k);
  49. /// Returns whether the element has the @a k key.
  50. bool has_key(const char* k) const;
  51. /// Returns true wheter the element is the JSON nil special value.
  52. bool is_nil() const;
  53. /// Returns true wheter the element is a JSON boolean (true or false).
  54. bool is_bool() const;
  55. /// Returns true wheter the element is a JSON number.
  56. bool is_number() const;
  57. /// Returns true whether the element is a JSON string.
  58. bool is_string() const;
  59. /// Returns true whether the element is a JSON array.
  60. bool is_array() const;
  61. /// Returns true whether the element is a JSON object.
  62. bool is_object() const;
  63. /// Returns the size of the element based on the
  64. /// element's type:
  65. /// * nil, bool, number: 1
  66. /// * string: length of the string
  67. /// * array: number of elements in the array
  68. /// * object: number of keys in the object
  69. uint32_t size() const;
  70. /// Returns the boolean value of the element.
  71. bool to_bool(bool def = false) const;
  72. /// Returns the integer value of the element.
  73. int32_t to_int(int32_t def = 0) const;
  74. /// Returns the float value of the element.
  75. float to_float(float def = 0) const;
  76. /// Returns the string value of the element.
  77. void to_string(DynamicString& str, const char* def = "") const;
  78. /// Returns the Vector2 value of the element.
  79. /// @note Vector2 = [x, y]
  80. Vector2 to_vector2(const Vector2& def = Vector2(0, 0)) const;
  81. /// Returns the Vector3 value of the element.
  82. /// @note Vector3 = [x, y, z]
  83. Vector3 to_vector3(const Vector3& def = Vector3(0, 0, 0)) const;
  84. /// Returns the Vector4 value of the element.
  85. /// @note Vector4 = [x, y, z, w]
  86. Vector4 to_vector4(const Vector4& def = Vector4(0, 0, 0, 0)) const;
  87. /// Returns the Quaternion value of the element.
  88. /// @note Quaternion = [x, y, z, w]
  89. Quaternion to_quaternion(const Quaternion& def = quaternion::IDENTITY) const;
  90. /// Returns the Matrix4x4 value of the element.
  91. /// @note Matrix4x4 = [x, x, x, x, y, y, y, y, z, z, z, z, t, t, t, t]
  92. Matrix4x4 to_matrix4x4(const Matrix4x4& def = matrix4x4::IDENTITY) const;
  93. /// Returns the string id value hashed to murmur2_32() of the element.
  94. StringId32 to_string_id(const StringId32 def = 0) const;
  95. /// Returns the resource id value of the element.
  96. /// If @a type is NULL then the string element is assumed to already contain extension.
  97. ResourceId to_resource_id(const char* type) const;
  98. /// Returns the array value of the element.
  99. /// @note
  100. /// Calling this function is way faster than accessing individual
  101. /// array elements by JSONElement::operator[] and it is the very preferred way
  102. /// for retrieving array elemets. However, you have to be sure that the array
  103. /// contains only items of the given @array type.
  104. void to_array(Array<bool>& array) const;
  105. /// @copydoc JSONElement::to_array(Array<bool>&)
  106. void to_array(Array<int16_t>& array) const;
  107. /// @copydoc JSONElement::to_array(Array<bool>&)
  108. void to_array(Array<uint16_t>& array) const;
  109. /// @copydoc JSONElement::to_array(Array<bool>&)
  110. void to_array(Array<int32_t>& array) const;
  111. /// @copydoc JSONElement::to_array(Array<bool>&)
  112. void to_array(Array<uint32_t>& array) const;
  113. /// @copydoc JSONElement::to_array(Array<bool>&)
  114. void to_array(Array<float>& array) const;
  115. /// @copydoc JSONElement::to_array(Array<bool>&)
  116. void to_array(Vector<DynamicString>& array) const;
  117. /// Returns all the keys of the element.
  118. void to_keys(Vector<DynamicString>& keys) const;
  119. private:
  120. const char* _at;
  121. friend class JSONParser;
  122. };
  123. /// Parses JSON documents.
  124. ///
  125. /// @ingroup JSON
  126. class JSONParser
  127. {
  128. public:
  129. /// Reads the JSON document contained in the non-null @a s string.
  130. /// @note
  131. /// The @a s string has to remain valid for the whole parser's
  132. /// existence scope.
  133. JSONParser(const char* s);
  134. /// Reads the JSON document from file @a f.
  135. JSONParser(File& f);
  136. ~JSONParser();
  137. /// Returns the root element of the JSON document.
  138. JSONElement root();
  139. private:
  140. bool _file;
  141. const char* _document;
  142. private:
  143. // Disable copying
  144. JSONParser(const JSONParser&);
  145. JSONParser& operator=(const JSONParser&);
  146. };
  147. } // namespace crown