Hegel 0.3.5
Property-based testing for C++
Loading...
Searching...
No Matches
json.h
1#pragma once
2
6
7#include <cstddef>
8#include <initializer_list>
9#include <memory>
10#include <optional>
11#include <string>
12#include <type_traits>
13#include <vector>
14
15namespace hegel::internal {
16 class NlohmannReader;
17}
18
19namespace hegel::internal::json {
20 class json;
21 class json_ref;
22 class ImplUtil;
23 struct json_ref_holder;
24 struct json_holder;
25
26 class json_raw_ref {
27 std::unique_ptr<json_ref_holder> ref;
28 friend class ImplUtil;
29
30 public:
31 json_raw_ref(json_ref_holder* ref_);
32 json_raw_ref(const json_raw_ref& other);
33 ~json_raw_ref();
34
35 std::string get_string() const noexcept;
36 bool get_bool() const noexcept;
37 uint32_t get_uint32_t() const noexcept;
38 uint64_t get_uint64_t() const noexcept;
39 int64_t get_int64_t() const noexcept;
40 double get_double() const noexcept;
41
42 size_t size() const noexcept;
43
44 bool is_string() const noexcept;
45 bool is_null() const noexcept;
46 bool is_boolean() const noexcept;
47 bool is_number() const noexcept;
48 bool is_number_integer() const noexcept;
49 bool is_number_unsigned() const noexcept;
50 bool is_array() const noexcept;
51 bool is_object() const noexcept;
52
53 json_raw_ref& operator=(const size_t& other);
54 json_raw_ref& operator=(const double& other);
55 json_raw_ref& operator=(const std::nullptr_t& other);
56 json_raw_ref& operator=(bool other);
57 json_raw_ref& operator=(const std::string& other);
58 json_raw_ref& operator=(const json& other);
59#ifdef __APPLE__
60 json_raw_ref& operator=(const uint64_t& other);
61#endif
62
63 json_raw_ref operator[](size_t index) const;
64
65 std::vector<json_raw_ref> iterate() const;
66 std::vector<std::pair<std::string, json_raw_ref>> items() const;
67 std::optional<json_raw_ref> find(const std::string& key) const;
68 };
69
70 class json {
71 using initializer_list_t = std::initializer_list<json_ref>;
72
73 public:
74 json(const json& init);
75
76 json(json&& init) noexcept;
77
78 json(initializer_list_t init);
79
80 json(const char* init);
81
82 json(const int32_t init);
83 json(const int64_t init);
84 json(const uint32_t init);
85 json(const uint64_t init);
86#ifdef __APPLE__
87 json(const unsigned long init);
88#endif
89 json(const bool init);
90 json(const double init);
91 json(const std::string& init);
92 json(std::nullptr_t init = nullptr);
93 json(const json_raw_ref& init);
94 ~json();
95
96 json_raw_ref operator[](const std::string& key);
97
98 json& operator=(json other) noexcept;
99
100 std::string value(const std::string& key,
101 const std::string& default_value);
102 uint32_t value(const std::string& key, const uint32_t& default_value);
103
104 bool contains(const std::string& key);
105
106 static json array(initializer_list_t init = {});
107 void push_back(json&& val);
108 void push_back(const json& val);
109 void push_back(const std::string& val);
110
111 std::string dump() const;
112
113 std::vector<unsigned char>& get_binary();
114
115 static json parse(const char* arg);
116
117 private:
118 std::unique_ptr<json_holder> impl;
119 friend class ImplUtil;
120 };
121
122 class json_ref {
123 public:
124 json_ref(json&& value) : owned_value(std::move(value)) {}
125
126 json_ref(const json& value) : value_ref(&value) {}
127
128 json_ref(std::initializer_list<json_ref> init) : owned_value(init) {}
129
130 template <class... Args,
131 std::enable_if_t<std::is_constructible<json, Args...>::value,
132 int> = 0>
133 json_ref(Args&&... args) : owned_value(std::forward<Args>(args)...) {}
134
135 // class should be movable only
136 json_ref(json_ref&&) noexcept = default;
137 json_ref(const json_ref&) = delete;
138 json_ref& operator=(const json_ref&) = delete;
139 json_ref& operator=(json_ref&&) = delete;
140 ~json_ref() = default;
141
142 json const& operator*() const {
143 return value_ref ? *value_ref : owned_value;
144 }
145
146 private:
147 mutable json owned_value = nullptr;
148 json const* value_ref = nullptr;
149 };
150} // namespace hegel::internal::json