]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/json/test/doc_forward_conversion.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / json / test / doc_forward_conversion.cpp
1 //
2 // Copyright (c) 2021 Dmitry Arkhipov (grisumbras@gmail.com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/boostorg/json
8 //
9
10 //[doc_forward_conversion_1
11 namespace boost {
12 namespace json {
13
14 class value;
15
16 struct value_from_tag;
17
18 template< class T >
19 struct value_to_tag;
20
21 template< class T >
22 T value_to( const value& v );
23
24 template<class T>
25 void value_from( T&& t, value& jv );
26
27 }
28 }
29 //]
30
31 #include <string>
32
33 namespace thirdparty {
34
35 struct customer
36 {
37 std::uint64_t id;
38 std::string name;
39 bool late;
40
41 customer() = default;
42
43 customer( std::uint64_t i, const std::string& n, bool l )
44 : id( i ), name( n ), late( l ) { }
45 };
46
47 } // namespace thirdparty
48
49 //[doc_forward_conversion_2
50 namespace thirdparty {
51
52 template< class JsonValue >
53 customer tag_invoke( const boost::json::value_to_tag<customer>&, const JsonValue& jv )
54 {
55 std::uint64_t id = boost::json::value_to<std::uint64_t>( jv.at( "id" ) );
56 std::string name = boost::json::value_to< std::string >( jv.at( "name" ) );
57 bool late = boost::json::value_to<bool>( jv.at( "late" ) );
58 return customer(id, std::move(name), late);
59 }
60
61 template< class JsonValue >
62 void tag_invoke( const boost::json::value_from_tag&, JsonValue& jv, const customer& c)
63 {
64 auto& obj = jv.emplace_object();
65 boost::json::value_from(c.id, obj["id"]);
66 boost::json::value_from(c.name, obj["name"]);
67 boost::json::value_from(c.late, obj["late"]);
68 }
69
70 }
71 //]
72
73
74 #include <boost/json/value_from.hpp>
75 #include <boost/json/value_to.hpp>
76
77 #include "test_suite.hpp"
78
79
80 BOOST_JSON_NS_BEGIN
81
82 class doc_forward_conversion
83 {
84 public:
85 void
86 run()
87 {
88 value const jv{ { "id", 1 }, { "name", "Carl" }, { "late", true } };
89 auto const c = value_to<thirdparty::customer>( jv );
90 BOOST_TEST( c.id == 1 );
91 BOOST_TEST( c.name == "Carl" );
92 BOOST_TEST( c.late );
93
94 value const jv2 = value_from( c );
95 BOOST_TEST( jv == jv2 );
96 }
97 };
98
99 TEST_SUITE(doc_forward_conversion, "boost.json.doc_forward_conversion");
100
101 BOOST_JSON_NS_END