What is the best practice to serialize variant or variable object? #3436
-
Sometime I need serialize variant or variable object, but how to do it and what is the best practice?
Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
You need to provide to_json/from_json functions for class ServerReply, see #2208 for an example. |
Beta Was this translation helpful? Give feedback.
-
There's no automatic handling of void to_json(json &j, const ServerReply &sr)
{
std::visit([&](auto &v) {
using nlohmann::to_json;
to_json(j, v);
}, sr.data);
}
void from_json(const json &j, ServerReply &sr)
{
std::visit([&](auto &v) {
using nlohmann::from_json;
from_json(j, v);
}, sr.data);
} |
Beta Was this translation helpful? Give feedback.
There's no automatic handling of
std::variant
(yet). You need to provide your own serialization/deserialization functions.Example: https://godbolt.org/z/369o1j1q9