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

Function usingObjects

test/snippets.cpp:684–743  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

682//----------------------------------------------------------
683
684void
685usingObjects()
686{
687 {
688 // tag::snippet_objects_1[]
689
690 object obj1; // empty object, uses the default memory resource
691
692 object obj2( make_shared_resource<monotonic_resource>() ); // empty object, uses a counted monotonic resource
693
694 // end::snippet_objects_1[]
695 }
696 {
697 // tag::snippet_objects_2[]
698
699 object obj( {{"key1", "value1" }, { "key2", 42 }, { "key3", false }} );
700
701 // end::snippet_objects_2[]
702 }
703 {
704 // tag::snippet_objects_3[]
705
706 object obj;
707
708 obj.emplace( "key1", "value1" );
709 obj.emplace( "key2", 42 );
710 obj.emplace( "key3", false );
711
712 // end::snippet_objects_3[]
713 }
714 try
715 {
716 // tag::snippet_objects_4[]
717
718 object obj;
719
720 obj["key1"] = "value1";
721 obj["key2"] = 42;
722 obj["key3"] = false;
723
724 // The following line throws system_error, since the key does not exist
725 obj.at( "key4" );
726
727 // end::snippet_objects_4[]
728 }
729 catch (...)
730 {
731 }
732 {
733 // tag::snippet_objects_5[]
734
735 object obj{{"arr", {1, 11}}};
736 value& arr = obj.at("arr");
737 obj.emplace("added", "value"); // invalidates arr
738
739 // end::snippet_objects_5[]
740
741 (void)arr;

Callers 1

runMethod · 0.85

Calls 1

emplaceMethod · 0.80

Tested by

no test coverage detected