整理代码
This commit is contained in:
commit
c7b4e178ec
364 changed files with 59691 additions and 0 deletions
27
util/xpack/example/alias.cpp
Executable file
27
util/xpack/example/alias.cpp
Executable file
|
|
@ -0,0 +1,27 @@
|
|||
#include <iostream>
|
||||
#include "xpack/json.h"
|
||||
#include "xpack/xml.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Test {
|
||||
long uid;
|
||||
string name;
|
||||
XPACK(A(uid, "id xml:xid"), O(name)); // xml use 'xid' and others use 'id'
|
||||
};
|
||||
|
||||
int main() {
|
||||
Test t;
|
||||
string json="{\"id\":123, \"name\":\"Pony\"}";
|
||||
|
||||
xpack::json::decode(json, t);
|
||||
cout<<t.uid<<endl;
|
||||
|
||||
string xstr = xpack::xml::encode(t, "root");
|
||||
cout<<xstr<<endl;
|
||||
Test t1;
|
||||
xpack::xml::decode(xstr, t1);
|
||||
cout<<t1.uid<<endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
23
util/xpack/example/bitfield.cpp
Executable file
23
util/xpack/example/bitfield.cpp
Executable file
|
|
@ -0,0 +1,23 @@
|
|||
#include <iostream>
|
||||
#include "xpack/json.h"
|
||||
#include "xpack/xml.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Test {
|
||||
short ver:8;
|
||||
short len:8;
|
||||
string name;
|
||||
XPACK(B(F(0), ver, len), O(name));
|
||||
};
|
||||
|
||||
int main() {
|
||||
Test t;
|
||||
string json="{\"ver\":4, \"len\":20, \"name\":\"IPv4\"}";
|
||||
|
||||
xpack::json::decode(json, t);
|
||||
cout<<t.ver<<endl;
|
||||
cout<<t.len<<endl;
|
||||
cout<<xpack::xml::encode(t, "root")<<endl;
|
||||
return 0;
|
||||
}
|
||||
40
util/xpack/example/char_array.cpp
Executable file
40
util/xpack/example/char_array.cpp
Executable file
|
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* Copyright (C) 2021 Duowan Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#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[]) {
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
Test t;
|
||||
string json="{\"name\":\"Pony\", \"email\":\"pony@xpack.com\"}";
|
||||
|
||||
xpack::json::decode(json, t);
|
||||
cout<<t.name<<endl;
|
||||
cout<<t.email<<endl;
|
||||
return 0;
|
||||
}
|
||||
66
util/xpack/example/custom.cpp
Executable file
66
util/xpack/example/custom.cpp
Executable file
|
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* Copyright (C) 2021 Duowan Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include "xpack/json.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Test {
|
||||
unsigned int uid;
|
||||
string name;
|
||||
XPACK(C(hexint, F(0), uid), O(name));
|
||||
};
|
||||
|
||||
namespace xpack {
|
||||
|
||||
template <class Decoder>
|
||||
bool hexint_decode(Decoder&de, Test&t, const char *name, unsigned int&data, const xpack::Extend *ext) {
|
||||
(void)t;
|
||||
string raw;
|
||||
bool ret = de.decode(name, raw, ext);
|
||||
if (ret) {
|
||||
sscanf(raw.c_str(), "%x", &data);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <class Encoder>
|
||||
bool hexint_encode(Encoder&en, const Test&t, const char *name, const unsigned &data, const xpack::Extend *ext) {
|
||||
(void)t;
|
||||
char buf[64];
|
||||
sprintf(buf, "%x", data);
|
||||
bool ret = en.encode(name, buf, ext);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
Test t;
|
||||
t.uid = 127;
|
||||
t.name = "PonyJack";
|
||||
|
||||
string str = xpack::json::encode(t);
|
||||
cout<<str<<endl;
|
||||
|
||||
Test n;
|
||||
xpack::json::decode(str, t);
|
||||
cout<<t.uid<<endl;
|
||||
return 0;
|
||||
}
|
||||
45
util/xpack/example/encoder.cpp
Executable file
45
util/xpack/example/encoder.cpp
Executable file
|
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* Copyright (C) 2021 Duowan Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include "xpack/json.h"
|
||||
#include "xpack/xml.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
vector<int> vi(3);
|
||||
vi[0] = 1;
|
||||
vi[1] = 2;
|
||||
vi[2] = 3;
|
||||
|
||||
xpack::JsonWriter jw;
|
||||
xpack::XEncoder<xpack::JsonWriter> je(jw);
|
||||
|
||||
xpack::XmlWriter xw;
|
||||
xpack::XEncoder<xpack::XmlWriter> xe(xw);
|
||||
|
||||
je.ob(NULL).add("vv", vi).add("i",10).add("s","hello").oe();
|
||||
xe.ob("root").add("vv", vi).add("i",10).add("s","hello").oe();
|
||||
|
||||
cout<<je.String()<<endl;
|
||||
cout<<xe.String()<<endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
58
util/xpack/example/enum.cpp
Executable file
58
util/xpack/example/enum.cpp
Executable file
|
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
* Copyright (C) 2021 Duowan Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include "xpack/json.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
enum Enum {
|
||||
X = 0,
|
||||
Y = 1,
|
||||
Z = 2,
|
||||
};
|
||||
|
||||
enum class Enum1 {
|
||||
X = 0,
|
||||
Y = 1,
|
||||
Z = 2,
|
||||
};
|
||||
|
||||
enum class Enum2:int64_t {
|
||||
X = 0,
|
||||
Y = 1,
|
||||
Z = 2,
|
||||
};
|
||||
|
||||
struct Test {
|
||||
string name;
|
||||
Enum e;
|
||||
Enum1 e1;
|
||||
Enum2 e2;
|
||||
XPACK(O(name), E(F(0), e), O(e1, e2));
|
||||
};
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
Test t;
|
||||
string json="{\"name\":\"IPv4\", \"e\":1, \"e1\":2, \"e2\":2}";
|
||||
|
||||
xpack::json::decode(json, t);
|
||||
cout<<xpack::json::encode(t)<<endl;
|
||||
return 0;
|
||||
}
|
||||
69
util/xpack/example/inherit.cpp
Executable file
69
util/xpack/example/inherit.cpp
Executable file
|
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
* Copyright (C) 2021 Duowan Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#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), A(uid, "id"), O(name));
|
||||
};
|
||||
|
||||
struct Pa {
|
||||
string mail;
|
||||
XPACK(O(mail));
|
||||
};
|
||||
|
||||
struct Pb: public Pa {
|
||||
long version;
|
||||
XPACK(I(Pa), O(version));
|
||||
};
|
||||
|
||||
struct Pc:public Pb {
|
||||
long uid;
|
||||
string name;
|
||||
XPACK(I(Pb), A(uid, "id"), O(name));
|
||||
};
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
Test t;
|
||||
string json="{\"mail\":\"pony@xpack.com\", \"version\":2019, \"id\":123, \"name\":\"Pony\"}";
|
||||
|
||||
xpack::json::decode(json, t);
|
||||
cout<<xpack::json::encode(t)<<endl;
|
||||
|
||||
Pc p;
|
||||
xpack::json::decode(json, p);
|
||||
cout<<xpack::json::encode(p)<<endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
73
util/xpack/example/json-data.cpp
Executable file
73
util/xpack/example/json-data.cpp
Executable file
|
|
@ -0,0 +1,73 @@
|
|||
/*
|
||||
* Copyright (C) 2021 Duowan Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include "xpack/json.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Example {
|
||||
string type;
|
||||
xpack::JsonData data;
|
||||
XPACK(O(type, data));
|
||||
};
|
||||
|
||||
struct Range {
|
||||
int min;
|
||||
int max;
|
||||
XPACK(O(min, max));
|
||||
};
|
||||
|
||||
struct User {
|
||||
int id;
|
||||
string name;
|
||||
XPACK(O(id, name));
|
||||
};
|
||||
|
||||
static void test(const std::string &data) {
|
||||
Example e;
|
||||
xpack::json::decode(data, e);
|
||||
if (e.type == "range") {
|
||||
Range r;
|
||||
e.data.Get(r);
|
||||
cout<<"min in r:"<<r.min<<endl;
|
||||
cout<<"max in r:"<<r.max<<endl;
|
||||
|
||||
cout<<"min:"<<e.data["min"].Get<int>()<<endl;
|
||||
cout<<"max:"<<e.data["max"].Get<int>()<<endl;
|
||||
} else if (e.type == "user") {
|
||||
cout<<"id:"<<e.data["id"].Get<int>()<<endl;
|
||||
cout<<"name:"<<e.data["name"].Get<std::string>()<<endl;
|
||||
} else {
|
||||
cout<<"unknow type"<<endl;
|
||||
}
|
||||
cout<<"re encode:"<<xpack::json::encode(e)<<endl;
|
||||
cout<<"data encode:"<<xpack::json::encode(e.data)<<endl;
|
||||
cout<<"data dir encode:"<<e.data.String()<<endl;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
string s1 = "{\"type\":\"range\", \"data\":{\"min\":12, \"max\":22}}";
|
||||
string s2 = "{\"type\":\"user\", \"data\":{\"id\":123, \"name\":\"xpack\"}}";
|
||||
test(s1);
|
||||
test(s2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
36
util/xpack/example/makefile
Executable file
36
util/xpack/example/makefile
Executable file
|
|
@ -0,0 +1,36 @@
|
|||
ifeq ($(GPP),)
|
||||
GPP=g++
|
||||
endif
|
||||
|
||||
SRC=$(wildcard *.cpp)
|
||||
TAR=$(basename $(SRC))
|
||||
|
||||
ifeq ($(c11),off)
|
||||
NEEDC11=sharedptr
|
||||
TAR:=$(filter-out $(NEEDC11),$(TAR))
|
||||
else
|
||||
CXXFLAG=-std=c++11
|
||||
endif
|
||||
|
||||
%:%.cpp
|
||||
$(GPP) -o $@ -g $< -Wall -Wextra -I ../.. ${CXXFLAG}
|
||||
@echo ============ run $@ ================
|
||||
@./$@
|
||||
@-rm $@
|
||||
@-rm -rf $@.dSYM
|
||||
|
||||
tar:$(TAR)
|
||||
@echo -------test done-------
|
||||
|
||||
mysql:mysql.cpp
|
||||
$(GPP) -o $@ -g $< -lmysqlclient -I ../..
|
||||
|
||||
sqlite:sqlite.cpp
|
||||
$(GPP) -o $@ -g $< -lsqlite -I ../..
|
||||
|
||||
yaml:yaml.cpp
|
||||
$(GPP) -o $@ -g $< -Wall -Wextra -I ../.. -I ../../yaml-cpp/include ../../yaml-cpp/build/libyaml-cpp.a -std=c++11
|
||||
@echo ============ run $@ ================
|
||||
@./$@
|
||||
@-rm $@
|
||||
@-rm -rf $@.dSYM
|
||||
19
util/xpack/example/msvc.make
Executable file
19
util/xpack/example/msvc.make
Executable file
|
|
@ -0,0 +1,19 @@
|
|||
MAKEFLAGS += --no-builtin-rules
|
||||
MAKEFLAGS += --no-builtin-variables
|
||||
SRC=$(wildcard *.cpp)
|
||||
TAR=$(SRC:.cpp=)
|
||||
|
||||
NEEDLIB=mysql sqlite yaml
|
||||
TAR:=$(filter-out $(NEEDLIB),$(TAR))
|
||||
|
||||
%:%.cpp
|
||||
cl /Fe:${CURDIR}/$@.exe /nologo /wd4819 $< /EHsc -I ../..
|
||||
@echo ============ run $@.exe ================
|
||||
@$@.exe
|
||||
@del $@.exe $@.obj
|
||||
|
||||
tar:$(TAR)
|
||||
@echo -------test done-------
|
||||
|
||||
clean:
|
||||
@-del *.obj *.exe
|
||||
70
util/xpack/example/my_decoder.cpp
Executable file
70
util/xpack/example/my_decoder.cpp
Executable file
|
|
@ -0,0 +1,70 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
#include "xpack/json.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
// assign value by name
|
||||
class AssignDecoder {
|
||||
public:
|
||||
AssignDecoder(const std::vector<std::string>&vs, const unsigned char *src, unsigned char *dst) {
|
||||
for (size_t i=0; i<vs.size(); ++i) {
|
||||
fields.insert(vs[i]);
|
||||
}
|
||||
this->src = src;
|
||||
this->dst = dst;
|
||||
}
|
||||
|
||||
// decoder need implemetion decode function
|
||||
template<class T>
|
||||
bool decode(const char *key, T&val, const xpack::Extend *ext) {
|
||||
(void)ext;
|
||||
// set value by offset
|
||||
if (fields.end() != fields.find(key)) {
|
||||
unsigned int offset = (unsigned int)((unsigned char*)&val - dst);
|
||||
val = *((T*)(src+offset));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
private:
|
||||
std::set<std::string> fields;
|
||||
const unsigned char *src;
|
||||
unsigned char *dst;
|
||||
};
|
||||
|
||||
// SetFields<T>(src, dst, "a,b") means dst.a = src.a; dst.b = src.b;
|
||||
template <class T>
|
||||
void SetFields(const T&src, T&dst, const std::string&fields) {
|
||||
std::vector<std::string> vs;
|
||||
xpack::Util::split(vs, fields, ',');
|
||||
if (vs.size() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
AssignDecoder de(vs, (unsigned char*)(&src), (unsigned char*)(&dst));
|
||||
dst.__x_pack_decode(de, dst, NULL);
|
||||
}
|
||||
|
||||
struct Data {
|
||||
int a;
|
||||
int b;
|
||||
std::string c;
|
||||
XPACK(O(a,b,c));
|
||||
};
|
||||
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
Data d1;
|
||||
Data d2;
|
||||
d1.a = 5;
|
||||
d1.b = 10;
|
||||
d1.c = "hello";
|
||||
d2.b = 9;
|
||||
SetFields<Data>(d1, d2, "a,c");
|
||||
cout<<d2.a<<','<<d2.b<<','<<d2.c<<endl;
|
||||
return 0;
|
||||
}
|
||||
89
util/xpack/example/mysql.cpp
Executable file
89
util/xpack/example/mysql.cpp
Executable file
|
|
@ -0,0 +1,89 @@
|
|||
#include <iostream>
|
||||
#include <mysql/mysql.h>
|
||||
#include <xpack/mysql.h>
|
||||
#include <xpack/json.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct VID {
|
||||
int64_t Vid;
|
||||
int64_t vlen;
|
||||
string sid;
|
||||
int64_t level;
|
||||
XPACK(A(Vid, "db:vid"), O(vlen, sid, level));
|
||||
};
|
||||
|
||||
typedef char cfgstr[64];
|
||||
struct config {
|
||||
cfgstr host;
|
||||
uint32_t port;
|
||||
cfgstr user;
|
||||
cfgstr password;
|
||||
cfgstr dbname;
|
||||
XPACK(M(host, port, user, password, dbname))
|
||||
};
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
MYSQL *mysql = NULL;
|
||||
|
||||
if (mysql_library_init(argc, argv, NULL)) {
|
||||
cout<<"could not initialize MySQL client library"<<endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
mysql = mysql_init(mysql);
|
||||
|
||||
if (!mysql) {
|
||||
cout<<"Init faild, out of memory?"<<endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
config cfg;
|
||||
xpack::json::decode_file("./config.json", cfg);
|
||||
|
||||
if (!mysql_real_connect(mysql, /* MYSQL structure to use */
|
||||
cfg.host, /* server hostname or IP address */
|
||||
cfg.user, /* mysql user */
|
||||
cfg.password, /* password */
|
||||
cfg.dbname, /* default database to use, NULL for none */
|
||||
cfg.port, /* port number, 0 for default */
|
||||
NULL, /* socket file or named pipe name */
|
||||
CLIENT_FOUND_ROWS /* connection flags */ )) {
|
||||
cout<<"Connect failed"<<endl;
|
||||
} else {
|
||||
if (mysql_query(mysql, argv[1])) {
|
||||
cout<<"Query failed: "<<mysql_error(mysql)<<endl;
|
||||
} else {
|
||||
MYSQL_RES *result = mysql_store_result(mysql);
|
||||
|
||||
if (!result) {
|
||||
cout<<"Couldn't get results set: "<<mysql_error(mysql)<<endl;
|
||||
} else {
|
||||
VID vid;
|
||||
xpack::mysql::decode(result, vid); // decode first row
|
||||
|
||||
vector<VID> vids;
|
||||
xpack::mysql::decode(result, vids);// decode all rows
|
||||
|
||||
vector<int64_t> ids;
|
||||
xpack::mysql::decode(result, "vid", ids); // decode vid column of all rows
|
||||
|
||||
int64_t id;
|
||||
xpack::mysql::decode(result, "vid", id); // decode vid column of first row
|
||||
|
||||
cout<<xpack::json::encode(vid)<<endl;
|
||||
cout<<xpack::json::encode(vids)<<endl;
|
||||
cout<<xpack::json::encode(ids)<<endl;
|
||||
cout<<id<<endl;
|
||||
|
||||
mysql_free_result(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mysql_close(mysql);
|
||||
|
||||
mysql_library_end();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
61
util/xpack/example/sharedptr.cpp
Executable file
61
util/xpack/example/sharedptr.cpp
Executable file
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* Copyright (C) 2021 Duowan Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include "xpack/json.h"
|
||||
#include "xpack/xml.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct User {
|
||||
int64_t id;
|
||||
string name;
|
||||
string mail;
|
||||
std::shared_ptr<string> bio;
|
||||
User(int64_t i=0, const string& n="", const string& m=""):id(i),name(n),mail(m){}
|
||||
XPACK(O(id, name, mail, bio));
|
||||
};
|
||||
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
User u1(1, "Jack", "jack@xpack.com");
|
||||
User u2;
|
||||
User u3;
|
||||
|
||||
string json = xpack::json::encode(u1);
|
||||
string xml = xpack::xml::encode(u1, "root");
|
||||
cout<<"========================"<<endl;
|
||||
cout<<json<<endl;
|
||||
cout<<xml<<endl;
|
||||
|
||||
cout<<"========================"<<endl;
|
||||
xpack::json::decode(json, u2);
|
||||
cout<<xpack::json::encode(u2)<<endl;
|
||||
xpack::xml::decode(xml, u3);
|
||||
cout<<xpack::xml::encode(u3, "root")<<endl;
|
||||
|
||||
cout<<"========================"<<endl;
|
||||
u1.bio.reset(new std::string("farmer"));
|
||||
json = xpack::json::encode(u1);
|
||||
xml = xpack::xml::encode(u1, "root");
|
||||
cout<<json<<endl;
|
||||
cout<<xml<<endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
61
util/xpack/example/sqlite.cpp
Executable file
61
util/xpack/example/sqlite.cpp
Executable file
|
|
@ -0,0 +1,61 @@
|
|||
#include <iostream>
|
||||
#include <xpack/sqlite.h>
|
||||
#include <xpack/json.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct VID {
|
||||
int64_t Vid;
|
||||
int64_t vlen;
|
||||
string sid;
|
||||
int64_t level;
|
||||
XPACK(A(Vid, "db:vid"), O(vlen, sid, level));
|
||||
};
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
(void)argc;
|
||||
char *errMsg = NULL;
|
||||
sqlite *pDB = sqlite_open("./test.db", 0, &errMsg);
|
||||
if (NULL == pDB) {
|
||||
cout<<"open sqlite db fail:"<<errMsg<<endl;
|
||||
sqlite_freemem(errMsg);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int nCols = 0;
|
||||
int nRows = 0;
|
||||
char **azResult = NULL;
|
||||
|
||||
int ret = sqlite_get_table(pDB, argv[1], &azResult, &nRows, &nCols, &errMsg);
|
||||
if (ret != SQLITE_OK) {
|
||||
cout<<"query sqlite db fail:"<<ret<<endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
cout<<nRows<<','<<nCols<<endl;
|
||||
for (int i=0; i<nCols; ++i) {
|
||||
cout<<azResult[i]<<' ';
|
||||
}
|
||||
cout<<endl;
|
||||
int idx = nCols;
|
||||
for (int i=0; i<nRows; ++i) {
|
||||
for (int j=0; j<nCols; ++j, ++idx) {
|
||||
if (NULL == azResult[idx]) {
|
||||
cout<<"NULL"<<' ';
|
||||
} else {
|
||||
cout<<azResult[idx]<<' ';
|
||||
}
|
||||
}
|
||||
cout<<endl;
|
||||
}
|
||||
|
||||
if (NULL != azResult) {
|
||||
sqlite_free_table(azResult);
|
||||
}
|
||||
if (NULL != errMsg) {
|
||||
sqlite_freemem(errMsg);
|
||||
}
|
||||
sqlite_close(pDB);
|
||||
|
||||
return 0;
|
||||
}
|
||||
118
util/xpack/example/union1.cpp
Executable file
118
util/xpack/example/union1.cpp
Executable file
|
|
@ -0,0 +1,118 @@
|
|||
/*
|
||||
* Copyright (C) 2021 Duowan Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include "xpack/json.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Base {
|
||||
int a;
|
||||
int b;
|
||||
XPACK(O(a, b));
|
||||
};
|
||||
|
||||
struct Data {
|
||||
int type;
|
||||
union {
|
||||
Base b;
|
||||
int i;
|
||||
char s[10];
|
||||
};
|
||||
string email;
|
||||
string birthday;
|
||||
// 'type' determines which member of the union to use, so we put type in 'C'
|
||||
// union does not need to be put into XPACK, we will handled it in 'type'
|
||||
XPACK(C(data_uinon, F(0), type), O(email, birthday));
|
||||
};
|
||||
|
||||
struct User {
|
||||
string name;
|
||||
Data data;
|
||||
XPACK(O(name, data));
|
||||
};
|
||||
|
||||
namespace xpack { // must define in namespace xpack
|
||||
|
||||
template<class OBJ>
|
||||
bool data_uinon_decode(OBJ &obj, Data &d, const char*key, int &type, const Extend *ext) {
|
||||
(void)ext;
|
||||
obj.decode(key, type, ext);
|
||||
switch (type) {
|
||||
case 1:
|
||||
obj.decode("b", d.b, NULL); // key("b") should same as encode
|
||||
break;
|
||||
case 2:
|
||||
obj.decode("i", d.i, NULL);
|
||||
break;
|
||||
case 3:
|
||||
obj.decode("s", d.s, NULL);
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template<class OBJ>
|
||||
bool data_uinon_encode(OBJ &obj, const Data &d, const char*key, const int &type, const Extend *ext) {
|
||||
(void)ext;
|
||||
obj.encode(key, type, NULL);
|
||||
switch (type) {
|
||||
case 1:
|
||||
obj.encode("b", d.b, NULL);
|
||||
break;
|
||||
case 2:
|
||||
obj.encode("i", d.i, NULL);
|
||||
break;
|
||||
case 3:
|
||||
obj.encode("s", d.s, NULL);
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
User u;
|
||||
u.name = "Pony";
|
||||
u.data.type = 1;
|
||||
u.data.b.a = 10;
|
||||
u.data.b.b = 12;
|
||||
u.data.email = "Pony@xpack.com";
|
||||
u.data.birthday = "20200202";
|
||||
|
||||
string s = xpack::json::encode(u);
|
||||
cout<<s<<endl;
|
||||
|
||||
u.data.type = 2;
|
||||
u.data.i = 22;
|
||||
s = xpack::json::encode(u);
|
||||
cout<<s<<endl;
|
||||
|
||||
u.data.type = 3;
|
||||
strcpy(u.data.s, "hello");
|
||||
s = xpack::json::encode(u);
|
||||
cout<<s<<endl;
|
||||
|
||||
User u1;
|
||||
xpack::json::decode(s, u1);
|
||||
cout<<u1.data.s<<endl;
|
||||
return 0;
|
||||
}
|
||||
37
util/xpack/example/xml-content.cpp
Executable file
37
util/xpack/example/xml-content.cpp
Executable file
|
|
@ -0,0 +1,37 @@
|
|||
#include <iostream>
|
||||
#include <xpack/xml.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Base {
|
||||
int a;
|
||||
int b;
|
||||
string c;
|
||||
XPACK(X(F(ATTR), a, b), X(F(XML_CONTENT), c));
|
||||
};
|
||||
|
||||
struct Root {
|
||||
Base root;
|
||||
XPACK(O(root));
|
||||
};
|
||||
|
||||
|
||||
int main() {
|
||||
Root r;
|
||||
r.root.a = 1;
|
||||
r.root.b = 2;
|
||||
r.root.c = "hello";
|
||||
string s = xpack::xml::encode(r, "root", 0, 4, ' ');
|
||||
cout<<s<<endl;
|
||||
string s1 = xpack::xml::encode(r, "root");
|
||||
cout<<s1<<endl;
|
||||
|
||||
Root r1,r2;
|
||||
xpack::xml::decode(s, r1);
|
||||
xpack::xml::decode(s1, r2);
|
||||
|
||||
cout<<xpack::xml::encode(r1, "r1")<<endl;
|
||||
cout<<xpack::xml::encode(r2, "r2")<<endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
67
util/xpack/example/xml.cpp
Executable file
67
util/xpack/example/xml.cpp
Executable file
|
|
@ -0,0 +1,67 @@
|
|||
#include <iostream>
|
||||
#include <xpack/xml.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Base {
|
||||
int a;
|
||||
int b;
|
||||
XPACK(O(a, b));//, X(F(ATTR), b));
|
||||
};
|
||||
|
||||
struct Root {
|
||||
Base root;
|
||||
XPACK(O(root));
|
||||
};
|
||||
|
||||
struct Top {
|
||||
int k;
|
||||
string n;
|
||||
vector<vector<Base> > bs;
|
||||
vector<int> vi;
|
||||
float f;
|
||||
std::string name; // cdata
|
||||
XPACK(O(k, n, f), A(bs, "xml:base,sbs", vi, "xml:vi,vl@x", name, "xml:name,cdata"));
|
||||
};
|
||||
|
||||
int main() {
|
||||
Top t;
|
||||
t.k = 666;
|
||||
t.n = "你好";
|
||||
t.f = 1.23333;
|
||||
t.vi.resize(2);
|
||||
t.vi[0] = 1;
|
||||
t.vi[1] = 2;
|
||||
t.bs.resize(2);
|
||||
t.bs[0].resize(2);
|
||||
t.bs[1].resize(2);
|
||||
t.bs[0][0].a = 100;
|
||||
t.bs[0][0].b = 200;
|
||||
t.bs[0][1].a = 101;
|
||||
t.bs[0][1].b = 201;
|
||||
t.bs[1][0].a = 110;
|
||||
t.bs[1][0].b = 210;
|
||||
t.bs[1][1].a = 111;
|
||||
t.bs[1][1].b = 211;
|
||||
t.name = "好好学习,天天向上<good good study, day day up>";
|
||||
string s = xpack::xml::encode(t, "root", 0, 4, ' ');
|
||||
cout<<s<<endl;
|
||||
|
||||
xpack::XmlEncoder en;
|
||||
en.SetMaxDecimalPlaces(3);
|
||||
cout<<"=========="<<endl<<en.encode(t, "root")<<endl;
|
||||
|
||||
Top t1;
|
||||
xpack::xml::decode(s, t1);
|
||||
cout<<xpack::xml::encode(t1, "root", 0, 4, ' ')<<endl;
|
||||
|
||||
Base b1;
|
||||
bool ret = xpack::XmlDecoder().decode("<root><a>1</a><b>2</b></root>", b1);
|
||||
cout<<ret<<','<<b1.a<<','<<b1.b<<endl;
|
||||
|
||||
Root r1;
|
||||
ret = xpack::XmlDecoder().decode("<root><a>1</a><b>2</b></root>", r1, true);
|
||||
cout<<ret<<','<<r1.root.a<<','<<r1.root.b<<endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
59
util/xpack/example/xpack_out.cpp
Executable file
59
util/xpack/example/xpack_out.cpp
Executable file
|
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
* Copyright (C) 2021 Duowan Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
// #include <sys/time.h> linux only
|
||||
#include <iostream>
|
||||
#include "xpack/json.h"
|
||||
#ifndef _MSC_VER
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
|
||||
#ifdef _MSC_VER
|
||||
struct timeval {
|
||||
long tv_sec;
|
||||
long tv_usec;
|
||||
};
|
||||
#endif
|
||||
|
||||
// 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[]) {
|
||||
(void)argc;
|
||||
(void)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;
|
||||
}
|
||||
121
util/xpack/example/xtype.cpp
Executable file
121
util/xpack/example/xtype.cpp
Executable file
|
|
@ -0,0 +1,121 @@
|
|||
/*
|
||||
* Copyright (C) 2021 Duowan Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include "xpack/json.h"
|
||||
#include "xpack/xml.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Date {
|
||||
long unix_time;
|
||||
};
|
||||
|
||||
namespace xpack { // must define in namespace xpack
|
||||
|
||||
template<>
|
||||
struct is_xpack_xtype<Date> {static bool const value = true;};
|
||||
|
||||
// implement decode
|
||||
template<class OBJ>
|
||||
bool xpack_xtype_decode(OBJ &obj, Date &val, const Extend *ext) {
|
||||
std::string str;
|
||||
obj.decode(str, ext);
|
||||
if (str.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifndef _MSC_VER
|
||||
tm ttm;
|
||||
|
||||
if (0 != strptime(str.c_str(), "%Y-%m-%d %H:%M:%S", &ttm)) {
|
||||
val.unix_time = mktime(&ttm);
|
||||
} else {
|
||||
val.unix_time = 0;
|
||||
}
|
||||
#else
|
||||
static int days[]={31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 32};
|
||||
struct tm ttm={0};
|
||||
sscanf_s(str.c_str(), "%d-%d-%d %d:%d:%d", &ttm.tm_year, &ttm.tm_mon, &ttm.tm_mday, &ttm.tm_hour, &ttm.tm_min, &ttm.tm_sec);
|
||||
ttm.tm_mon-=1; // mon[0-11]
|
||||
ttm.tm_year-=1900; // since 1900
|
||||
val.unix_time = mktime(&ttm);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
// implement encode
|
||||
template<class OBJ>
|
||||
bool xpack_xtype_encode(OBJ &obj, const char*key, const Date &val, const Extend *ext) {
|
||||
time_t tt = (time_t)val.unix_time;
|
||||
tm ttm;
|
||||
|
||||
#ifndef _MSC_VER
|
||||
localtime_r(&tt, &ttm);
|
||||
#else
|
||||
localtime_s(&ttm, &tt);
|
||||
#endif
|
||||
|
||||
char buf[64];
|
||||
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &ttm);
|
||||
return obj.encode(key, buf, ext);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
struct User {
|
||||
int64_t id;
|
||||
string name;
|
||||
string mail;
|
||||
Date d;
|
||||
User(int64_t i=0, const string& n="", const string& m="", long _d=0):id(i),name(n),mail(m){d.unix_time = _d;}
|
||||
XPACK(O(id, name, mail, d));
|
||||
};
|
||||
|
||||
struct Group {
|
||||
string name;
|
||||
int64_t master;
|
||||
vector<User> members;
|
||||
XPACK(O(name, master, members));
|
||||
};
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
Group g;
|
||||
g.name = "C++";
|
||||
g.master = 2019;
|
||||
g.members.resize(2);
|
||||
g.members[0] = User(1, "Jack", "jack@xpack.com", 1);
|
||||
g.members[1] = User(2, "Pony", "pony@xpack.com", 1609249232);
|
||||
|
||||
string json = xpack::json::encode(g, 0, 2, ' ');
|
||||
cout<<json<<endl;
|
||||
|
||||
string xml = xpack::xml::encode(g, "root", 0, 2, ' ');
|
||||
cout<<xml<<endl;
|
||||
|
||||
Group n1;
|
||||
xpack::json::decode(json, n1);
|
||||
cout<<n1.name<<';'<<n1.members[0].name<<';'<<n1.members[0].d.unix_time<<endl;
|
||||
|
||||
Group n2;
|
||||
xpack::xml::decode(xml, n2);
|
||||
cout<<n2.name<<';'<<n2.members[1].name<<';'<<n2.members[1].d.unix_time<<endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
108
util/xpack/example/xtype_advance.cpp
Executable file
108
util/xpack/example/xtype_advance.cpp
Executable file
|
|
@ -0,0 +1,108 @@
|
|||
/*
|
||||
* Copyright (C) 2021 Duowan Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include "xpack/json.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
// The requirement is to encode seq1 when type==1, and encode seq2 when type==2
|
||||
struct Sub {
|
||||
int type;
|
||||
string seq1;
|
||||
string seq2;
|
||||
XPACK(O(type, seq1, seq2));
|
||||
};
|
||||
|
||||
|
||||
struct Test {
|
||||
unsigned int uid;
|
||||
string name;
|
||||
Sub sub;
|
||||
XPACK(O(uid, name, sub));
|
||||
};
|
||||
|
||||
namespace xpack {
|
||||
|
||||
template<>
|
||||
struct is_xpack_xtype<Sub> {static bool const value = true;};
|
||||
|
||||
template <class Decoder>
|
||||
bool xpack_xtype_decode(Decoder& de, Sub &data, const xpack::Extend *ext) {
|
||||
de.decode_struct(data, ext);
|
||||
return true;
|
||||
}
|
||||
|
||||
template <class BaseEncoder>
|
||||
class MyEncoder {
|
||||
public:
|
||||
MyEncoder(BaseEncoder&b, const Sub&sub):_b(&b), _s(&sub){}
|
||||
template <class T>
|
||||
bool encode(const char*key, const T&val, const xpack::Extend *ext) {
|
||||
if (strcmp(key, "seq1") == 0) {
|
||||
if (_s->type == 1) {
|
||||
return _b->encode(key, val, ext);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else if (strcmp(key, "seq2") == 0) {
|
||||
if (_s->type == 2) {
|
||||
return _b->encode(key, val, ext);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return _b->encode(key, val, ext);
|
||||
}
|
||||
}
|
||||
private:
|
||||
BaseEncoder *_b;
|
||||
const Sub *_s;
|
||||
};
|
||||
|
||||
template <class Encoder>
|
||||
bool xpack_xtype_encode(Encoder &en, const char *name, const Sub &data, const xpack::Extend *ext) {
|
||||
(void)name;
|
||||
MyEncoder<Encoder> my(en, data);
|
||||
data.__x_pack_encode(my, data, ext);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
Test t;
|
||||
t.uid = 127;
|
||||
t.name = "PonyJack";
|
||||
t.sub.type = 1;
|
||||
t.sub.seq1 = "10086";
|
||||
t.sub.seq2 = "10001";
|
||||
|
||||
string str = xpack::json::encode(t);
|
||||
cout<<str<<endl;
|
||||
|
||||
Test n;
|
||||
xpack::json::decode(str, t);
|
||||
cout<<t.uid<<endl;
|
||||
|
||||
t.sub.type = 2;
|
||||
cout<<xpack::json::encode(t)<<endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
65
util/xpack/example/yaml.cpp
Executable file
65
util/xpack/example/yaml.cpp
Executable file
|
|
@ -0,0 +1,65 @@
|
|||
/*
|
||||
* Copyright (C) 2021 Duowan Inc. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include "xpack/yaml.h"
|
||||
#include "xpack/json.h"
|
||||
|
||||
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){}
|
||||
XPACK(O(id, name, mail));
|
||||
};
|
||||
|
||||
struct Group {
|
||||
string name;
|
||||
int64_t master;
|
||||
map<string, string> flags;
|
||||
vector<User> members;
|
||||
User arrays[2];
|
||||
XPACK(O(name, master, flags, members, arrays));
|
||||
};
|
||||
|
||||
int main() {
|
||||
User u;
|
||||
xpack::yaml::decode_file("./test.yml", u);
|
||||
string json = xpack::json::encode(u);
|
||||
cout<<"json:"<<json<<endl;
|
||||
cout<<"yaml is =================="<<endl<<xpack::yaml::encode(u)<<endl<<"=================="<<endl;
|
||||
|
||||
Group g;
|
||||
g.name = "C++";
|
||||
g.master = 2019;
|
||||
g.members.resize(2);
|
||||
g.members[0] = User(1, "Jack", "jack@xpack.com");
|
||||
g.members[1] = User(2, "Pony", "pony@xpack.com");
|
||||
g.arrays[0] = g.members[0];
|
||||
g.arrays[1] = g.members[1];
|
||||
g.flags["a"] = "good";
|
||||
g.flags["b"] = "nice";
|
||||
string ys = xpack::yaml::encode(g);
|
||||
cout<<"yaml is =================="<<endl<<ys<<endl<<"=================="<<endl;
|
||||
|
||||
Group g1;
|
||||
xpack::yaml::decode(ys, g1);
|
||||
cout<<xpack::json::encode(g1)<<endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue