json.hpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 "../variant/variant.hpp"
  10. #include "json_common.hpp"
  11. #include <map>
  12. #include <vector>
  13. #include <utility>
  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. namespace parser {
  25. void parse(value&, char const*);
  26. }
  27. class value {
  28. public:
  29. using object_jt = std::map<std::string, value>;
  30. using array_jt = std::vector<value>;
  31. using string_jt = json::string_jt;
  32. using double_jt = json::double_jt;
  33. using int_jt = json::int_jt;
  34. using uint_jt = json::uint_jt;
  35. using bool_jt = json::bool_jt;
  36. private:
  37. static const value null_value;
  38. using data_t = variant<object_jt, array_jt, string_jt, double_jt, int_jt, uint_jt, bool_jt>;
  39. data_t data;
  40. public:
  41. #define X(type) bool is_##type() const { return data.is<type##_jt>(); }
  42. JSON_TYPE_LIST
  43. #undef X
  44. bool is_null() const { return !data.valid(); }
  45. value() = default;
  46. value(value const&) = default;
  47. value(value &&) = default;
  48. value& operator=(value const&) = default;
  49. value& operator=(value &&) = default;
  50. #define X(type) value(type##_jt val) { data.set<type##_jt>(std::move(val)); }
  51. JSON_TYPE_LIST
  52. #undef X
  53. #define X(type) value(type##_jt && val) { data.set<type##_jt>(std::forward<type##_jt>(val)); }
  54. JSON_TYPE_LIST
  55. #undef X
  56. #define X(type) value& operator=(type##_jt val) { data.set<type##_jt>(std::move(val)); return *this; }
  57. JSON_TYPE_LIST
  58. #undef X
  59. void parse(char const* data);
  60. void parse(std::string const& str);
  61. void parse(std::istream & in);
  62. void clear() { data = data_t(); }
  63. value& operator[](const size_t idx);
  64. value const& operator[](const size_t idx) const;
  65. value& operator[](std::string const& key);
  66. value const& operator[](std::string const& key) const;
  67. string_jt const& as_string() const;
  68. double_jt as_double() const;
  69. int_jt as_int() const;
  70. uint_jt as_uint() const;
  71. bool_jt as_bool() const;
  72. operator string_jt const&() const { return as_string(); }
  73. operator double_jt() const { return as_double(); }
  74. operator int_jt() const { return as_int(); }
  75. operator uint_jt() const { return as_uint(); }
  76. operator bool_jt() const { return as_bool(); }
  77. };
  78. #undef JSON_TYPE_LIST
  79. namespace parser {
  80. void parse(json::value &, char const *);
  81. void parse(json::value& json, std::istream & in);
  82. }
  83. }