JSONParser.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 "List.h"
  26. namespace crown
  27. {
  28. class JSONParser;
  29. /// Represents a JSON element.
  30. /// The objects of this class are valid until the parser
  31. /// which has generated them, will exist.
  32. class JSONElement
  33. {
  34. public:
  35. /// Construct the nil JSONElement.
  36. /// Used to forward-instantiate elements or as a special
  37. /// nil element.
  38. JSONElement();
  39. explicit JSONElement(const char* at);
  40. JSONElement(const JSONElement& other);
  41. JSONElement& operator=(const JSONElement& other);
  42. /// Returns the @a i -th item of the current array.
  43. JSONElement operator[](uint32_t i);
  44. /// @copydoc JSONElement::operator[]
  45. JSONElement index(uint32_t i);
  46. /// Returns the @a i -th item of the current array or
  47. /// the special nil JSONElement() if the index does not exist.
  48. JSONElement index_or_nil(uint32_t i);
  49. /// Returns the element corresponding to key @a k of the
  50. /// current object.
  51. /// @note
  52. /// If the key is not unique in the object scope, the last
  53. /// key in order of appearance will be selected.
  54. JSONElement key(const char* k);
  55. /// Returns the element corresponding to key @a k of the current
  56. /// object, or the special nil JSONElement() if the key does not exist.
  57. JSONElement key_or_nil(const char* k);
  58. /// Returns whether the element has the @a k key.
  59. bool has_key(const char* k) const;
  60. /// Returns whether the @a k key is unique in the object
  61. /// element. If no such key is found it returns false.
  62. bool is_key_unique(const char* k) const;
  63. /// Returns true wheter the element is the JSON nil special value.
  64. bool is_nil() const;
  65. /// Returns true wheter the element is a JSON boolean (true or false).
  66. bool is_bool() const;
  67. /// Returns true wheter the element is a JSON number.
  68. bool is_number() const;
  69. /// Returns true whether the element is a JSON string.
  70. bool is_string() const;
  71. /// Returns true whether the element is a JSON array.
  72. bool is_array() const;
  73. /// Returns true whether the element is a JSON object.
  74. bool is_object() const;
  75. /// Returns the size of the element based on the
  76. /// element's type:
  77. /// * nil, bool, number: 1
  78. /// * string: length of the string
  79. /// * array: number of elements in the array
  80. /// * object: number of keys in the object
  81. uint32_t size() const;
  82. /// Returns the boolean value of the element.
  83. bool bool_value() const;
  84. /// Returns the integer value of the element.
  85. int32_t int_value() const;
  86. /// Returns the float value of the element.
  87. float float_value() const;
  88. /// Returns the string value of the element.
  89. /// @warning
  90. /// The returned string is kept internally until the next call to
  91. /// this function, so it is highly unsafe to just keep the pointer
  92. /// instead of copying its content somewhere else.
  93. const char* string_value() const;
  94. /// Returns the array value of the element.
  95. /// @note
  96. /// Calling this function is way faster than accessing individual
  97. /// array elements by JSONElement::operator[] and it is the very preferred way
  98. /// for retrieving array elemets. However, you have to be sure that the array
  99. /// contains only items of the given @array type.
  100. void array_value(List<bool>& array) const;
  101. /// @copydoc JSONElement::array_value(List<bool>&)
  102. void array_value(List<int16_t>& array) const;
  103. /// @copydoc JSONElement::array_value(List<bool>&)
  104. void array_value(List<uint16_t>& array) const;
  105. /// @copydoc JSONElement::array_value(List<bool>&)
  106. void array_value(List<int32_t>& array) const;
  107. /// @copydoc JSONElement::array_value(List<bool>&)
  108. void array_value(List<uint32_t>& array) const;
  109. /// @copydoc JSONElement::array_value(List<bool>&)
  110. void array_value(List<float>& array) const;
  111. private:
  112. const char* m_at;
  113. friend class JSONParser;
  114. };
  115. /// Parses JSON documents.
  116. class JSONParser
  117. {
  118. public:
  119. /// Read the JSON document contained in the non-null @a s string.
  120. /// @note
  121. /// The @a s string has to remain valid for the whole parser's
  122. /// existence scope.
  123. JSONParser(const char* s);
  124. /// Returns the root element of the JSON document.
  125. JSONElement root();
  126. private:
  127. const char* const m_document;
  128. private:
  129. // Disable copying
  130. JSONParser(const JSONParser&);
  131. JSONParser& operator=(const JSONParser&);
  132. };
  133. } // namespace crown