json.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #ifndef json_hpp
  9. #define json_hpp
  10. #include "../variant/variant.hpp"
  11. #include "json_common.hpp"
  12. #include <utility>
  13. #define JSON_TYPE_LIST \
  14. X(object) \
  15. X(array) \
  16. X(string) \
  17. X(double) \
  18. X(int) \
  19. X(uint) \
  20. X(bool)
  21. namespace json {
  22. namespace {
  23. const constexpr json::int_jt INT_JT_MAX_LAST_DIGIT = (0x7FFFFFFF % 10);
  24. const constexpr json::int_jt INT_JT_MAX = json::uint_jt(-1) - 1;
  25. const constexpr json::int_jt INT_JT_MIN = json::int_jt(~(json::uint_jt(-1) / 2));
  26. const constexpr json::uint_jt INT_JT_OVER = json::uint_jt(INT_JT_MAX) + 1;
  27. const constexpr json::uint_jt UINT_JT_MAX = json::uint_jt(0) - 1;
  28. // const constexpr json::uint_jt UINT_JT_MIN = 0;
  29. }
  30. class value;
  31. namespace parser {
  32. void parse(value&, char const*);
  33. }
  34. class value {
  35. public:
  36. using object_jt = std::map<std::string, value>;
  37. using array_jt = std::vector<value>;
  38. using string_jt = json::string_jt;
  39. using double_jt = json::double_jt;
  40. using int_jt = json::int_jt;
  41. using uint_jt = json::uint_jt;
  42. using bool_jt = json::bool_jt;
  43. private:
  44. static const value null_value;
  45. using data_t = variant<object_jt, array_jt, string_jt, double_jt, int_jt, uint_jt, bool_jt>;
  46. data_t data;
  47. public:
  48. #define X(type) bool is_##type() const { return data.is<type##_jt>(); }
  49. JSON_TYPE_LIST
  50. #undef X
  51. bool is_null() const { return !data.valid(); }
  52. value() = default;
  53. value(value const&) = default;
  54. value(value &&) = default;
  55. value& operator=(value const&) = default;
  56. value& operator=(value &&) = default;
  57. #define X(type) value(type##_jt val) { data.set<type##_jt>(std::move(val)); }
  58. JSON_TYPE_LIST
  59. #undef X
  60. #define X(type) value(type##_jt && val) { data.set<type##_jt>(std::forward<type##_jt>(val)); }
  61. JSON_TYPE_LIST
  62. #undef X
  63. #define X(type) value& operator=(type##_jt val) { data.set<type##_jt>(std::move(val)); return *this; }
  64. JSON_TYPE_LIST
  65. #undef X
  66. void parse(char const* data);
  67. void parse(std::string const& str);
  68. void parse(std::istream & in);
  69. void clear() { data = data_t(); }
  70. value& operator[](const size_t idx);
  71. value const& operator[](const size_t idx) const;
  72. value& operator[](std::string const& key);
  73. value const& operator[](std::string const& key) const;
  74. string_jt const& as_string() const;
  75. double_jt as_double() const;
  76. int_jt as_int() const;
  77. uint_jt as_uint() const;
  78. bool_jt as_bool() const;
  79. operator string_jt const&() const { return as_string(); }
  80. operator double_jt() const { return as_double(); }
  81. operator int_jt() const { return as_int(); }
  82. operator uint_jt() const { return as_uint(); }
  83. operator bool_jt() const { return as_bool(); }
  84. };
  85. #undef JSON_TYPE_LIST
  86. }
  87. #endif /* json_hpp */