In the below example I parse a json object using boost::json
.
When I print the type of the returned boost::json::value
it is of type object
, as expected.
I then have 2 classes which are identical in every way, other than in BraceInit
I initialise my member boost::json::value
using brace initialisation and in ParenInit
I initialise my member boost::json::value
using parens.
Using brace initialisation causes my object
to be converted into an array
, of size 1, containing my original object
.
#include <iostream>
#include <boost/json.hpp>
namespace json = boost::json;
void print_type(const json::value& jv)
{
switch (jv.kind())
{
case json::kind::object: std::cout << "object\n"; break;
case json::kind::array: std::cout << "array\n"; break;
default: std::cout << "other\n"; break;
}
};
struct BraceInit
{
BraceInit(json::value jv)
: _jv{jv}
{
print_type(_jv);
}
json::value _jv;
};
struct ParenInit
{
ParenInit(json::value jv)
: _jv(jv)
{
print_type(_jv);
}
json::value _jv;
};
int main()
{
const std::string str = R"({ "foo": "bar" })";
const json::value jv = json::parse(str);
print_type(jv);
BraceInit b(jv);
ParenInit p(jv);
return 0;
}
Output:
object array object
What is happening here?
Is my "brace initialisation" actually not doing brace initialisation as I expect, but rather creating an std::initializer_list
of size 1?