qb  2.0.0.0
C++17 Actor Framework
qb Issue Watch Star Fork Follow @isndev
Loading...
Searching...
No Matches
json.h
Go to the documentation of this file.
1
24
25#ifndef QB_JSON_H
26#define QB_JSON_H
27#include "qb/io/protocol/json.h"
28#include "uuid.h"
29
30namespace nlohmann {
31
32// template <typename IO_>
33// using protocol = qb::protocol::json<IO_>;
34// template <typename IO_>
35// using protocol_packed = qb::protocol::json_packed<IO_>;
36
43using pointer = json::pointer;
44
51using object = json;
52
59using array = json::array_t;
60
67using string = json::string_t;
68
75using number = json::number_integer_t;
76
83using floating = json::number_float_t;
84
91using boolean = json::boolean_t;
92
103struct jsonb {
104 using json = nlohmann::json;
105 json data;
106
107 // --- Constructors ---
109 jsonb() = default;
111 jsonb(const json &j)
112 : data(j) {}
113
114 jsonb(json &&j) noexcept
115 : data(std::move(j)) {}
116
117 jsonb(std::initializer_list<json::value_type> init)
118 : data(init) {}
119
120 // --- Conversion operators ---
122 operator json &() {
123 return data;
124 }
125
126 operator const json &() const {
127 return data;
128 }
129
130 // --- Forwarded operators ---
132 json &
133 operator=(const json &j) {
134 data = j;
135 return *this;
136 }
137 json &
138 operator=(json &&j) noexcept {
139 data = std::move(j);
140 return *this;
141 }
142
143 json &
144 operator[](const std::string &key) {
145 return data[key];
146 }
147 const json &
148 operator[](const std::string &key) const {
149 return data.at(key);
150 }
151
152 json &
153 operator[](size_t i) {
154 return data[i];
155 }
156 const json &
157 operator[](size_t i) const {
158 return data.at(i);
159 }
160
161 json *
162 operator->() {
163 return &data;
164 }
165 const json *
166 operator->() const {
167 return &data;
168 }
169
170 // --- Iterators ---
171 auto
172 begin() {
173 return data.begin();
174 }
175 auto
176 end() {
177 return data.end();
178 }
179 auto
180 begin() const {
181 return data.begin();
182 }
183 auto
184 end() const {
185 return data.end();
186 }
187
188 // --- Common methods (forwarded) ---
189 inline std::string
190 dump(int indent = -1) const {
191 return data.dump(indent);
192 }
193 inline bool
194 is_null() const {
195 return data.is_null();
196 }
197 inline bool
198 is_object() const {
199 return data.is_object();
200 }
201 inline bool
202 is_array() const {
203 return data.is_array();
204 }
205 inline bool
206 is_string() const {
207 return data.is_string();
208 }
209 inline bool
210 is_number() const {
211 return data.is_number();
212 }
213 inline bool
214 is_boolean() const {
215 return data.is_boolean();
216 }
217
218 inline std::size_t
219 size() const {
220 return data.size();
221 }
222 inline bool
223 empty() const {
224 return data.empty();
225 }
226 inline void
227 clear() {
228 data.clear();
229 }
230 inline void
231 push_back(const json &value) {
232 data.push_back(value);
233 }
234 inline void
235 erase(const std::string &key) {
236 data.erase(key);
237 }
238 inline bool
239 contains(const std::string &key) const {
240 return data.contains(key);
241 }
242
243 // --- Comparisons ---
244 friend bool
245 operator==(const jsonb &a, const jsonb &b) {
246 return a.data == b.data;
247 }
248 friend bool
249 operator!=(const jsonb &a, const jsonb &b) {
250 return a.data != b.data;
251 }
252 friend bool
253 operator==(const jsonb &a, const json &b) {
254 return a.data == b;
255 }
256 friend bool
257 operator==(const json &a, const jsonb &b) {
258 return a == b.data;
259 }
260
261 // --- Access ---
266 json &
268 return data;
269 }
270
274 const json &
275 unwrap() const {
276 return data;
277 }
278};
279
280} // namespace nlohmann
281
282namespace qb {
283using namespace nlohmann;
284namespace allocator {
285
286template <>
287pipe<char> &pipe<char>::put<json>(const json &c);
288
289} // namespace allocator
290} // namespace qb
291
292namespace uuids {
301void to_json(qb::json &obj, qb::uuid const &id);
302
311void from_json(qb::json const &obj, qb::uuid &id);
312} // namespace uuids
313
314// Stream output support
323inline std::ostream &
324operator<<(std::ostream &os, const qb::jsonb &j) {
325 return os << j.dump();
326}
327
328// Hash support
329namespace std {
337template <>
338struct hash<qb::jsonb> {
344 std::size_t
345 operator()(const qb::jsonb &j) const noexcept {
346 return std::hash<std::string>{}(j.dump());
347 }
348};
349} // namespace std
350
351// Optional: fmtlib support
352#ifdef FMT_VERSION
353#include <fmt/format.h>
361template <>
362struct fmt::formatter<qb::jsonb> : formatter<std::string> {
369 auto
370 format(const qb::jsonb &j, fmt::format_context &ctx) {
371 return formatter<std::string>::format(j.dump(), ctx);
372 }
373};
374#endif
375
376#endif // QB_JSON_H
Extensible buffer optimized for performance.
Definition pipe.h:552
json::number_float_t floating
JSON floating-point number type from nlohmann::json.
Definition json.h:83
std::ostream & operator<<(std::ostream &os, const qb::jsonb &j)
Stream output operator for qb::jsonb.
Definition json.h:324
json::boolean_t boolean
JSON boolean type from nlohmann::json.
Definition json.h:91
json object
Alias for the main JSON type from nlohmann::json.
Definition json.h:51
json::string_t string
JSON string type from nlohmann::json.
Definition json.h:67
json::array_t array
JSON array type from nlohmann::json.
Definition json.h:59
json::pointer pointer
JSON pointer type from nlohmann::json library.
Definition json.h:43
json::number_integer_t number
JSON integer number type from nlohmann::json.
Definition json.h:75
void from_json(qb::json const &obj, qb::uuid &id)
Converts a JSON object to a UUID.
void to_json(qb::json &obj, qb::uuid const &id)
Converts a UUID to a JSON object.
JSON protocol implementations for the QB IO system.
A wrapper around nlohmann::json providing a qb::jsonb type, often used for binary JSON (BSON) or simi...
Definition json.h:103
jsonb(const json &j)
Constructs from an existing nlohmann::json object (copy).
Definition json.h:111
const json & unwrap() const
Provides direct constant access to the underlying nlohmann::json object.
Definition json.h:275
jsonb(json &&j) noexcept
Constructs from an existing nlohmann::json object (move).
Definition json.h:114
jsonb()=default
Default constructor.
json data
The underlying nlohmann::json object storing the JSON data.
Definition json.h:105
json & operator=(const json &j)
Assigns from another nlohmann::json object (copy).
Definition json.h:133
jsonb(std::initializer_list< json::value_type > init)
Constructs from an initializer list, same as nlohmann::json.
Definition json.h:117
json & unwrap()
Provides direct mutable access to the underlying nlohmann::json object.
Definition json.h:267
std::size_t operator()(const qb::jsonb &j) const noexcept
Calculates the hash value for a qb::jsonb object.
Definition json.h:345
Universally Unique Identifier (UUID) support.
::uuids::uuid uuid
UUID type alias for the underlying implementation.
Definition uuid.h:40