json.h 1.4 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. OBJECT,
  19. ARRAY,
  20. STRING,
  21. NUMBER,
  22. BOOL,
  23. NIL
  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 s.
  32. JSONType::Enum type(const char* s);
  33. /// Parses the JSON string @a s ad puts it into @a str.
  34. void parse_string(const char* s, DynamicString& str);
  35. /// Returns the JSON number @a s as double.
  36. double parse_number(const char* s);
  37. /// Returns the JSON number @a s as int.
  38. int32_t parse_int(const char* s);
  39. /// Returns the JSON number @a s as float.
  40. float parse_float(const char* s);
  41. /// Returns the JSON boolean @a s as bool.
  42. bool parse_bool(const char* s);
  43. /// Parses the JSON array @a s and puts it into @a array as pointers to
  44. /// the corresponding items into the original @a s string.
  45. void parse_array(const char* s, Array<const char*>& array);
  46. /// Parses the JSON object @a s and puts it into @a object as map from
  47. /// key to pointer to the corresponding value into the original string @a s.
  48. void parse_object(const char* s, Map<DynamicString, const char*>& object);
  49. } // namespace json
  50. } // namespace crown