Serialization
JSON
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main(int argc, char const *argv[])
{
std::string game_name = "the_last_samurai";
std::string game_version = "0.1.0";
std::string token = "cc1e240aacb641fed0050d2c2f16db918b4a7c10";
json j = {
{"command", "register"},
{"register", {
{"game_name", game_name},
{"game_version", game_version},
{"token", token},
}}
};
std::string data_to_send = j.dump();
std::cout << j.dump(4) << std::endl;
return 0;
}
Another example, how to prepare a map for a printable string that can be parsed by another application
std::string variablesToJson(const std::map<std::string, std::string>& map)
{
using json = nlohmann::json;
json jsonObject = json::object();
for (const auto& [name, value] : map) {
jsonObject[name] = value;
}
return " '" + jsonObject.dump() + "'";
}