JSON.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #include "Types.h"
  24. #include "DynamicString.h"
  25. #pragma once
  26. namespace crown
  27. {
  28. template<typename T>
  29. class List;
  30. class DynamicString;
  31. /// @defgroup JSON
  32. /// Functions to parse JSON
  33. ///
  34. /// @ingroup JSON
  35. namespace json
  36. {
  37. /// Enumerates JSON value types.
  38. ///
  39. /// @ingroup JSON
  40. struct JSONType
  41. {
  42. enum Enum
  43. {
  44. OBJECT,
  45. ARRAY,
  46. STRING,
  47. NUMBER,
  48. BOOL,
  49. NIL
  50. };
  51. };
  52. /// Represents a key-value pair in a JSON document.
  53. ///
  54. /// @ingroup JSON
  55. struct JSONPair
  56. {
  57. const char* key;
  58. const char* val;
  59. };
  60. /// Returns the type of the @a s JSON text.
  61. JSONType::Enum type(const char* s);
  62. /// Parses the @a s JSON string a puts its C representation into @a str.
  63. void parse_string(const char* s, DynamicString& str);
  64. /// Returns the value of the @a s JSON number as double.
  65. double parse_number(const char* s);
  66. /// Returns the value of the @a s JSON boolean.
  67. bool parse_bool(const char* s);
  68. /// Returns the value of the @a s JSON number as signed integer.
  69. int32_t parse_int(const char* s);
  70. /// Returns the value of the @a s JSON number as float.
  71. float parse_float(const char* s);
  72. /// Parses the @a s JSON array and puts it into @a array as pointers to
  73. /// the corresponding items into the original @a s string.
  74. void parse_array(const char* s, Array<const char*>& array);
  75. /// Parses the @a s JSON object and puts it into @a object as pointers to
  76. /// the corresponding key/value pairs into the original @a s string.
  77. void parse_object(const char* s, Array<JSONPair>& object);
  78. } // namespace json
  79. } // namespace crown