1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
//
// Copyright (c) 2021 Dmitry Arkhipov (grisumbras@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/json
//
// tag::doc_forward_conversion_1[]
namespace boost {
namespace json {
class value;
struct value_from_tag;
template< class T >
struct try_value_to_tag;
template< class T1, class T2 >
struct result_for;
template< class T >
void value_from( T&& t, value& jv );
template< class T >
typename result_for< T, value >::type
try_value_to( const value& jv );
}
}
// end::doc_forward_conversion_1[]
// tag::doc_forward_conversion_3[]
namespace boost {
namespace json {
class value;
struct value_from_tag;
template< class T >
struct try_value_to_tag;
template< class T1, class T2 >
struct result_for;
template< class T, class Context >
void value_from( T&& t, value& jv, const Context& ctx );
template< class T, class Context >
typename result_for< T, value >::type
try_value_to( const value& jv, const Context& ctx );
}
}
// end::doc_forward_conversion_3[]
#include "doc_types.hpp"
#include <system_error>
// tag::doc_forward_conversion_2[]
namespace user_ns
{
template< class JsonValue >
void tag_invoke(
const boost::json::value_from_tag&, JsonValue& jv, const ip_address& addr )
{
const unsigned char* b = addr.begin();
jv = { b[0], b[1], b[2], b[3] };
}
template< class JsonValue >
typename boost::json::result_for< ip_address, JsonValue >::type
tag_invoke(
const boost::json::try_value_to_tag< ip_address >&,
const JsonValue& jv )
{
using namespace boost::json;
if( !jv.is_array() )
return make_error_code( std::errc::invalid_argument );
auto const& arr = jv.get_array();
if( arr.size() != 4 )
return make_error_code( std::errc::invalid_argument );
auto oct1 = try_value_to< unsigned char >( arr[0] );
if( !oct1 )
return make_error_code( std::errc::invalid_argument );
auto oct2 = try_value_to< unsigned char >( arr[1] );
if( !oct2 )
return make_error_code( std::errc::invalid_argument );
auto oct3 = try_value_to< unsigned char >( arr[2] );
if( !oct3 )
return make_error_code( std::errc::invalid_argument );
auto oct4 = try_value_to< unsigned char >( arr[3] );
if( !oct4 )
return make_error_code( std::errc::invalid_argument );
return ip_address{ *oct1, *oct2, *oct3, *oct4 };
}
}
// end::doc_forward_conversion_2[]
// tag::doc_forward_conversion_3[]
namespace user_ns
{
struct as_string
{ };
template< class JsonValue >
void
tag_invoke(
const boost::json::value_from_tag&, JsonValue& jv,
const ip_address& addr,
const as_string& )
{
auto& js = jv.emplace_string();
js.resize( 4 * 3 + 3 + 1 ); // XXX.XXX.XXX.XXX\0
auto it = addr.begin();
auto n = std::snprintf(
js.data(), js.size(), "%hhu.%hhu.%hhu.%hhu", it[0], it[1], it[2], it[3] );
js.resize(n);
}
template< class JsonValue >
typename boost::json::result_for< ip_address, JsonValue >::type
tag_invoke(
const boost::json::try_value_to_tag< ip_address >&,
const JsonValue& jv,
const as_string& )
{
const auto* js = jv.if_string();
if( ! js )
return make_error_code( std::errc::invalid_argument );
unsigned char octets[4];
int result = std::sscanf(
js->data(), "%hhu.%hhu.%hhu.%hhu", octets, octets + 1, octets + 2, octets + 3 );
if( result != 4 )
return make_error_code( std::errc::invalid_argument );
return ip_address( octets[0], octets[1], octets[2], octets[3] );
}
}
// end::doc_forward_conversion_3[]
#include <boost/json/value_from.hpp>
#include <boost/json/value_to.hpp>
#include "test_suite.hpp"
namespace boost {
namespace json {
class doc_forward_conversion
{
public:
void
run()
{
value jv{ 212, 115, 81, 22 };
auto const addr = value_to< user_ns::ip_address >( jv );
BOOST_TEST( get<0>(addr) == 212 );
BOOST_TEST( get<1>(addr) == 115 );
BOOST_TEST( get<2>(addr) == 81 );
BOOST_TEST( get<3>(addr) == 22 );
value const jv2 = value_from( addr );
BOOST_TEST( jv == jv2 );
jv = value_from( addr, user_ns::as_string() );
BOOST_TEST( jv.is_string() );
string const& js = jv.get_string();
BOOST_TEST( js == "212.115.81.22" );
auto const addr2 = value_to< user_ns::ip_address >(
jv, user_ns::as_string() );
BOOST_TEST( get<0>(addr2) == 212 );
BOOST_TEST( get<1>(addr2) == 115 );
BOOST_TEST( get<2>(addr2) == 81 );
BOOST_TEST( get<3>(addr2) == 22 );
}
};
TEST_SUITE(doc_forward_conversion, "boost.json.doc_forward_conversion");
} // namespace json
} // namespace boost
|