json.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 <map>
  11. #include <vector>
  12. #include <string>
  13. #include <utility>
  14. #include "../variant/variant.hpp"
  15. #define JSON_TYPE_LIST \
  16. X(object) \
  17. X(array) \
  18. X(string) \
  19. X(double) \
  20. X(int) \
  21. X(bool)
  22. namespace json {
  23. class value;
  24. namespace parser {
  25. template <typename T> void parse(T& json, char const* data);
  26. template <typename T> void parse(T& json, std::string const& str);
  27. template <typename T> void parse(T& json, std::istream & in);
  28. }
  29. class malformed_json_exception : public std::domain_error {
  30. using std::domain_error::domain_error;
  31. };
  32. class value {
  33. public:
  34. using object_jt = std::map<std::string, value>;
  35. using array_jt = std::vector<value>;
  36. using string_jt = std::string;
  37. using double_jt = double;
  38. using int_jt = int32_t;
  39. using bool_jt = bool;
  40. private:
  41. static const value null_value;
  42. using data_t = variant<object_jt, array_jt, string_jt, double_jt, int_jt, bool_jt>;
  43. data_t data;
  44. public:
  45. #define X(type) bool is_##type() const { return data.is<type##_jt>(); }
  46. JSON_TYPE_LIST
  47. #undef X
  48. bool is_null() const { return !data.valid(); }
  49. value() = default;
  50. value(value const&) = default;
  51. value(value &&) = default;
  52. value& operator=(value const&) = default;
  53. value& operator=(value &&) = default;
  54. #define X(type) value(type##_jt val) { data.set<type##_jt>(std::move(val)); }
  55. JSON_TYPE_LIST
  56. #undef X
  57. #define X(type) value(type##_jt && val) { data.set<type##_jt>(std::forward<type##_jt>(val)); }
  58. JSON_TYPE_LIST
  59. #undef X
  60. #define X(type) value& operator=(type##_jt val) { data.set<type##_jt>(std::move(val)); return *this; }
  61. JSON_TYPE_LIST
  62. #undef X
  63. void parse(char const* data);
  64. void parse(std::string const& str);
  65. void parse(std::istream & in);
  66. void clear() { data = data_t(); }
  67. value& operator[](const size_t idx);
  68. value const& operator[](const size_t idx) const;
  69. value& operator[](std::string const& key);
  70. value const& operator[](std::string const& key) const;
  71. string_jt const& as_string() const;
  72. double_jt as_double() const;
  73. int_jt as_int() const;
  74. bool_jt as_bool() const;
  75. operator string_jt const&() const { return as_string(); }
  76. operator double_jt() const { return as_double(); }
  77. operator int_jt() const { return as_int(); }
  78. operator bool_jt() const { return as_bool(); }
  79. };
  80. #undef JSON_TYPE_LIST
  81. }
  82. namespace json {
  83. namespace helper {
  84. const char get_next_element(char const*& data) {
  85. while (isspace(*data)) ++data;
  86. return *data;
  87. }
  88. template <char const E>
  89. void advance_to_boundary(char const*& data) {
  90. switch (get_next_element(data)) {
  91. case ',': // This is there 'more data remains' token for the compound value
  92. ++data;
  93. break;
  94. case E: // This is the end-token for the compound value
  95. break;
  96. default: // Error, malformed JSON
  97. throw json::malformed_json_exception(std::string("Expected to recieve container delimiter ',' or '") + E + "', got '" + *data + "' instead");
  98. break;
  99. }
  100. }
  101. template <typename T>
  102. void parse_string(T& json, char const*& data) {
  103. char const* start = data;
  104. while (*++data) {
  105. if (*data == '"' && *(data-1) != '\\') {
  106. json = std::string(start+1, data);
  107. ++data;
  108. return;
  109. }
  110. }
  111. throw json::malformed_json_exception("Could not locate end of string");
  112. }
  113. template <typename T>
  114. void parse_numeric(T& json, char const*& data) {
  115. char const* start = data;
  116. while (*++data) {
  117. if (*data == '.') {
  118. while (isnumber(*++data));
  119. json = atof(start);
  120. break;
  121. } else if (!isnumber(*data)) {
  122. json = atoi(start);
  123. break;
  124. }
  125. }
  126. }
  127. }
  128. }
  129. #endif /* json_hpp */