Boost.Redis: example/cpp20_json.cpp Source File

Boost.Redis 1.84.0 A redis client library

Loading...
Searching...
No Matches
cpp20_json.cpp
1/* Copyright (c) 2018-2022 Marcelo Zimbres Silva (mzimbres@gmail.com)
2 *
3 * Distributed under the Boost Software License, Version 1.0. (See
4 * accompanying file LICENSE.txt)
5 */
6
7#include <boost/redis/connection.hpp>
8#include <boost/asio/detached.hpp>
9#include <boost/describe.hpp>
10#include <boost/asio/consign.hpp>
11#include <boost/asio/use_awaitable.hpp>
12#include <string>
13#include <iostream>
14
15#if defined(BOOST_ASIO_HAS_CO_AWAIT)
16
17#include <boost/json/serialize.hpp>
18#include <boost/json/parse.hpp>
19#include <boost/json/value_from.hpp>
20#include <boost/json/value_to.hpp>
21#include <boost/redis/resp3/serialization.hpp>
22
23namespace asio = boost::asio;
24namespace resp3 = boost::redis::resp3;
25using namespace boost::describe;
26using boost::redis::request;
27using boost::redis::response;
28using boost::redis::ignore_t;
29using boost::redis::config;
30using boost::redis::connection;
31using boost::redis::resp3::node_view;
32
33// Struct that will be stored in Redis using json serialization.
34struct user {
35 std::string name;
36 std::string age;
37 std::string country;
38};
39
40// The type must be described for serialization to work.
41BOOST_DESCRIBE_STRUCT(user, (), (name, age, country))
42
43// Boost.Redis customization points (example/json.hpp)
44void boost_redis_to_bulk(std::string& to, user const& u)
45{
46 resp3::boost_redis_to_bulk(to, boost::json::serialize(boost::json::value_from(u)));
47}
48
49void
50boost_redis_from_bulk(
51 user& u,
52 node_view const& node,
53 boost::system::error_code&)
54{
55 u = boost::json::value_to<user>(boost::json::parse(node.value));
56}
57
58auto co_main(config cfg) -> asio::awaitable<void>
59{
60 auto ex = co_await asio::this_coro::executor;
61 auto conn = std::make_shared<connection>(ex);
62 conn->async_run(cfg, {}, asio::consign(asio::detached, conn));
63
64 // user object that will be stored in Redis in json format.
65 user const u{"Joao", "58", "Brazil"};
66
67 // Stores and retrieves in the same request.
68 request req;
69 req.push("SET", "json-key", u); // Stores in Redis.
70 req.push("GET", "json-key"); // Retrieves from Redis.
71
72 response<ignore_t, user> resp;
73
74 co_await conn->async_exec(req, resp);
75 conn->cancel();
76
77 // Prints the first ping
78 std::cout
79 << "Name: " << std::get<1>(resp).value().name << "\n"
80 << "Age: " << std::get<1>(resp).value().age << "\n"
81 << "Country: " << std::get<1>(resp).value().country << "\n";
82}
83
84#endif // defined(BOOST_ASIO_HAS_CO_AWAIT)
boost::redis::connection
A basic_connection that type erases the executor.
Definition connection.hpp:1231
boost::redis::request
Creates Redis requests.
Definition request.hpp:46
boost::redis::ignore_t
std::decay_t< decltype(std::ignore)> ignore_t
Type used to ignore responses.
Definition ignore.hpp:31
boost::redis::resp3::node
basic_node< std::string > node
A node in the response tree that owns its data.
Definition node.hpp:60
boost::redis::response
std::tuple< adapter::result< Ts >... > response
Response with compile-time size.
Definition response.hpp:25
boost::redis::config
Configure parameters used by the connection classes.
Definition config.hpp:30
boost::redis::resp3::basic_node
A node in the response tree.
Definition node.hpp:28