JSONParser.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. enum JSONType
  29. {
  30. JT_NIL,
  31. JT_OBJECT,
  32. JT_ARRAY,
  33. JT_STRING,
  34. JT_NUMBER,
  35. JT_BOOL
  36. };
  37. /// Represents a key-value pair in a JSON document.
  38. struct JSONPair
  39. {
  40. const char* key;
  41. const char* val;
  42. };
  43. class JSONParser;
  44. /// Represents a JSON element.
  45. /// The objects of this class are valid until the parser
  46. /// which has generated them, will exist.
  47. class JSONElement
  48. {
  49. public:
  50. /// Construct the nil JSONElement.
  51. /// Used to forward-instantiate elements or as a special
  52. /// nil element.
  53. JSONElement();
  54. JSONElement(const JSONElement& other);
  55. JSONElement& operator=(const JSONElement& other);
  56. /// Returns the @a i -th item of the current array.
  57. JSONElement& operator[](uint32_t i);
  58. /// @copydoc JSONElement::operator[]
  59. JSONElement& index(uint32_t i);
  60. /// Returns the @a i -th item of the current array or
  61. /// the special nil JSONElement() if the index does not exist.
  62. JSONElement index_or_nil(uint32_t i);
  63. /// Returns the element corresponding to key @a k of the
  64. /// current object.
  65. /// @note
  66. /// If the key is not unique in the object scope, the last
  67. /// key in order of appearance will be selected.
  68. JSONElement& key(const char* k);
  69. /// Returns the element corresponding to key @a k of the current
  70. /// object, or the special nil JSONElement() if the key does not exist.
  71. JSONElement key_or_nil(const char* k);
  72. /// Returns whether the element has the @a k key.
  73. bool has_key(const char* k) const;
  74. /// Returns whether the @a k key is unique in the object
  75. /// element. If no such key is found it returns false.
  76. bool is_key_unique(const char* k) const;
  77. /// Returns true wheter the element is the JSON nil special value.
  78. bool is_nil() const;
  79. /// Returns true wheter the element is a JSON boolean (true or false).
  80. bool is_bool() const;
  81. /// Returns true wheter the element is a JSON number.
  82. bool is_number() const;
  83. /// Returns true whether the element is a JSON string.
  84. bool is_string() const;
  85. /// Returns true whether the element is a JSON array.
  86. bool is_array() const;
  87. /// Returns true whether the element is a JSON object.
  88. bool is_object() const;
  89. /// Returns the size of the element based on the
  90. /// element's type:
  91. /// * nil, bool, number: 1
  92. /// * string: length of the string
  93. /// * array: number of elements in the array
  94. /// * object: number of keys in the object
  95. uint32_t size() const;
  96. /// Returns the boolean value of the element.
  97. bool bool_value();
  98. /// Returns the integer value of the element.
  99. int32_t int_value();
  100. /// Returns the float value of the element.
  101. float float_value();
  102. /// Returns the string value of the element.
  103. /// @warning
  104. /// The returned string is kept internally until the next call to
  105. /// this function, so it is highly unsafe to just keep the pointer
  106. /// instead of copying its content somewhere else.
  107. const char* string_value();
  108. /// Returns the array value of the element.
  109. /// @note
  110. /// Calling this function is way faster than accessing individual
  111. /// array elements by JSONElement::operator[] and it is the very preferred way
  112. /// for retrieving array elemets. However, you have to be sure that the array
  113. /// contains only items of the given @array type.
  114. void array_value(List<bool>& array);
  115. /// @copydoc JSONElement::array_value(List<bool>&)
  116. void array_value(List<int16_t>& array);
  117. /// @copydoc JSONElement::array_value(List<bool>&)
  118. void array_value(List<uint16_t>& array);
  119. /// @copydoc JSONElement::array_value(List<bool>&)
  120. void array_value(List<int32_t>& array);
  121. /// @copydoc JSONElement::array_value(List<bool>&)
  122. void array_value(List<uint32_t>& array);
  123. /// @copydoc JSONElement::array_value(List<bool>&)
  124. void array_value(List<float>& array);
  125. private:
  126. explicit JSONElement(JSONParser& parser, const char* at);
  127. JSONParser* m_parser;
  128. const char* m_begin;
  129. const char* m_at;
  130. friend class JSONParser;
  131. };
  132. /// Parses JSON documents.
  133. class JSONParser
  134. {
  135. public:
  136. /// Read the JSON document contained in the non-null @a s string.
  137. /// @note
  138. /// The @a s string has to remain valid for the whole parser's
  139. /// existence scope.
  140. JSONParser(const char* s);
  141. /// Returns the root element of the JSON document.
  142. JSONElement root();
  143. public:
  144. /// Returns the type of the @a s JSON text.
  145. static JSONType type(const char* s);
  146. /// Parses the @a s JSON string a puts its C representation into @a str.
  147. static void parse_string(const char* s, List<char>& str);
  148. /// Returns the value of the @a s JSON number as double.
  149. static double parse_number(const char* s);
  150. /// Returns the value of the @a s JSON boolean.
  151. static bool parse_bool(const char* s);
  152. /// Returns the value of the @a s JSON number as signed integer.
  153. static int32_t parse_int(const char* s);
  154. /// Returns the value of the @a s JSON number as float.
  155. static float parse_float(const char* s);
  156. /// Parses the @a s JSON array and puts it into @a array as pointers to
  157. /// the corresponding items into the original @a s string.
  158. static void parse_array(const char* s, List<const char*>& array);
  159. /// Parses the @a s JSON object and puts it into @a object as pointers to
  160. /// the corresponding key/value pairs into the original @a s string.
  161. static void parse_object(const char* s, List<JSONPair>& object);
  162. private:
  163. const char* const m_document;
  164. private:
  165. // Disable copying
  166. JSONParser(const JSONParser&);
  167. JSONParser& operator=(const JSONParser&);
  168. };
  169. } // namespace crown