| 65 | class JsonValue; |
| 66 | |
| 67 | class Json final { |
| 68 | public: |
| 69 | // Types |
| 70 | enum Type { |
| 71 | NUL, NUMBER, BOOL, STRING, ARRAY, OBJECT |
| 72 | }; |
| 73 | |
| 74 | // Array and object typedefs |
| 75 | typedef std::vector<Json> array; |
| 76 | typedef std::map<std::string, Json> object; |
| 77 | |
| 78 | // Constructors for the various types of JSON value. |
| 79 | Json() noexcept; // NUL |
| 80 | Json(std::nullptr_t) noexcept; // NUL |
| 81 | Json(double value); // NUMBER |
| 82 | Json(int value); // NUMBER |
| 83 | Json(bool value); // BOOL |
| 84 | Json(const std::string &value); // STRING |
| 85 | Json(std::string &&value); // STRING |
| 86 | Json(const char * value); // STRING |
| 87 | Json(const array &values); // ARRAY |
| 88 | Json(array &&values); // ARRAY |
| 89 | Json(const object &values); // OBJECT |
| 90 | Json(object &&values); // OBJECT |
| 91 | |
| 92 | // Implicit constructor: anything with a to_json() function. |
| 93 | template <class T, class = decltype(&T::to_json)> |
| 94 | Json(const T & t) : Json(t.to_json()) {} |
| 95 | |
| 96 | // Implicit constructor: map-like objects (std::map, std::unordered_map, etc) |
| 97 | template <class M, typename std::enable_if< |
| 98 | std::is_constructible<std::string, decltype(std::declval<M>().begin()->first)>::value |
| 99 | && std::is_constructible<Json, decltype(std::declval<M>().begin()->second)>::value, |
| 100 | int>::type = 0> |
| 101 | Json(const M & m) : Json(object(m.begin(), m.end())) {} |
| 102 | |
| 103 | // Implicit constructor: vector-like objects (std::list, std::vector, std::set, etc) |
| 104 | template <class V, typename std::enable_if< |
| 105 | std::is_constructible<Json, decltype(*std::declval<V>().begin())>::value, |
| 106 | int>::type = 0> |
| 107 | Json(const V & v) : Json(array(v.begin(), v.end())) {} |
| 108 | |
| 109 | // This prevents Json(some_pointer) from accidentally producing a bool. Use |
| 110 | // Json(bool(some_pointer)) if that behavior is desired. |
| 111 | Json(void *) = delete; |
| 112 | |
| 113 | // Accessors |
| 114 | Type type() const; |
| 115 | |
| 116 | bool is_null() const { return type() == NUL; } |
| 117 | bool is_number() const { return type() == NUMBER; } |
| 118 | bool is_bool() const { return type() == BOOL; } |
| 119 | bool is_string() const { return type() == STRING; } |
| 120 | bool is_array() const { return type() == ARRAY; } |
| 121 | bool is_object() const { return type() == OBJECT; } |
| 122 | |
| 123 | // Return the enclosed value if this is a number, 0 otherwise. Note that json11 does not |
| 124 | // distinguish between integer and non-integer numbers - number_value() and int_value() |
no test coverage detected