Skip to content

Add conversion example for a simple struct #1125

@pdimov

Description

@pdimov

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" ) );
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions