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

Function usingExchange

test/snippets.cpp:816–919  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

814//]
815
816void
817usingExchange()
818{
819 {
820 // tag::snippet_conv_1[]
821
822 std::vector< int > v1{ 1, 2, 3, 4 };
823
824 // Convert the vector to a JSON array
825 value jv = value_from( v1 );
826 assert( serialize( jv ) == R"([1,2,3,4])" );
827
828 // Convert back to vector< int >
829 std::vector< int > v2 = value_to< std::vector< int > >( jv );
830 assert( v1 == v2 );
831
832 // end::snippet_conv_1[]
833
834 (void)v2;
835 }
836 {
837 using namespace user_ns2;
838
839 // tag::snippet_tag_invoke_3[]
840 std::map< std::string, ip_address > computers = {
841 { "Alex", { 192, 168, 1, 1 } },
842 { "Blake", { 192, 168, 1, 2 } },
843 { "Carol", { 192, 168, 1, 3 } },
844 };
845
846 // conversions are applied recursively;
847 // the key type and value type will be converted
848 // using value_from as well
849 value jv = value_from( computers );
850 assert( jv.is_object() );
851
852 value serialized = parse(R"(
853 {
854 "Alex": [ 192, 168, 1, 1 ],
855 "Blake": [ 192, 168, 1, 2 ],
856 "Carol": [ 192, 168, 1, 3 ]
857 }
858 )");
859 assert( jv == serialized );
860 // end::snippet_tag_invoke_3[]
861
862 (void)jv;
863 }
864 {
865 using namespace user_ns2;
866
867 // tag::snippet_tag_invoke_2[]
868 ip_address addr = { 127, 0, 0, 12 };
869 value jv = value_from( addr );
870 assert( serialize( jv ) == R"([127,0,0,12])" );
871
872 // Convert back to IP address
873 ip_address addr2 = value_to< ip_address >( jv );

Callers 1

runMethod · 0.85

Calls 7

value_fromFunction · 0.85
serializeFunction · 0.85
equalFunction · 0.85
is_objectMethod · 0.80
valueClass · 0.70
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected