JSONParser.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. struct JSONPair
  38. {
  39. const char* key;
  40. const char* val;
  41. };
  42. /// Parses JSON documents.
  43. class JSONParser
  44. {
  45. public:
  46. /// Read the JSON document contained in the non-null @a s string.
  47. /// @note
  48. /// The @a s string has to remain valid for the whole parser's
  49. /// existence scope.
  50. JSONParser(const char* s);
  51. /// Returns the root element of the JSON document.
  52. JSONParser& root();
  53. /// Returns the @a i -th item of the current array.
  54. JSONParser& operator[](uint32_t i);
  55. /// @copydoc JSONParser::operator[]
  56. JSONParser& index(uint32_t i);
  57. /// Returns the element corresponding to key @a k of the
  58. /// current object.
  59. /// @note
  60. /// If the key is not unique in the object scope, the last
  61. /// key in order of appearance will be selected.
  62. JSONParser& key(const char* k);
  63. /// Returns true wheter the current element is the JSON nil special value.
  64. bool is_nil() const;
  65. /// Returns true wheter the current element is a JSON boolean (true or false).
  66. bool is_bool() const;
  67. /// Returns true wheter the current element is a JSON number.
  68. bool is_number() const;
  69. /// Returns true whether the current element is a JSON string.
  70. bool is_string() const;
  71. /// Returns true whether the current element is a JSON array.
  72. bool is_array() const;
  73. /// Returns true whether the current element is a JSON object.
  74. bool is_object() const;
  75. /// Returns the size of the current 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 current element.
  83. bool bool_value() const;
  84. /// Returns the integer value of the current element.
  85. int32_t int_value() const;
  86. /// Returns the float value of the current element.
  87. float float_value() const;
  88. /// Returns the string value of the current 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. public:
  95. /// Returns the type of the @a s JSON text.
  96. static JSONType type(const char* s);
  97. /// Parses the @a s JSON string a puts its C representation into @a str.
  98. static void parse_string(const char* s, List<char>& str);
  99. /// Returns the value of the @a s JSON number as double.
  100. static double parse_number(const char* s);
  101. /// Returns the value of the @a s JSON boolean.
  102. static bool parse_bool(const char* s);
  103. /// Returns the value of the @a s JSON number as signed integer.
  104. static int32_t parse_int(const char* s);
  105. /// Returns the value of the @a s JSON number as float.
  106. static float parse_float(const char* s);
  107. /// Parses the @a s JSON array and puts it into @a array as pointers to
  108. /// the corresponding items into the original @a s string.
  109. static void parse_array(const char* s, List<const char*>& array);
  110. /// Parses the @a s JSON object and puts it into @a object as pointers to
  111. /// the corresponding key/value pairs into the original @a s string.
  112. static void parse_object(const char* s, List<JSONPair>& object);
  113. private:
  114. const char* const m_document;
  115. const char* m_at;
  116. private:
  117. // Disable copying
  118. JSONParser& JSONParser(const JSONParser&);
  119. JSONParser& operator=(const JSONParser&);
  120. };
  121. } // namespace crown