JSON.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #include "ContainerTypes.h"
  26. #pragma once
  27. namespace crown
  28. {
  29. /// @defgroup JSON
  30. /// Enumerates JSON value types.
  31. ///
  32. /// @ingroup JSON
  33. struct JSONType
  34. {
  35. enum Enum
  36. {
  37. OBJECT,
  38. ARRAY,
  39. STRING,
  40. NUMBER,
  41. BOOL,
  42. NIL
  43. };
  44. };
  45. /// Functions to parse JSON-encoded strings.
  46. ///
  47. /// @ingroup JSON
  48. namespace json
  49. {
  50. /// Returns the data type of the JSON string @a s.
  51. JSONType::Enum type(const char* s);
  52. /// Parses the JSON string @a s ad puts it into @a str.
  53. void parse_string(const char* s, DynamicString& str);
  54. /// Returns the JSON number @a s as double.
  55. double parse_number(const char* s);
  56. /// Returns the JSON number @a s as int.
  57. int32_t parse_int(const char* s);
  58. /// Returns the JSON number @a s as float.
  59. float parse_float(const char* s);
  60. /// Returns the JSON boolean @a s as bool.
  61. bool parse_bool(const char* s);
  62. /// Parses the JSON array @a s and puts it into @a array as pointers to
  63. /// the corresponding items into the original @a s string.
  64. void parse_array(const char* s, Array<const char*>& array);
  65. /// Parses the JSON object @a s and puts it into @a object as map from
  66. /// key to pointer to the corresponding value into the original string @a s.
  67. void parse_object(const char* s, Map<DynamicString, const char*>& object);
  68. } // namespace json
  69. } // namespace crown