Skip to content

What is the best practice to serialize variant or variable object? #3436

Discussion options

You must be logged in to vote

There's no automatic handling of std::variant (yet). You need to provide your own serialization/deserialization functions.
Example: https://godbolt.org/z/369o1j1q9

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);
}

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
2 replies
@krishna116
Comment options

@falbrechtskirchinger
Comment options

Comment options

You must be logged in to vote
1 reply
@krishna116
Comment options

Answer selected by krishna116
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