Skip to content

Arbitrary types conversions for a generic list type variable #4267

Answered by gregmarr
mohit-mittal-cosm asked this question in Q&A
Discussion options

You must be logged in to vote

The DEFINE_TYPE macros make this very simple:

namespace ns {
    struct C {
        std::string currency;
    };

    NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(C, currency);

    struct A {
        std::list<C> object;
    };

    NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(A, object);
}

int main()
{
using namespace nlohmann::literals;
json j3 = R"(
{
    "object": [
    {
      "currency": "USD"
    },
    {
      "currency": "INR"
    }]
}
)"_json;

auto p2 = j3.get<ns::A>();
}

Replies: 2 comments 6 replies

Comment options

You must be logged in to vote
4 replies
@gregmarr
Comment options

Answer selected by mohit-mittal-cosm
@mohit-mittal-cosm
Comment options

@gregmarr
Comment options

@ThePython10110
Comment options

Comment options

You must be logged in to vote
2 replies
@mohit-mittal-cosm
Comment options

@mohit-mittal-cosm
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants