Skip to content

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

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 5 replies

You must be logged in to vote
2 replies
@krishna116

@falbrechtskirchinger

You must be logged in to vote
3 replies
@krishna116

@j1ah0ng

@gregmarr

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
5 participants