Skip to content

Recover ability to initialize json::value directly from C++ objects by way of automatically using value_from #1126

@pdimov

Description

@pdimov

As shown in #1125, the natural implementation of value_from for simple structs

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 subtly inefficient, even though it compiles and works.

To avoid this trap, we should re-enable the functionality of assigning to json::value directly, as in

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

In addition to being correct, this is also much simpler.

This used to work in 1.80 and was intentionally broken in 1.81 (#823) because "it was never supposed to work." (But it probably was supposed to work, for the reason above.)

Note that the solution proposed in #823 has the same problem as the above example.

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