JSONParser.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. /// Used only to forward-instantiate elements.
  51. /// In order to be able to use the element, it must be
  52. /// obtained from JSONParser::root() or copied from an
  53. /// already existent and valid element.
  54. JSONElement();
  55. JSONElement(const JSONElement& other);
  56. JSONElement& operator=(const JSONElement& other);
  57. /// Returns the @a i -th item of the current array.
  58. JSONElement& operator[](uint32_t i);
  59. /// @copydoc JSONParser::operator[]
  60. JSONElement& index(uint32_t i);
  61. /// Returns the element corresponding to key @a k of the
  62. /// current object.
  63. /// @note
  64. /// If the key is not unique in the object scope, the last
  65. /// key in order of appearance will be selected.
  66. JSONElement& key(const char* k);
  67. /// Returns whether the element has the @a k key.
  68. bool has_key(const char* k) const;
  69. /// Returns whether the @a k key is unique in the object
  70. /// element. If no such key is found it returns false.
  71. bool is_key_unique(const char* k) const;
  72. /// Returns true wheter the element is the JSON nil special value.
  73. bool is_nil() const;
  74. /// Returns true wheter the element is a JSON boolean (true or false).
  75. bool is_bool() const;
  76. /// Returns true wheter the element is a JSON number.
  77. bool is_number() const;
  78. /// Returns true whether the element is a JSON string.
  79. bool is_string() const;
  80. /// Returns true whether the element is a JSON array.
  81. bool is_array() const;
  82. /// Returns true whether the element is a JSON object.
  83. bool is_object() const;
  84. /// Returns the size of the element based on the
  85. /// element's type:
  86. /// * nil, bool, number: 1
  87. /// * string: length of the string
  88. /// * array: number of elements in the array
  89. /// * object: number of keys in the object
  90. uint32_t size() const;
  91. /// Returns the boolean value of the element.
  92. bool bool_value();
  93. /// Returns the integer value of the element.
  94. int32_t int_value();
  95. /// Returns the float value of the element.
  96. float float_value();
  97. /// Returns the string value of the element.
  98. /// @warning
  99. /// The returned string is kept internally until the next call to
  100. /// this function, so it is highly unsafe to just keep the pointer
  101. /// instead of copying its content somewhere else.
  102. const char* string_value();
  103. private:
  104. explicit JSONElement(JSONParser& parser, const char* at);
  105. JSONParser* m_parser;
  106. const char* m_begin;
  107. const char* m_at;
  108. friend class JSONParser;
  109. };
  110. /// Parses JSON documents.
  111. class JSONParser
  112. {
  113. public:
  114. /// Read the JSON document contained in the non-null @a s string.
  115. /// @note
  116. /// The @a s string has to remain valid for the whole parser's
  117. /// existence scope.
  118. JSONParser(const char* s);
  119. /// Returns the root element of the JSON document.
  120. JSONElement root();
  121. public:
  122. /// Returns the type of the @a s JSON text.
  123. static JSONType type(const char* s);
  124. /// Parses the @a s JSON string a puts its C representation into @a str.
  125. static void parse_string(const char* s, List<char>& str);
  126. /// Returns the value of the @a s JSON number as double.
  127. static double parse_number(const char* s);
  128. /// Returns the value of the @a s JSON boolean.
  129. static bool parse_bool(const char* s);
  130. /// Returns the value of the @a s JSON number as signed integer.
  131. static int32_t parse_int(const char* s);
  132. /// Returns the value of the @a s JSON number as float.
  133. static float parse_float(const char* s);
  134. /// Parses the @a s JSON array and puts it into @a array as pointers to
  135. /// the corresponding items into the original @a s string.
  136. static void parse_array(const char* s, List<const char*>& array);
  137. /// Parses the @a s JSON object and puts it into @a object as pointers to
  138. /// the corresponding key/value pairs into the original @a s string.
  139. static void parse_object(const char* s, List<JSONPair>& object);
  140. private:
  141. const char* const m_document;
  142. private:
  143. // Disable copying
  144. JSONParser(const JSONParser&);
  145. JSONParser& operator=(const JSONParser&);
  146. };
  147. } // namespace crown