Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
michael-brade opened this issue Sep 11, 2023 · 3 comments
Assignees
Labels

Comments

@michael-brade
Copy link

michael-brade commented Sep 11, 2023

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
  }
}
@ColinH ColinH self-assigned this Sep 17, 2023
@ColinH
Copy link
Member

ColinH commented Sep 17, 2023

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

@ColinH
Copy link
Member

ColinH commented Sep 20, 2023

There is no quick and simple solution like there was for #146, but I won't give up just yet.

@michael-brade
Copy link
Author

thanks a lot for trying! 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants