代码拉取完成,页面将自动刷新
#include <iostream>
#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<User> 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", "jack@x2struct.com");
g.members[1] = User(2, "Pony", "pony@x2struct.com");
string json = x2struct::X::tojson(g); // 结构体转json
cout<<json<<endl;
Group n;
x2struct::X::loadjson(json, n, false); // json转结构体
cout<<n.name<<endl;
vector<int> vi;
x2struct::X::loadjson("[1,2,3]", vi, false); // 直接加载vector
cout<<vi.size()<<','<<vi[1]<<endl;
map<int, int> m;
x2struct::X::loadjson("{\"1\":10, \"2\":20}", m, false); // 直接加载map
cout<<m.size()<<','<<m[2]<<endl;
return 0;
}
步骤有:
#include <iostream>
#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 <iostream>
#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<<t.uid<<endl;
return 0;
}
#include <iostream>
#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\":\"pony@x2struct.com\", \"version\":2019, \"id\":123, \"name\":\"Pony\"}";
x2struct::X::loadjson(json, t, false);
cout<<t.mail<<endl;
cout<<t.version<<endl;
return 0;
}
#include <iostream>
#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<<t.ver<<endl;
cout<<t.len<<endl;
return 0;
}
#include <iostream>
#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<<t.task.name<<endl;
}
修改config.h,使能XTOSTRUCT_SUPPORT_CHAR_ARRAY这个宏即可
#include <iostream>
#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<<t.xhas("id")<<endl; // 用xhas检查是否存在
cout<<t.xhas("name")<<endl;
return 0;
}
// nt.cpp
// g++ -o t nt.cpp -std=c++11
#include <iostream>
#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<<t.name<<endl;
return 0;
}
#include <iostream>
#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<<t.ip.ip<<','<<t.mask.ip<<endl;
cout<<x2struct::X::tojson(t)<<endl;
return 0;
}
#include <sys/time.h>
#include <iostream>
#include "x2struct/x2struct.hpp"
using namespace std;
/*
struct timeval {
time_t tv_sec;
suseconds_t tv_usec;
};
*/
// timeval is thirdparty struct
XTOSTRUCT_OUT(timeval, O(tv_sec, tv_usec));
struct T {
int a;
string b;
timeval t;
XTOSTRUCT(O(a, b, t));
};
int main(int argc, char *argv[]) {
T t;
T r;
t.a = 123;
t.b = "x2struct";
t.t.tv_sec = 888;
t.t.tv_usec = 999;
string s = x2struct::X::tojson(t);
cout<<s<<endl;
x2struct::X::loadjson(s, r, false);
cout<<r.a<<','<<r.b<<','<<r.t.tv_sec<<','<<r.t.tv_usec<<endl;
return 0;
}
// go.cpp
// g++ -o t go.cpp -DXTOSTRUCT_GOCODE
#include <iostream>
#include "x2struct/x2struct.hpp"
using namespace std;
struct Test {
int64_t id;
vector<string> names;
XTOSTRUCT(O(id, names));
};
int main(int argc, char *argv[]) {
Test t;
cout<<x2struct::X::togocode(t, true, true, true)<<endl; // 最后三个参数用于控制(json/bson/xml) tag的生成
return 0;
}
输出是:
type Test struct {
Id int64 `json:"id" bson:"id" xml:"id"`
Names []string `json:"names" bson:"names" xml:"names"`
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。