Crow
Crow copied to clipboard
Is there a method to create nested json other than crow::json::load( string )
i hope to create nested json from a unordered_map of string -> struct( int start, int end ) to json response such as
{ "test1":{ "start":10, "end":20 }, "test2":{ "start":20, "end":50 } }
Currently i could make this by concating my unordered_map elements to json formed std::string, then load with crow::json::load.
Is there any methods other than concat strings?
Yes, you can use crow::json::wvalue::object
for this.
#include "crow.h"
#include <unordered_map>
#include <iostream>
struct StartEnd
{
int start, end;
};
int main()
{
std::unordered_map < std::string, StartEnd > m{ {"test1", {10, 20}}, {"test2", {20, 50}} };
crow::json::wvalue::object input;
for (const auto& p : m)
{
const auto& key = p.first;
const auto& start = p.second.start;
const auto& end = p.second.end;
crow::json::wvalue value{
{"start", start},
{"end", end}
};
input.insert({key, value});
}
crow::json::wvalue response{ input };
std::cout << response.dump() << '\n';
return 0;
}