]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/json/test/doc_forward_conversion.cpp
2 // Copyright (c) 2021 Dmitry Arkhipov (grisumbras@gmail.com)
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)
7 // Official repository: https://github.com/boostorg/json
10 //[doc_forward_conversion_1
16 struct value_from_tag
;
22 T
value_to( const value
& v
);
25 void value_from( T
&& t
, value
& jv
);
33 namespace thirdparty
{
43 customer( std::uint64_t i
, const std::string
& n
, bool l
)
44 : id( i
), name( n
), late( l
) { }
47 } // namespace thirdparty
49 //[doc_forward_conversion_2
50 namespace thirdparty
{
52 template< class JsonValue
>
53 customer
tag_invoke( const boost::json::value_to_tag
<customer
>&, const JsonValue
& jv
)
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
);
61 template< class JsonValue
>
62 void tag_invoke( const boost::json::value_from_tag
&, JsonValue
& jv
, const customer
& c
)
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"]);
74 #include <boost/json/value_from.hpp>
75 #include <boost/json/value_to.hpp>
77 #include "test_suite.hpp"
82 class doc_forward_conversion
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" );
94 value
const jv2
= value_from( c
);
95 BOOST_TEST( jv
== jv2
);
99 TEST_SUITE(doc_forward_conversion
, "boost.json.doc_forward_conversion");