JSON.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. namespace json
  32. {
  33. /// Enumerates JSON value types.
  34. struct JSONType
  35. {
  36. enum Enum
  37. {
  38. OBJECT,
  39. ARRAY,
  40. STRING,
  41. NUMBER,
  42. BOOL,
  43. NIL
  44. };
  45. };
  46. /// Represents a key-value pair in a JSON document.
  47. struct JSONPair
  48. {
  49. const char* key;
  50. const char* val;
  51. };
  52. /// Returns the type of the @a s JSON text.
  53. JSONType::Enum type(const char* s);
  54. /// Parses the @a s JSON string a puts its C representation into @a str.
  55. void parse_string(const char* s, DynamicString& str);
  56. /// Returns the value of the @a s JSON number as double.
  57. double parse_number(const char* s);
  58. /// Returns the value of the @a s JSON boolean.
  59. bool parse_bool(const char* s);
  60. /// Returns the value of the @a s JSON number as signed integer.
  61. int32_t parse_int(const char* s);
  62. /// Returns the value of the @a s JSON number as float.
  63. float parse_float(const char* s);
  64. /// Parses the @a s JSON array and puts it into @a array as pointers to
  65. /// the corresponding items into the original @a s string.
  66. void parse_array(const char* s, List<const char*>& array);
  67. /// Parses the @a s JSON object and puts it into @a object as pointers to
  68. /// the corresponding key/value pairs into the original @a s string.
  69. void parse_object(const char* s, List<JSONPair>& object);
  70. } // namespace json
  71. } // namespace crown