xpack
v1.0.6
English
libbson-1.0
,需自行安装。未经充分测试,具体请参考READMElibmysqlclient-dev
,需自行安装。未经充分测试
#include <iostream>
#include "xpack/json.h" // Json包含这个头文件,xml则包含xpack/xml.h
using namespace std;
struct User {
int id;
string name;
XPACK(O(id, name)); // 添加宏定义XPACK在结构体定义结尾
};
int main(int argc, char *argv[]) {
User u;
string data = "{"id":12345, "name":"xpack"}";
xpack::json::decode(data, u); // json转结构体
cout<<u.id<<';'<<u.name<<endl;
string json = xpack::json::encode(u); // 结构体转json
cout<<json<<endl;
return 0;
}
目前支持下列容器(std)
宏XPACK里面,需要用字母将变量包含起来,比如XPACK(O(a,b)),XPACK可以包含多个字母,每个字母可以包含多个变量。目前支持的字母有:
json:_id
是合法的_id
是合法的#include <iostream>
#include "xpack/json.h"
using namespace std;
struct Test {
long uid;
string name;
XPACK(A(uid, "id"), O(name)); // "uid"的别名是"id"
};
int main(int argc, char *argv[]) {
Test t;
string json="{"id":123, "name":"Pony"}";
xpack::json::decode(json, t);
cout<<t.uid<<endl;
return 0;
}
#include <iostream>
#include "xpack/json.h"
using namespace std;
struct Test {
short ver:8;
short len:8;
string name;
XPACK(B(F(0), ver, len), O(name));
};
int main(int argc, char *argv[]) {
Test t;
string json="{"ver":4, "len":20, "name":"IPv4"}";
xpack::json::decode(json, t);
cout<<t.ver<<endl;
cout<<t.len<<endl;
return 0;
}
#include <iostream>
#include "xpack/json.h"
using namespace std;
struct P1 {
string mail;
XPACK(O(mail));
};
struct P2 {
long version;
XPACK(O(version));
};
struct Test:public P1, public P2 {
long uid;
string name;
XPACK(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"}";
xpack::json::decode(json, t);
cout<<t.mail<<endl;
cout<<t.version<<endl;
return 0;
}
#include <iostream>
#include "xpack/json.h"
using namespace std;
enum Enum {
X = 0,
Y = 1,
Z = 2,
};
struct Test {
string name;
Enum e;
XPACK(O(name), E(F(0), e));
};
int main(int argc, char *argv[]) {
Test t;
string json="{"name":"IPv4", "e":1}";
xpack::json::decode(json, t);
cout<<t.name<<endl;
cout<<t.e<<endl;
return 0;
}
应用场景
struct Time {
long ts; //unix timestamp
};
并不希望编码成{"ts":1218196800} 这种格式,而是希望编码成"2008-08-08 20:00:00"这种格式。
这里有两种方式:
两种方法本质上都是自己去实现encode/decode,但是有以下区别:
用这两个特性,可以实现一些比较灵活的编解码控制,比如这个例子实现了一个根据变量情况来编码的功能,如果Sub.type==1则encode seq1,否则encode seq2. __x_pack_decode
和__x_pack_encode
是XPACK宏给结构体添加的decode/encode函数,自定义编解码函数可以通过这些函数调用xpack默认的编解码功能。
可以使用自定义编解码来处理联合体,可以参考范例
#include <iostream>
#include "xpack/json.h"
using namespace std;
struct Test {
char name[64];
char email[64];
XPACK(O(name, email));
};
int main(int argc, char *argv[]) {
Test t;
string json="{"name":"Pony", "email":"[email protected]"}";
xpack::json::decode(json, t);
cout<<t.name<<endl;
cout<<t.email<<endl;
return 0;
}
operator [](size_t index)
用来取数组的第index个元素(从0开始)operator [](const char *key)
用来根据key取Object类型的元素#include <sys/time.h>
#include <iostream>
#include "xpack/json.h"
using namespace std;
/*
struct timeval {
time_t tv_sec;
suseconds_t tv_usec;
};
*/
// timeval is thirdparty struct
XPACK_OUT(timeval, O(tv_sec, tv_usec));
struct T {
int a;
string b;
timeval t;
XPACK(O(a, b, t));
};
int main(int argc, char *argv[]) {
T t;
T r;
t.a = 123;
t.b = "xpack";
t.t.tv_sec = 888;
t.t.tv_usec = 999;
string s = xpack::json::encode(t);
cout<<s<<endl;
xpack::json::decode(s, r);
cout<<r.a<<','<<r.b<<','<<r.t.tv_sec<<','<<r.t.tv_usec<<endl;
return 0;
}
<ids>
<ids>1</ids>
<ids>2</ids>
<ids>3</ids>
</ids>
<ids>
<id>1</id>
<id>2</id>
<id>3</id>
</ids>
<ids>1</ids>
<ids>2</ids>
<ids>3</ids>
<data>hello</data>
也可以解析成功static void decode(MYSQL_RES *result, T &val)
static void decode(MYSQL_RES *result, const std::string&field, T &val)