Skip to content

Question: nesting binding::object in one binding trait definition possible/supported? #145

@michael-brade

Description

@michael-brade

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
  }
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions