json.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #pragma once
  6. #include "dynamic_string.h"
  7. #include "container_types.h"
  8. namespace crown
  9. {
  10. /// @defgroup JSON
  11. /// Enumerates JSON value types.
  12. ///
  13. /// @ingroup JSON
  14. struct JSONType
  15. {
  16. enum Enum
  17. {
  18. NIL,
  19. BOOL,
  20. NUMBER,
  21. STRING,
  22. ARRAY,
  23. OBJECT
  24. };
  25. };
  26. /// Functions to parse JSON-encoded strings.
  27. ///
  28. /// @ingroup JSON
  29. namespace json
  30. {
  31. /// Returns the data type of the JSON string @a str.
  32. JSONType::Enum type(const char* str);
  33. /// Parses the JSON string @a str ad puts it into @a string.
  34. void parse_string(const char* str, DynamicString& string);
  35. /// Returns the JSON number @a str as double.
  36. double parse_number(const char* str);
  37. /// Returns the JSON number @a str as int.
  38. int32_t parse_int(const char* str);
  39. /// Returns the JSON number @a str as float.
  40. float parse_float(const char* str);
  41. /// Returns the JSON boolean @a str as bool.
  42. bool parse_bool(const char* str);
  43. /// Parses the JSON array @a str and puts it into @a array as pointers to
  44. /// the corresponding items into the original @a str string.
  45. void parse_array(const char* str, Array<const char*>& array);
  46. /// Parses the JSON object @a str and puts it into @a object as map from
  47. /// key to pointer to the corresponding value into the original string @a str.
  48. void parse_object(const char* str, Map<DynamicString, const char*>& object);
  49. } // namespace json
  50. } // namespace crown