json.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // json.hpp
  3. // json
  4. //
  5. // Created by Sam Jaffe on 1/30/16.
  6. // Copyright © 2016 Sam Jaffe. All rights reserved.
  7. //
  8. #pragma once
  9. #include "json_common.hpp"
  10. #include "variant/variant.hpp"
  11. #include <map>
  12. #include <utility>
  13. #include <vector>
  14. #define JSON_TYPE_LIST \
  15. X(object) \
  16. X(array) \
  17. X(string) \
  18. X(double) \
  19. X(int) \
  20. X(uint) \
  21. X(bool)
  22. namespace json {
  23. class value;
  24. template <> void helper::parse_numeric(value & json, char const *& data);
  25. namespace parser {
  26. void parse(value &, char const *);
  27. }
  28. class value {
  29. public:
  30. using object_jt = std::map<std::string, value>;
  31. using array_jt = std::vector<value>;
  32. using string_jt = json::string_jt;
  33. using double_jt = json::double_jt;
  34. using int_jt = json::int_jt;
  35. using uint_jt = json::uint_jt;
  36. using bool_jt = json::bool_jt;
  37. private:
  38. static const value null_value;
  39. using data_t = variant<object_jt, array_jt, string_jt, double_jt, int_jt,
  40. uint_jt, bool_jt>;
  41. data_t data;
  42. public:
  43. #define X(type) \
  44. bool is_##type() const { return data.is<type##_jt>(); }
  45. JSON_TYPE_LIST
  46. #undef X
  47. bool is_null() const { return !data.valid(); }
  48. value() = default;
  49. value(value const &) = default;
  50. value(value &&) = default;
  51. value & operator=(value const &) = default;
  52. value & operator=(value &&) = default;
  53. #define X(type) \
  54. value(type##_jt val) { data.set<type##_jt>(std::move(val)); }
  55. JSON_TYPE_LIST
  56. #undef X
  57. #define X(type) \
  58. value & operator=(type##_jt val) { \
  59. data.set<type##_jt>(std::move(val)); \
  60. return *this; \
  61. }
  62. JSON_TYPE_LIST
  63. #undef X
  64. void parse(char const * data);
  65. void parse(std::string const & str);
  66. void parse(std::istream & in);
  67. void clear() { data = data_t(); }
  68. value & operator[](const size_t idx);
  69. value const & operator[](const size_t idx) const;
  70. value & operator[](std::string const & key);
  71. value const & operator[](std::string const & key) const;
  72. string_jt const & as_string() const;
  73. double_jt as_double() const;
  74. int_jt as_int() const;
  75. uint_jt as_uint() const;
  76. bool_jt as_bool() const;
  77. operator string_jt const &() const { return as_string(); }
  78. operator double_jt() const { return as_double(); }
  79. operator int_jt() const { return as_int(); }
  80. operator uint_jt() const { return as_uint(); }
  81. operator bool_jt() const { return as_bool(); }
  82. };
  83. #undef JSON_TYPE_LIST
  84. namespace parser {
  85. void parse(json::value &, char const *);
  86. void parse(json::value & json, std::istream & in);
  87. }
  88. }