#include <cppcms/json.h>
#include <iostream>
#include <sstream>
struct person {
std::string name;
double salary;
std::vector<std::string> kids_names;
};
namespace json {
template<>
struct traits<person> {
static person get(value const &v)
{
person p;
throw bad_value_cast();
p.name=v.get<std::string>("name");
p.salary=v.get<double>("salary");
p.kids_names=
v.get<std::vector<std::string> >("kids_names");
return p;
}
static void set(value &v,person const &in)
{
v.set("name",in.name);
v.set("salary",in.salary);
v.set("kids_names",in.kids_names);
}
};
}
}
int main()
{
my_object["name"]="Moshe";
my_object["salary"]=1000.0;
my_object["kids_names"][0]="Yossi";
my_object["kids_names"][1]="Yonni";
my_object["data"]["weight"]=85;
my_object["data"]["height"]=1.80;
std::string name=my_object.
get<std::string>(
"name");
double salary = my_object.
get<
double>(
"salary");
int weight = my_object.
get<
int>(
"data.weight");
double height = my_object.
get<
double>(
"data.height");
std::string eyes_color = my_object.
get(
"data.eyes_color",
"brown");
person moshe = my_object.
get_value<person>();
std::cout << other << std::endl;
std::stringstream tmp;
tmp << my_object;
}