Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/xyz347/x2struct
Convert between json string and c++ object. json字符串和c++结构体之间互相转换
https://github.com/xyz347/x2struct
bson convert cpp json object struct xml
Last synced: 7 days ago
JSON representation
Convert between json string and c++ object. json字符串和c++结构体之间互相转换
- Host: GitHub
- URL: https://github.com/xyz347/x2struct
- Owner: xyz347
- Created: 2017-09-05T07:41:53.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2020-12-21T05:30:09.000Z (almost 4 years ago)
- Last Synced: 2024-10-22T22:53:42.573Z (15 days ago)
- Topics: bson, convert, cpp, json, object, struct, xml
- Language: C++
- Homepage:
- Size: 2.66 MB
- Stars: 264
- Watchers: 16
- Forks: 107
- Open Issues: 1
-
Metadata Files:
- Readme: README.en.md
Awesome Lists containing this project
README
x2struct
====
*Read this in other languages: [简体中文](README.zh-cn.md).** Used to convert between C++ objects and json/xml/json/libconfig
* json/xml is header file only, no need to compile librarys
* json is enabled by default, others need to modify [config.h](config.h)
* The following example uses json as an example. Others can refer to api of x2struct::X in [x2struct.hpp](x2struct.hpp)
------
* [Basic usage](#basic-usage)
* [Mandatory](#mandatory)
* [Alias](#alias)
* [Inherit](#inherit)
* [Bit field](#bit-field)
* [Load conditional](#load-conditional)
* [char array](#char-array)
* [check exist](#check-exist)
* [local class](#local-class)
* [customize type](#customize-type)
* [thirdparty class](#thirdparty-class)
* [Format indent](#format-indent)
* [Qt support](#qt-support)
* [xml bson libconfig](#xml-bson-libconfig)
* [Generate Golang struct](#generate-golang-struct)
* [IMPORTANT](#important)Basic usage
----
```C++
#include
#include "x2struct/x2struct.hpp" // include this header fileusing 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)); // add XTOSTRUCT at the end of struct/class
};struct Group {
string name;
int64_t master;
vector members;
XTOSTRUCT(O(name, master, members)); // add XTOSTRUCT at the end of struct/class
};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); // C++ object to json
cout< vi;
x2struct::X::loadjson("[1,2,3]", vi, false); // load vector directory
cout< m;
x2struct::X::loadjson("{\"1\":10, \"2\":20}", m, false); // load map directory
cout<
#include "x2struct/x2struct.hpp"using namespace std;
struct Test {
int64_t id;
string name;
XTOSTRUCT(M(id), O(name)); // "id" is mandatory and "name" is optional
};int main(int argc, char *argv[]) {
Test t;
string json="{\"name\":\"Pony\"}";x2struct::X::loadjson(json, t, false); // will throw exception(id is mandatory and no "id" in json)
return 0;
}
```Alias
----
- used for scenes where the variable name and json key name are inconsistent
- use A to include alias variable. "A" take two parameters, 1st is variable, 2nd is tag-field
- tag format is "[type:]alias[,extension]", Tag-field can contain multiple tags, separated by spaces
- type is one of "json"/"xml"/"bson"/"config", no type field in tag means this tag is applied to all type
- for example, "tid" means use tid as alias for all type
- "tid json:xid" means json use "xid" as alias and other types use "tid"
- extension only support "m" now, means mandatory(A like O, optional)
- if no alias in json, loadjson will also try variable name
- tojson use alias``` C++
#include
#include "x2struct/x2struct.hpp"using namespace std;
struct Test {
int64_t uid;
string name;
XTOSTRUCT(A(uid, "id"), O(name)); // "uid" use alias "id"
};int main(int argc, char *argv[]) {
Test t;
string json="{\"id\":123, \"name\":\"Pony\"}";x2struct::X::loadjson(json, t, false);
cout<
#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 "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 "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)); // task included in C and O, C must at beginning of XTOSTRUCT
/*
XTOSTRUCT_CONDITION take two parameters, 1st is class name, 2nd is variable name
XTOSTRUCT_CONDITION defined a function take one parameter named "obj", pointer to xdoc of variable
"task" in json is array, load conditional only load the one that this functin return 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 "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); // 4th parameter apply true
cout<
#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< to define customize type
- following is an IPv4 example```C++
#include
#include "x2struct/x2struct.hpp"using namespace std;
// just example, no error handle
struct _XIPv4 {
uint32_t ip;
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);
}
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 x2struct::XType<_XIPv4> XIPv4;
struct Test {
XIPv4 ip;
XIPv4 mask;
XTOSTRUCT(O(ip, mask));
};int main(int argc, char *argv[]) {
Test t;
string json="{\"ip\":\"192.168.1.2\", \"mask\":\"255.255.255.0\"}";x2struct::X::loadjson(json, t, false);
cout<
#include
#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<, key need add 'x', e.g. x100
- mongoxclient(https://github.com/xyz347/mongoxclient) is a wrapper of mongo-cxx-driver use bsonGenerate Golang struct
----
- use to generate Golang struct define by C++ struct
- need to define macro XTOSTRUCT_GOCODE
```C++
// 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<