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

    pFad - Phonifier reborn

    Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

    Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy