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_IO_PROTOCOL_JSON_H
26#define QB_IO_PROTOCOL_JSON_H
27#include "base.h"
28#include "nlohmann/json.hpp"
29
30namespace qb {
31namespace protocol {
32
50template <typename IO_>
51class json : public base::byte_terminated<IO_, '\0'> {
52public:
56 json() = delete;
57
63 explicit json(IO_ &io) noexcept
65
73 struct message {
74 const std::size_t size;
75 const char *data;
76 nlohmann::json json;
77 };
78
87 void
88 onMessage(std::size_t size) noexcept final {
89 const auto parsed = this->shiftSize(size);
90 const auto data = this->_io.in().cbegin();
91
92 this->_io.on(message{
93 parsed, data,
94 nlohmann::json::parse(std::string_view(data, parsed), nullptr, false)});
95 }
96};
97
113template <typename IO_>
114class json_packed : public base::byte_terminated<IO_, '\0'> {
115public:
119 json_packed() = delete;
120
126 explicit json_packed(IO_ &io) noexcept
128
136 struct message {
137 const std::size_t size;
138 const char *data;
139 nlohmann::json json;
140 };
141
151 void
152 onMessage(std::size_t size) noexcept final {
153 const auto parsed = this->shiftSize(size);
154 const auto data = this->_io.in().cbegin();
155 this->_io.on(message{
156 parsed, data, nlohmann::json::from_msgpack(std::string_view(data, parsed))});
157 }
158};
159
160} // namespace protocol
161} // namespace qb
162#endif // QB_IO_PROTOCOL_JSON_H
IO_ & _io
Definition protocol.h:166
Protocol for messages delimited by a specific single byte character.
Definition base.h:48
std::size_t shiftSize(std::size_t const size) const noexcept
Definition base.h:81
json_packed()=delete
Default constructor (deleted)
void onMessage(std::size_t size) noexcept final
Process a received message.
Definition json.h:152
json_packed(IO_ &io) noexcept
Constructor with I/O reference.
Definition json.h:126
void onMessage(std::size_t size) noexcept final
Process a received message.
Definition json.h:88
json()=delete
Default constructor (deleted)
json(IO_ &io) noexcept
Constructor with I/O reference.
Definition json.h:63
Base protocol implementations for message framing in the QB IO system.
Structure representing a JSON message.
Definition json.h:73
const std::size_t size
Message size.
Definition json.h:74
nlohmann::json json
Parsed JSON object.
Definition json.h:76
const char * data
Pointer to the raw data.
Definition json.h:75
Structure representing a MessagePack encoded JSON message.
Definition json.h:136
const std::size_t size
Message size.
Definition json.h:137
const char * data
Pointer to the raw data.
Definition json.h:138
nlohmann::json json
Parsed JSON object.
Definition json.h:139