Skip to content

Commit 6d3a112

Browse files
committed
Removes json serialization boilerplate.
1 parent 1f3ef6b commit 6d3a112

File tree

6 files changed

+65
-143
lines changed

6 files changed

+65
-143
lines changed

CMakeLists.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,13 @@ if (MSVC)
134134
target_compile_definitions(cpp20_resolve_with_sentinel PRIVATE _WIN32_WINNT=0x0601)
135135
endif()
136136

137-
add_executable(cpp20_serialization examples/cpp20_serialization.cpp)
138-
target_compile_features(cpp20_serialization PUBLIC cxx_std_20)
139-
target_link_libraries(cpp20_serialization common)
140-
add_test(cpp20_serialization cpp20_serialization)
137+
add_executable(cpp20_json_serialization examples/cpp20_json_serialization.cpp)
138+
target_compile_features(cpp20_json_serialization PUBLIC cxx_std_20)
139+
target_link_libraries(cpp20_json_serialization common)
140+
add_test(cpp20_json_serialization cpp20_json_serialization)
141141
if (MSVC)
142-
target_compile_options(cpp20_serialization PRIVATE /bigobj)
143-
target_compile_definitions(cpp20_serialization PRIVATE _WIN32_WINNT=0x0601)
142+
target_compile_options(cpp20_json_serialization PRIVATE /bigobj)
143+
target_compile_definitions(cpp20_json_serialization PRIVATE _WIN32_WINNT=0x0601)
144144
endif()
145145

146146
add_executable(cpp20_subscriber examples/cpp20_subscriber.cpp)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,7 @@ Acknowledgement to people that helped shape Boost.Redis
881881
next. Now requests are continuously coalesced and written to the
882882
socket. This made the request::coalesce unnecessary and threfore it
883883
was removed.
884+
* Native json support for Boost.Describe'd classes.
884885

885886
### v1.4.0-1
886887

examples/common/serialization.hpp

Lines changed: 0 additions & 105 deletions
This file was deleted.

examples/cpp20_serialization.cpp renamed to examples/cpp20_json_serialization.cpp

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,26 @@
66

77
#include <boost/asio.hpp>
88
#if defined(BOOST_ASIO_HAS_CO_AWAIT)
9+
#define BOOST_JSON_NO_LIB
10+
#define BOOST_CONTAINER_NO_LIB
911
#include <boost/redis.hpp>
10-
#include <iostream>
12+
#include <boost/describe.hpp>
13+
#include <boost/redis/json.hpp>
1114
#include <set>
1215
#include <string>
16+
#include <iostream>
1317
#include "common/common.hpp"
14-
#include "common/serialization.hpp"
1518

1619
// Include this in no more than one .cpp file.
1720
#include <boost/json/src.hpp>
1821

1922
namespace net = boost::asio;
2023
namespace redis = boost::redis;
24+
using namespace boost::describe;
2125
using boost::redis::request;
2226
using boost::redis::response;
23-
using boost::redis::ignore_t;
2427
using boost::redis::operation;
28+
using boost::redis::ignore_t;
2529

2630
struct user {
2731
std::string name;
@@ -41,46 +45,34 @@ auto run(std::shared_ptr<connection> conn, std::string host, std::string port) -
4145
co_await conn->async_run();
4246
}
4347

44-
auto hello(std::shared_ptr<connection> conn) -> net::awaitable<void>
48+
net::awaitable<void> co_main(std::string host, std::string port)
4549
{
46-
request req;
47-
req.push("HELLO", 3);
48-
49-
co_await conn->async_exec(req);
50-
}
50+
auto ex = co_await net::this_coro::executor;
51+
auto conn = std::make_shared<connection>(ex);
52+
net::co_spawn(ex, run(conn, host, port), net::detached);
5153

52-
auto sadd(std::shared_ptr<connection> conn) -> net::awaitable<void>
53-
{
54+
// A set of users that will be automatically serialized to json.
5455
std::set<user> users
5556
{{"Joao", "58", "Brazil"} , {"Serge", "60", "France"}};
5657

58+
// To simplify we send the set and retrieve it in the same
59+
// resquest.
5760
request req;
58-
req.push_range("SADD", "sadd-key", users); // Sends
59-
60-
co_await conn->async_exec(req);
61-
}
62-
63-
auto smembers(std::shared_ptr<connection> conn) -> net::awaitable<void>
64-
{
65-
request req;
61+
req.push("HELLO", 3);
62+
req.push_range("SADD", "sadd-key", users);
6663
req.push("SMEMBERS", "sadd-key");
6764

68-
response<std::set<user>> resp;
65+
// The response will contain the deserialized set, which should
66+
// match the one we sent.
67+
response<ignore_t, ignore_t, std::set<user>> resp;
6968

69+
// Sends the request and receives the response.
7070
co_await conn->async_exec(req, resp);
7171

72-
for (auto const& e: std::get<0>(resp).value())
73-
std::cout << e << "\n";
74-
}
72+
// Print.
73+
for (auto const& e: std::get<2>(resp).value())
74+
std::cout << e.name << " " << e.age << " " << e.country << "\n";
7575

76-
net::awaitable<void> co_main(std::string host, std::string port)
77-
{
78-
auto ex = co_await net::this_coro::executor;
79-
auto conn = std::make_shared<connection>(ex);
80-
net::co_spawn(ex, run(conn, host, port), net::detached);
81-
co_await hello(conn);
82-
co_await sadd(conn);
83-
co_await smembers(conn);
8476
conn->cancel(operation::run);
8577
}
8678

include/boost/redis/json.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* Copyright (c) 2018-2022 Marcelo Zimbres Silva ([email protected])
2+
*
3+
* Distributed under the Boost Software License, Version 1.0. (See
4+
* accompanying file LICENSE.txt)
5+
*/
6+
7+
#ifndef BOOST_REDIS_JSON_HPP
8+
#define BOOST_REDIS_JSON_HPP
9+
10+
#include <boost/json.hpp>
11+
#include <boost/redis/resp3/serialization.hpp>
12+
13+
namespace boost::redis::json
14+
{
15+
16+
template <class T>
17+
void boost_redis_to_bulk(std::string& to, T const& u)
18+
{
19+
redis::resp3::boost_redis_to_bulk(to, boost::json::serialize(boost::json::value_from(u)));
20+
}
21+
22+
template <class T>
23+
void boost_redis_from_bulk(T& u, std::string_view sv, system::error_code&)
24+
{
25+
auto const jv = boost::json::parse(sv);
26+
u = boost::json::value_to<T>(jv);
27+
}
28+
29+
} // boost::redis::json
30+
31+
using boost::redis::json::boost_redis_to_bulk;
32+
using boost::redis::json::boost_redis_from_bulk;
33+
34+
#endif // BOOST_REDIS_JSON_HPP

include/boost/redis/resp3/serialization.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ template <class T, typename = typename std::enable_if<std::is_integral<T>::value
4343
void boost_redis_to_bulk(std::string& payload, T n)
4444
{
4545
auto const s = std::to_string(n);
46-
boost_redis_to_bulk(payload, std::string_view{s});
46+
boost::redis::resp3::boost_redis_to_bulk(payload, std::string_view{s});
4747
}
4848

4949
template <class T>

0 commit comments

Comments
 (0)