Generic value from JSON to function #3779
Unanswered
paulocoutinhox
asked this question in
Q&A
Replies: 2 comments 4 replies
-
The usual recommendation would be to implement a conversion function for |
Beta Was this translation helpful? Give feedback.
2 replies
-
How about something like this? void test(bool value) {
std::cout << "Received Value: " << value << std::endl;
}
int main() {
std::map<std::string, std::function<void(const json &)>> flist;
flist["test"] = [](const json &j) {
bool value = j.get<bool>();
test(value);
};
json j{{"function", "test"}, {"param", true}};
auto function = j["function"].get<std::string>();
flist[function](j["param"]);
} |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
I have a small problem here that im searching a lot to solve.
I have this function:
And i have a generic code the read a JSON, get a function name and the parameter value, that can be bool, i32, or custom class.
But nlohmann json don't know
std::any
and i can't usebool
because only my functiontest
that can receive something that it will cast to correct value type. And the functiontest
can't receive a nlohmann object or string to be parsed after, because it need receive an independent value to be casted after be called.The error is:
Can anyone help me?
Beta Was this translation helpful? Give feedback.
All reactions