Aedis 1.0.0  
High level Redis client
serialization.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 <algorithm>
8 #include <cstdint>
9 #include <iostream>
10 #include <set>
11 #include <iterator>
12 #include <string>
13 #include <boost/json.hpp>
14 #include <boost/json/src.hpp>
15 #include <aedis.hpp>
16 #include "print.hpp"
17 
18 // Include this in no more than one .cpp file.
19 #include <aedis/src.hpp>
20 
21 namespace net = boost::asio;
23 using aedis::adapt;
25 using namespace boost::json;
26 
27 struct user {
28  std::string name;
29  std::string age;
30  std::string country;
31 };
32 
33 void tag_invoke(value_from_tag, value& jv, user const& u)
34 {
35  jv =
36  { {"name", u.name}
37  , {"age", u.age}
38  , {"country", u.country}
39  };
40 }
41 
42 template<class T>
43 void extract(object const& obj, T& t, boost::string_view key)
44 {
45  t = value_to<T>(obj.at(key));
46 }
47 
48 user tag_invoke(value_to_tag<user>, value const& jv)
49 {
50  user u;
51  object const& obj = jv.as_object();
52  extract(obj, u.name, "name");
53  extract(obj, u.age, "age");
54  extract(obj, u.country, "country");
55  return u;
56 }
57 
58 // Serializes
59 void to_bulk(std::string& to, user const& u)
60 {
61  aedis::resp3::to_bulk(to, serialize(value_from(u)));
62 }
63 
64 // Deserializes
65 void from_bulk(user& u, boost::string_view sv, boost::system::error_code&)
66 {
67  value jv = parse(sv);
68  u = value_to<user>(jv);
69 }
70 
71 std::ostream& operator<<(std::ostream& os, user const& u)
72 {
73  os << "Name: " << u.name << "\n"
74  << "Age: " << u.age << "\n"
75  << "Country: " << u.country;
76 
77  return os;
78 }
79 
80 bool operator<(user const& a, user const& b)
81 {
82  return std::tie(a.name, a.age, a.country) < std::tie(b.name, b.age, b.country);
83 }
84 
85 int main()
86 {
87  net::io_context ioc;
88  connection db{ioc};
89 
90  std::set<user> users
91  {{"Joao", "58", "Brazil"} , {"Serge", "60", "France"}};
92 
93  request req;
94  req.push("HELLO", 3);
95  req.push_range("SADD", "sadd-key", users); // Sends
96  req.push("SMEMBERS", "sadd-key"); // Retrieves
97  req.push("QUIT");
98 
99  std::tuple<aedis::ignore, int, std::set<user>, std::string> resp;
100 
101  db.async_run(req, adapt(resp), [](auto ec, auto) {
102  std::cout << ec.message() << std::endl;
103  });
104 
105  ioc.run();
106 
107  // Print
108  print(std::get<2>(resp));
109 }
A high level connection to Redis.
Definition: connection.hpp:45
Creates Redis requests.
Definition: request.hpp:172
void push_range(boost::string_view cmd, Key const &key, Range const &range)
Appends a new command to the end of the request.
Definition: request.hpp:308
void push(boost::string_view cmd, Ts const &... args)
Appends a new command to the end of the request.
Definition: request.hpp:203
std::ostream & operator<<(std::ostream &os, type t)
Writes the type to the output stream.
auto adapt() noexcept
Creates an adapter that ignores responses.
Definition: adapt.hpp:140