结构体包含由子结构体转换成的json字符串,无法反解回结构体
stupied-ies opened this issue · 0 comments
stupied-ies commented
以“六年级-三班-某学生”为例,学生由结构体转换为json字符,作为班级的成员; 班级由结构体转换为json字符,作为年级的成员; 由年级json字符无法解析出classParm成员变量,解析出的结构只有"{"
#include "mainwindow.h"
#include <QApplication>
#include <iostream>
#include "../iguana/iguana/json.hpp"
// 六年级-三班-某学生
struct Person
{
std::string name;
int age;
};
REFLECTION(Person, name, age);
struct Class
{
int classID;
std::string personParm;
};
REFLECTION(Class, classID, personParm)
struct Grade
{
int gradeID;
std::string classParm;
};
REFLECTION(Grade, gradeID, classParm)
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// MainWindow w;
// w.show();
Person p;
p.age = 10;
p.name = "suli";
iguana::string_stream pp;
iguana::json::to_json(pp, p);
std::cout << pp.str() << std::endl;
Person res_p;
iguana::json::from_json(res_p, pp.str().c_str());
Class c;
c.classID = 3;
c.personParm = pp.str();
iguana::string_stream cc;
iguana::json::to_json(cc, c);
std::cout << cc.str() << std::endl;
//const char* dd= "{\"classID\":3,\"personParm\":\"{\\\"name\\\":\\\"suli\\\",\\\"age\\\":10}\"}";
Class res_c;
iguana::json::from_json(res_c, cc.str().c_str());
Grade g;
g.gradeID = 6;
g.classParm = cc.str();
iguana::string_stream gg;
iguana::json::to_json(gg, g);
std::cout << gg.str().c_str() << std::endl;
Grade res_g;
iguana::json::from_json(res_g, gg.str().c_str());
Grade res;
iguana::json::from_json(res, gg.str().c_str());
return a.exec();
}