xyz347 / x2struct Goto Github PK
View Code? Open in Web Editor NEWConvert between json string and c++ object. json字符串和c++结构体之间互相转换
Convert between json string and c++ object. json字符串和c++结构体之间互相转换
QMap <QString, QList<QString>> x;
x.insert("a", QList<QString>() << "content1" << "content2");
这个 QMap 无法转换成json
C++ 版本依赖是怎样的?是否必须 C++11 及以上的版本支持?
error:
x2struct-master/xtypes.h:90:65: error: 'strptime' was not declared in this scope
if (0 != strptime(str.c_str(), "%Y-%m-%d %H:%M:%S", &ttm)) {
^
fix used :
#if defined(__WIN32__)
#ifndef WINDOWS
#define WINDOWS
#endif
in order to fix it :
warn on README that you have to define WINDOWS on windows before importing the library
or
use __WIN32__ instead of WINDOWS
time_t
is defined as these below in Linux:
time_t
(in time_t.h) --> typedef __time_t time_t
(in types.h) --> __STD_TYPE __TIME_T_TYPE __time_t;
(in typesizes.h) --> #define __TIME_T_TYPE __SYSCALL_SLONG_TYPE
and finally
#if defined __x86_64__ && defined __ILP32__
# define __SYSCALL_SLONG_TYPE __SQUAD_TYPE
# define __SYSCALL_ULONG_TYPE __UQUAD_TYPE
#else
# define __SYSCALL_SLONG_TYPE __SLONGWORD_TYPE
# define __SYSCALL_ULONG_TYPE __ULONGWORD_TYPE
#endif
for different platforms.
However, on macOS (at lease in macOS Catalina and 10.14), time_t
is defined as long (from the compiler error message)...
Tl; Dr:
Seems that x2struct has a missing convert of type long
, https://github.com/lhy0403/Qv2ray/commit/bcb328e772b50e7896e142450bd89dc9fcf939a0/checks?check_suite_id=379416319#step:6:321
你好:
我在声明的结构体中调用了XTOSTRUCT,但是报错了。提示宏定义TOX_ 和 X_STRUCT_L1是未声明的标识符。代码跟进去文件x2struct.hpp,果然没有找到这两个宏定义。宏定义TOS_也没有找到。
这是怎么回事啊?
struct Item
{
string label;
string type;
string value;
vector<string> choices;
XTOSTRUCT(M(label,type, value), O( choices));
};
map<string, Item> members;
Item test1;
test1.label = "Canon";
test1.type = "choice";
test1.value="Manual";
test1.choices.resize(2);
test1.choices[0]="TV";
test1.choices[1]="AV";
members["autoexposuremode"] = test1;
Item test2;
test2.label = "Bracket";
test2.type = "string";
test2.value="0";
members["bracketmode"] = test2;
string json = x2struct::X::tojson(members); // 结构体转json
cout << json << endl;
{
"autoexposuremode": {
"label": "Canon",
"type": "choice",
"value": "Manual",
"choices": ["TV", "AV"]
},
"bracketmode": {
"label": "Bracket",
"type": "string",
"value": "0",
"choices": []
}
}
{
"autoexposuremode": {
"label": "Canon",
"type": "choice",
"value": "Manual",
"choices": ["TV", "AV"]
},
"bracketmode": {
"label": "Bracket",
"type": "string",
"value": "0"
}
}
{
"streamSettings": {
"headers": null,
"network": "ws",
"security": "tls",
"tlsSettings": {
"allowInsecure": true,
"serverName": null
},
"wsSettings": {
"path": "/admin"
}
}
}
会出现 SIGABORT 错误
example.h(91): error C2664: “bool x2struct::JsonReader::convert(const char *,float &)”: 无法将参数 2 从“char [16]”转换为“std::string &”
eg
定义:
struct input
{
std::string name;
std::string id;
XTOSTRUCT(M(name,id));
};
json:
{
"name": "Tom",
"id":1
}
id 类型为int而不是string
没有错误,没有异常。
这个问题比较严重
It seems std::wstring
is not supported by the x2struct library. Is there any plan to implement that?
I am using boost::posix_time::ptime for storing datetime in my struct, but I am not able serialize the struct to json.
Can't support array, but vector is supported. Are there any ways to support array directly.
typedef struct _User
{
std::string name;
}User;
typedef struct _UserList
{
std::vectorstd::string userVec;
}UserList;
struct ConnectionProp{
int connectionId;
std::string connectionName;
double timeout;
XTOSTRUCT(M(connectionId,timeout),O(connectionName));
};
ConnectionProp x;
x2struct::X::loadjson(decodedstr, x, false);
In ConnectionProp object, connectionName property is getting a value of "".
Json is :"{"connectionId":3,"connectionName":"JAY","timeout":3.55566}
project.cpp:15:5: error: use of undeclared identifier 'X_STRUCT_L1_TOX'
x2struct.hpp:780:54: note: expanded from macro 'XTOSTRUCT'
I have modified your code to make it meet and I would like to be contributor
{"testVec":["key":null]}
打印:
Parse json string [{"testVec":["key":null]}] fail. :null]}
非常感谢作者提供这么好的库,因为前端这块要求的json是多了个反斜杠的{"aid":"1","cid":"1","cid_url":"11","time_stamp":"1","status":"1","time_stamp":"1","pic_name":"1","pic_data":"","data":[{"frame_id":"1","time_stamp":"1","alertFlag":"1","headInfo":[{"x":123,"y":123,"width":123,"height":123,"numOfHelmet":"1","color":"1"}]}]},但是这个库转json后是不带反斜杠的,请问如果有重新实现tojson这个函数要在哪里改
Hello, I have no problem in using the C + + standard type. Now I have customized the QT qstring to work, but when I want to use QT's qlist, I can't compile it, and the error "error c2039:" struct_to_str ": not a member of" qlist < T > ", I rewrite the conversion function. This is the" template < typename T >
JsonWriter& convert(const char*key, QList&data) {
x2struct_set_key(key);
this->array_begin();
for (size_t i=0; i<data.size(); ++i) {
this->convert("", data[i]);
}
this->array_end();
return *this;
}
During my program I run a query and I get this result in a string
{ "ok": true, "result": { "message_id": 109, "from": { "id": 3456789, "is_bot": true, "first_name": "ghhdhdsh", "username": "hbdhbhbdh" }, "chat": { "id": 5678, "first_name": "ghj", "last_name": "ghj", "username": "ghj", "type": "private" }, "date": 1606112895, "text": "ddd" } }
So I created some structures
`
struct from {
int id;
bool is_bot;
string first_name;
string username;
XTOSTRUCT( O(id, is_bot,first_name,username));
};
struct chat{
int id;
string first_name;
string last_name;
string username;
string type;
XTOSTRUCT( O(id, first_name,last_name,username,type));
};
struct result: public from,chat{
int message_id;
int date;
string text;
XTOSTRUCT(I(from,chat), O(message_id, date, text));
};
struct retour: public result {
bool ok;
XTOSTRUCT(I(result), O(ok));
};
`
And I want to do something like
if (retour.ok){ cout<<retour.result.chat.username<<" has received the message"; }
How can I do that, I've got some errors.
Thank you
I want to serialize array of struct to json I.E. BestFive_copy Best5Sell[20]; Where BestFive_copy is my struct.I cannot use vector for some reason. Xtostruct is not able to serialize array of struct to json.
What is the open source LICENSE for this project?
I want to use it in a project having license GPLv3, I'm not sure what your license is.
您好,请问是否不支持 xml的属性字段
I am trying with bool datatype and i have also passed variable in optional field but xhas method returns 0 and when i evaluate the struct the bool variable contains numeric value..is there any issue with bool datatype?
如果结构体里有char,就不支持了,该怎么改
Where is this function?
我的同事正在使用你的库解析json,他一直坚持你的库无法解析嵌套json,并要求我将嵌套的Object转为String。但我希望数据可以按照json的标准形式进行传输,我不是C++程序员,阅读你的源码有些困难,
所以我希望得到你的帮助。
我坚持的结构:
{
"errorCode": 0,
"message": "",
"data": {
"timestamp": 1531710826,
"collections": [
{
"temperature": 26.7,
"mac": "ABCDEF",
"humidity": 75.1
},
{
"temperature": 25.9,
"mac": "ABCDE2",
"humidity": 74.6
],
"address": "DEFAB"
}
}
他所坚持的结构:
{
"errorCode": 0,
"message": "",
"data": "{\"timestamp\": 1531710826,\"collections\": [{\"temperature\": 26.7,\"mac\": \"ABCDEF\","humidity": 75.1},{\"temperature\": 25.9,\"sensorMac\": \"ABCDE2\",\"humidity\": 74.6],\"address\": \"DEFAB\"}"
}
代码:
struct User
{
int64_t id;
string name;
string mail;
XTOSTRUCT(O(id, name, mail));
};
User user;
user.id = 1;
user.name = "Jack";
string json1 = x2struct::X::tojson(user);
std::cout << json1 << std::endl;
当前输出结果:
{"id":1,"name":"Jack","mail":""}
期望输出结果:
{"id":1,"name":"Jack"}
当结构提成员超过120个时候,编译失败
I am marshalling my struct i.e. converting struct to byte, and sending it to .Net, But when i include XTOSTRUCT(O(Field1,Field2) in my struct size get changed. Due to which, i get wrong structure on .NET side. Do you have any other way apart from declaring XTOSTRUCT field in struct.
我用Qt写了一个从xml直接转为你的c++结构体代码的程序
但是我无法联系到你 也就无法提交给你
麻烦给个联系方法
[email protected] 这个是我的
data
{
"a": "1",
"extend": {"b": "1", "c": "1", "d": "1", "e": "1", "f": "1", }
}
像这样的 extend 字段,是扩展字段,不知道具体的 字段 有哪些,只是将这个字段做一个转发。
这种情况能不能支持。
我这边用其他 json 库,是直接把 extend 解析成 string 类型
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.