json icon indicating copy to clipboard operation
json copied to clipboard

Add conversion example for a simple struct

Open pdimov opened this issue 1 month ago • 0 comments

E.g.

struct X
{
    std::vector<std::string> a;
    int b;
};

Be sure to prominently explain that the natural implementation of value_from

void tag_invoke( boost::json::value_from_tag const&, boost::json::value& jv, X const& x )
{
    jv = {
        { "a", boost::json::value_from( x.a ) },
        { "b", boost::json::value_from( x.b ) }
    };
}

is wrong because the intermediate calls to value_from use the default storage, which introduces an unnecessary copy if jv uses a different storage.

Instead, what needs to be written is either

void tag_invoke( boost::json::value_from_tag const&, boost::json::value& jv, X const& x )
{
    jv = {
        { "a", boost::json::value_from( x.a, jv.storage() ) },
        { "b", boost::json::value_from( x.b, jv.storage() ) }
    };
}

or

void tag_invoke( boost::json::value_from_tag const&, boost::json::value& jv, X const& x )
{
    jv = {
        { "a", nullptr },
        { "b", nullptr }
    };

    boost::json::value_from( x.a, jv.at( "a" ) );
    boost::json::value_from( x.b, jv.at( "b" ) );
}

pdimov avatar Dec 01 '25 17:12 pdimov