MCPcopy Create free account
hub / github.com/boostorg/json / usingContextualConversions

Function usingContextualConversions

test/snippets.cpp:1014–1120  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1012}
1013
1014void
1015usingContextualConversions()
1016{
1017 using namespace user_ns;
1018 using namespace boost::json;
1019 {
1020// tag::doc_context_conversion_2[]
1021 ip_address addr( 192, 168, 10, 11 );
1022
1023 value jv = value_from( addr, as_string() );
1024 assert( jv == parse(R"( "192.168.10.11" )") );
1025
1026 ip_address addr2 = value_to< ip_address >( jv, as_string() );
1027 assert(std::equal(
1028 addr.begin(), addr.end(), addr2.begin() ));
1029// end::doc_context_conversion_2[]
1030 (void)addr2;
1031 }
1032
1033 {
1034// tag::doc_context_conversion_3[]
1035 std::map< std::string, ip_address > computers = {
1036 { "Alex", { 192, 168, 1, 1 } },
1037 { "Blake", { 192, 168, 1, 2 } },
1038 { "Carol", { 192, 168, 1, 3 } },
1039 };
1040 value jv = value_from( computers, as_string() );
1041 assert( jv == parse(
1042 "{ "
1043 " \"Alex\" : \"192.168.1.1\", "
1044 " \"Blake\": \"192.168.1.2\", "
1045 " \"Carol\": \"192.168.1.3\" "
1046 "} "
1047 ) );
1048// end::doc_context_conversion_3[]
1049 (void)jv;
1050 }
1051
1052 {
1053// tag::doc_context_conversion_5[]
1054 std::chrono::system_clock::time_point tp;
1055 value jv = value_from( tp, as_iso_8601() );
1056 assert( jv == parse(R"( "1970-01-01T00:00:00" )") );
1057// end::doc_context_conversion_5[]
1058 (void)jv;
1059 }
1060
1061 {
1062// tag::doc_context_conversion_7[]
1063 std::chrono::system_clock::time_point tp;
1064
1065 value jv = value_from( tp, date_format{ "%T %D", 18 } );
1066 assert( jv == parse(R"( "00:00:00 01/01/70" )") );
1067
1068 jv = value_from( tp, as_iso_8601() );
1069 assert( jv == parse(R"( "1970-01-01T00:00:00" )") );
1070// end::doc_context_conversion_7[]
1071 (void)jv;

Callers 1

runMethod · 0.85

Calls 7

value_fromFunction · 0.85
equalFunction · 0.85
as_iso_8601Class · 0.85
maps_as_objectsClass · 0.85
as_stringClass · 0.70
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected