代码github地址
目录
基本用法
必需节点
别名
继承
位域
条件加载
char数组
检查是否存在
局部类
自定义类型
格式化缩进
xml bson libconfig
生成Golang结构体
说明
#include
#include "x2struct/x2struct.hpp" // 包含这个头文件
using namespace std;
struct User {
int64_t id;
string name;
string mail;
User(int64_t i=0, const string& n="", const string& m=""):id(i),name(n),mail(m){}
XTOSTRUCT(O(id, name, mail)); // 添加宏定义XTOSTRUCT在结构体定义结尾
};
struct Group {
string name;
int64_t master;
vector members;
XTOSTRUCT(O(name, master, members)); // 添加宏定义XTOSTRUCT在结构体定义结尾
};
int main(int argc, char *argv[]) {
Group g;
g.name = "C++";
g.master = 2019;
g.members.resize(2);
g.members[0] = User(1, "Jack", "[email protected]");
g.members[1] = User(2, "Pony", "[email protected]");
string json = x2struct::X::tojson(g); // 结构体转json
cout< vi;
x2struct::X::loadjson("[1,2,3]", vi, false); // 直接加载vector
cout< m;
x2struct::X::loadjson("{\"1\":10, \"2\":20}", m, false); // 直接加载map
cout<
步骤有:
#include
#include "x2struct/x2struct.hpp"
using namespace std;
struct Test {
int64_t id;
string name;
XTOSTRUCT(M(id), O(name)); // "id"是必需节点,用M包含,"name"是可选节点,用"O"包含
};
int main(int argc, char *argv[]) {
Test t;
string json="{\"name\":\"Pony\"}";
x2struct::X::loadjson(json, t, false); // 这里将会抛异常,因为"id"是必需节点,但是json里面没有
return 0;
}
#include
#include "x2struct/x2struct.hpp"
using namespace std;
struct Test {
int64_t uid;
string name;
XTOSTRUCT(A(uid, "id"), O(name)); // "uid"的别名是"id"
};
int main(int argc, char *argv[]) {
Test t;
string json="{\"id\":123, \"name\":\"Pony\"}";
x2struct::X::loadjson(json, t, false);
cout<
#include
#include "x2struct/x2struct.hpp"
using namespace std;
struct P1 {
string mail;
XTOSTRUCT(O(mail));
};
struct P2 {
int64_t version;
XTOSTRUCT(O(version));
};
struct Test:public P1, public P2 {
int64_t uid;
string name;
XTOSTRUCT(I(P1, P2), O(uid, name));
};
int main(int argc, char *argv[]) {
Test t;
string json="{\"mail\":\"[email protected]\", \"version\":2019, \"id\":123, \"name\":\"Pony\"}";
x2struct::X::loadjson(json, t, false);
cout<
#include
#include "x2struct/x2struct.hpp"
using namespace std;
struct Test {
int16_t ver:8;
int16_t len:8;
string name;
XTOSTRUCT(B(ver, len), O(name));
};
int main(int argc, char *argv[]) {
Test t;
string json="{\"ver\":4, \"len\":20, \"name\":\"IPv4\"}";
x2struct::X::loadjson(json, t, false);
cout<
#include
#include "x2struct/x2struct.hpp"
using namespace std;
struct Task {
int id;
string name;
XTOSTRUCT(O(id, name));
};
struct Test {
int tid;
Task task;
XTOSTRUCT(C(task), O(tid, task)); // 在XTOSTRUCT里面,task既包含在"C"里面,也包含在"O"里面
/*
XTOSTRUCT_CONDITION 这个宏包含两个参数,一个类名,一个变量名
XTOSTRUCT_CONDITION 定义了一个函数,函数有一个变量,名为"obj"指向的是变量对应的xdoc
"task"在json里面是一个数组,这里只加载那个XTOSTRUCT_CONDITION返回true的那个,这里当tid等于id时返回true
*/
XTOSTRUCT_CONDITION(Test, task) {
int id;
// only load the one that Task.id is equal to Test.tid
return obj.convert("id", id)&&id==this->tid;
}
};
int main(int argc, char *argv[]) {
string s = "{\"tid\":2019,\"task\":[{\"id\":2018,\"name\":\"hello\"},{\"id\":2019,\"name\":\"world\"}]}";
Test t;
x2struct::X::loadjson(s, t, false);
cout<
#include
#include "x2struct/x2struct.hpp"
using namespace std;
struct Test {
int64_t id;
string name;
XTOSTRUCT(O(id, name));
};
int main(int argc, char *argv[]) {
Test t;
string json="{\"name\":\"Pony\"}";
x2struct::X::loadjson(json, t, false, true); // 如果需要用这个功能,第四个参数需要是true
cout<
// nt.cpp
// g++ -o t nt.cpp -std=c++11
#include
#include "x2struct/x2struct.hpp"
using namespace std;
int main(int argc, char *argv[]) {
struct Test {
int64_t id;
string name;
XTOSTRUCT_NT(Json)(O(id, name));
};
Test t;
string json="{\"id\":123, \"name\":\"Pony\"}";
x2struct::X::loadjson(json, t, false);
cout<
#include
#include "x2struct/x2struct.hpp"
using namespace std;
// 只是一个范例,没做错误处理
struct _XIPv4 {
uint32_t ip;
// 实现format函数
std::string format() const {
char buf[64];
int len = sprintf(buf, "%u.%u.%u.%u", ip>>24, 0xff&(ip>>16), 0xff&(ip>>8), 0xff&ip);
return string(buf, len);
}
// 实现parse函数
void parse(const std::string& d) {
uint32_t u[4];
sscanf(d.c_str(), "%u.%u.%u.%u", &u[0], &u[1], &u[2], &u[3]);
ip = (u[0]<<24)+(u[1]<<16)+(u[2]<<8)+u[3];
}
};
// typedef 定义
typedef x2struct::XType<_XIPv4> XIPv4;
struct Test {
XIPv4 ip;
XIPv4 mask;
XTOSTRUCT(O(ip, mask));
};
int main(int argc, char *argv[]) {
Test t;
// 在json里面自定义类型是string
string json="{\"ip\":\"192.168.1.2\", \"mask\":\"255.255.255.0\"}";
x2struct::X::loadjson(json, t, false);
cout<
// go.cpp
// g++ -o t go.cpp -DXTOSTRUCT_GOCODE
#include
#include "x2struct/x2struct.hpp"
using namespace std;
struct Test {
int64_t id;
vector names;
XTOSTRUCT(O(id, names));
};
int main(int argc, char *argv[]) {
Test t;
cout<
输出是:
type Test struct {
Id int64 `json:"id" bson:"id" xml:"id"`
Names []string `json:"names" bson:"names" xml:"names"`
}