json
json copied to clipboard
Question: nesting binding::object in one binding trait definition possible/supported?
I would like to create a nested json structure of the members in a struct when defining the trait using binding traits. Something like this, which obviously fails to compile:
struct test {
int m_id;
constexpr std::string identifier() const noexcept { return "test"; }
};
template<typename T, typename = void>
struct my_traits
: public tao::json::traits<T>
{};
template<>
struct my_traits<test>
: public tao::json::binding::array<
TAO_JSON_BIND_ELEMENT(tao::json::binding::object<TAO_JSON_BIND_REQUIRED("name", &test::identifier)>)
>
{};
The element
template is declared template<auto P>
and gcc complains:
expected a constant of type ‘auto’, got ‘tao::json::binding::object<tao::json::binding::member<tao::json::binding::member_kind::required,....
So how do you put a json object into an array in one traits definition?
Or, same question basically, if I have an object (like in my last question) instead of an array and I want to group some members:
template<>
struct my_traits<test>
: public tao::json::binding::object<
TAO_JSON_BIND_REQUIRED("id", &test::m_id),
TAO_JSON_BIND_REQUIRED("turnus", tao::json::binding::object<
TAO_JSON_BIND_OPTIONAL("einheit", &test::m_turnusEinheit),
TAO_JSON_BIND_OPTIONAL("anzahl", &test::m_turnusAnzahl),
TAO_JSON_BIND_OPTIONAL("tag", &test::m_turnusTag)
>
)
>
{};
so that the result could be:
{
"id": 1,
"turnus": {
"einheit": "N",
"anzahl": 1
}
}
For now the answer is that this is not directly supported, the expectation is that something supposed to be a sub-object in JSON also is a sub-object in C++. That said, I'll look into whether this can be made possible without rewriting half the library...
There is no quick and simple solution like there was for #146, but I won't give up just yet.
thanks a lot for trying! :grin: