iguana::json/xml 序列化框架
环境:win10 vs2017 c++17 boost
1、下载源码:https://github.com/qicosmos/iguana
2、创建工程,包含源码目录、boost库目录;boost库;c++17
//json结构模板 struct person { std::string name; int age; }; REFLECTION(person, name, age) struct one_t { int id; }; REFLECTION(one_t, id); struct two { std::string name; one_t one; int age; }; REFLECTION(two, name, one, age); struct composit_t { int a; std::vector<std::string> b; int c; std::map<int, int> d; std::unordered_map<int, int> e; double f; std::list<one_t> g; }; REFLECTION(composit_t, a, b, c, d, e, f, g);
void test_json() { person p; const char * json = "{ \"name\" : \"tom\", \"age\" : 20}"; //string转对象 auto r = iguana::json::from_json0(p, json); std::cout << p.age << " " << p.name << std::endl; iguana::string_stream ss; //对象转string iguana::json::to_json(ss, p); std::cout << ss.str() << std::endl; //复杂结构转string one_t one = { 2 }; composit_t composit = { 1,{ "tom", "jack" }, 3,{ { 2,3 } },{ { 5,6 } }, 5.3,{ one } }; iguana::string_stream sst; iguana::json::to_json(sst, composit); std::cout << sst.str() << std::endl; //string转对象 const char* str_comp = R"({"b":["tom", "jack"], "a":1, "c":3, "e":{"3":4}, "d":{"2":3,"5":6},"f":5.3,"g":[{"id":1},{"id":2}])"; composit_t comp; iguana::json::from_json0(comp, str_comp); std::cout << comp.a << " " << comp.f << std::endl; }
void test_xml() { person p = { "admin", 20 }; iguana::string_stream ss; iguana::xml::to_xml(ss, p); std::cout << ss.str() << std::endl; //<?xml version = "1.0" encoding="UTF-8"><name>admin</name><age>20</age> ss.clear(); two t = { "test", {2}, 4 }; iguana::xml::to_xml(ss, t); auto result = ss.str(); std::cout << result << std::endl; //<?xml version = "1.0" encoding="UTF-8"><name>test</name><one><id>2</id></one><age>4</age> std::string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"> <name>buke</name> <one><id>1</id></one> <age>2</age>"; two t1; iguana::xml::from_xml(t1, xml.data(), xml.length()); std::cout << t1.name << std::endl; }