xyz347 / xpack Goto Github PK
View Code? Open in Web Editor NEWconvert json/xml/bson to c++ struct
License: Apache License 2.0
convert json/xml/bson to c++ struct
License: Apache License 2.0
elidedText 字段原文: ヽ市キ華虐蒙i川 鯨 | 根据 《幼ブL\n‐\ぷ弄始吋同: | 九、写―写。 | 替\niSBヽ 978-7-53,4-6579
Encode 结果:
{
"elidedText": "ヽ市キ華虐蒙i川 鯨 | 根据 《幼ブL\n‐\ぷ弄始吋同: | 九、写―写。 | 替\niSBヽ 978-7-53,4-6579"
}
错误: 转义符未进行处理
修复:
writer.h 442行添加
else if (escape[static_cast(c)] == '\') {
PutUnsafe(*os_, '\');
}
项目主页上介绍了怎么对结构体的成员变量重命名 那是否可以对结构体重命名呢
struct TimeSpan
{
string startTime;
string endTime;
XPACK(O(startTime, endTime));
};
struct CMSearchDescription
{
string searchID;
vector<TimeSpan> timeSpanList;
XPACK(O(searchID, timeSpanList))
};
在最后的xml中,结构体TimeSpan对应的是timeSpan标签,我又不想在代码中将结构体名改为timeSpan,有什么办法吗
您好,非常感谢技术的沟通,在支持pcdata后,能发布一个新版本吗,因为我是通过conan来引用的xpack库,但是最新的1.0.5的版本还没有master上的功能呢
在pro里加了include(xpack-1.0.5/xpack.pri),
在linux下正常工作,
windows下Qt + MSVC,报以下错误:use of undeclared identifier 'X_PACK_L1_ENCODE'
另外,#include "xpack/xpack.h"不识别,#include "xpack.h"才行
json文本为:{"data":{"info":[]}}时,解析会出错。在document.h的IsArray出错。
struct Info
{
int base, Id;
XPACK(O(base, Id));
};
struct dataInfo
{
std::vector data;
XPACK(O(data));
};
dataInfo sInfo;
xpack::json::decode("{"data":{"info":[]}}", sInfo);
为什么没在设计中看到有关于容器限制相关,实际生产中一些string stl等 异常报文也是要重点考虑的呢
Hello,
Thanks for this great library. While using it, I encounterd an error with overflowed integer. I was able to reproduce this on json-data.cpp
with a large integer.
* limitations under the License.
*/
#include <iostream>
#include "xpack/json.h"
#include <string>
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);
} else if (e.type == "user") {
User u;
e.data.Get(u);
cout << u.id << endl;
} else {
}
}
int main(int argc, char *argv[]) {
(void)argc;
(void)argv;
string s1 = "{\"type\":\"range\", \"data\":{\"min\":12, \"max\":33}}";
string s2 = "{\"type\":\"user\", \"data\":{\"id\":123344343434343435, \"name\":\"xpack\"}}";
test(s1);
test(s2);
return 0;
}
With a large number in the id
of s2
, no error was reported (I tried with a larger number and an error indicting not integer is given, which I think should be the correct behavior), but the output is -545967093
.
I'm using the latest version of the library on Ubuntu 20.04LTS.
两个struct属继承关系
struct A {
string t1;
XPACK(O(t1));
};
struct B : public A{
string t2;
XPACK(I(A), O(t2));
};
现在我有一个函数,接受的是A类型,但传递的是B类型,转成JSON字符串后B类型的数据丢了?
VOID invoke(A a)
{
string data = xpack::json::encode(a); // data 没有 t2 ?
}
B b = {0};
b.t1 = "111";
b.t2 = "222";
invoke(b);
json.h中有decode(std::string&, T&),可以满足字符串到对象的解析。但在实际应用中,可能需要对复杂结构体中的部分Value对象解析到Object中。参考现有代码,加起来应该比较简单,比如:
template <class T>
static void decode(const rapidjson::Value* value_data, T &val) {
JsonDecoder doc(value_data);
doc.decode(NULL, val, NULL);
}
enum class Enum : int64_t 类型的枚举解析会出错,解析出来是个很大的值, 如果是enum class Enum : int 不会出错,是哪里的bug 吗
目前是将xpack的解析json功能用于构造测试用例。代码中有一个结构体比较大,有3892616byte,在执行用例的时候出现了段错误。尝试减少结构体成员个数,发现3568232byte可以正常运行,但是目前不知道明确的边界。
这个结构体之前在ut测试中一直存在,区别只是第一次将其放到了json文件中,所以怀疑是不是xpack哪个地方产生了对结构体大小的限制。
问题如题.
在windows下,使用 clang 16.0.0-x86_64-pc-windows-msvc编译报错.
报错信息如下:
[build] 正在启动生成
[proc] 执行命令: chcp
[proc] 执行命令: "D:\Program Files\CMake\bin\cmake.EXE" --build d:/temp/xpack-clang/build --config Debug --target all --
[build] [1/2 50% :: 0.683] Building CXX object CMakeFiles/xpack-clang.dir/main.cpp.obj
[build] FAILED: CMakeFiles/xpack-clang.dir/main.cpp.obj
[build] C:\LLVM\bin\clang++.exe -ID:/temp/xpack-clang/include/xpack -g -Xclang -gcodeview -O0 -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd -MD -MT CMakeFiles/xpack-clang.dir/main.cpp.obj -MF CMakeFiles\xpack-clang.dir\main.cpp.obj.d -o CMakeFiles/xpack-clang.dir/main.cpp.obj -c D:/temp/xpack-clang/main.cpp
[build] D:/temp/xpack-clang/main.cpp:9:3: error: use of undeclared identifier 'X_PACK_L1_ENCODE'
[build] XPACK(M(age, name));
[build] ^
[build] D:/temp/xpack-clang/include/xpack/xpack.h:191:45: note: expanded from macro 'XPACK'
[build] X_PACK_ENCODE_BEGIN X_PACK_N(X_PACK_L1, X_PACK_L1_ENCODE, __VA_ARGS__) return __x_pack_ret; }
[build] ^
[build] D:/temp/xpack-clang/include/xpack/xencoder.h:150:16: note: in instantiation of function template specialization 'xpack::XEncoder<xpack::JsonWriter>::encode_struct<person>' requested here
[build] return encode_struct(key, val, ext);
[build] ^
[build] D:/temp/xpack-clang/include/xpack/json_encoder.h:273:12: note: in instantiation of function template specialization 'xpack::XEncoder<xpack::JsonWriter>::encode<person>' requested here
[build] en.encode(NULL, val, NULL);
[build] ^
[build] D:/temp/xpack-clang/include/xpack\json.h:50:19: note: in instantiation of function template specialization 'xpack::JsonEncoder::encode<person>' requested here
[build] return en.encode(val);
[build] ^
[build] D:/temp/xpack-clang/main.cpp:17:29: note: in instantiation of function template specialization 'xpack::json::encode<person>' requested here
[build] std::cout << xpack::json::encode(p) << std::endl;
[build] ^
[build] 1 error generated.
[build] ninja: build stopped: subcommand failed.
[proc] 命令“"D:\Program Files\CMake\bin\cmake.EXE" --build d:/temp/xpack-clang/build --config Debug --target all --”已退出,代码为 1
[driver] 生成完毕: 00:00:00.753
[build] 生成已完成,退出代码为 1
xpack.h
中vscode显示该宏X_PACK_L1_ENCODE
是启用的 请问该如何解决?
Dear developer,
How could I do if I wanna to customize basetype's serialisation? For example, if I want to convert an int to its hex string representation during serialize or deserialize, current xtype.cpp cannot do this, its just warning "already defined" when I write template.
Any suggestions well be welcome.
--> Caught signal 11 (at 0x16, invalid address) <--
backtrace(size=30)
你好,请问以下xml节点,结构体的XPACK怎么写
文件1网址base64加密
文件2网址base64加密
注:url是数组成员,带attr,但内容是直接给节点的
错误提示:错误 C4146 一元负运算符应用于无符号类型,结果仍为无符号类型 \xpack\util.h 89
Currently Im using the following workaround.
// std::map<enum, T>
template <typename TK, typename TV>
struct is_xpack_xtype<std::map<TK, TV>> {
static bool const value = true;
};
template <class OBJ, typename TK, typename TV>
bool xpack_xtype_decode(OBJ& obj, const char* key, std::map<TK, TV>& val, const Extend* ext) {
if constexpr (std::is_enum_v<TK>) {
std::map<int64_t, TV> tmp;
if (!obj.decode(key, tmp, ext)) {
return false;
}
val.clear();
for (auto&& item : tmp) {
val[item.first] = item.second;
}
return true;
} else {
static_assert(false, "std::map with this key type is not supported to decode");
return false;
}
}
template <class OBJ, typename TK, typename TV>
bool xpack_xtype_encode(OBJ& obj, const char* key, const std::map<TK, TV>& val, const Extend* ext) {
if constexpr (std::is_enum_v<TK>) {
std::map<TK, TV> tmp;
for (auto&& item : val) {
tmp[item.first] = item.second;
}
return obj.encode(key, tmp, ext);
} else {
static_assert(false, "std::map with this key type is not supported to encode");
return false;
}
}
是否能添加对UTF8-Bom文件的支持 xpack\json_decoder.h
JsonDecoder(const std::string& str, bool isfile=false):xdoc_type(NULL, ""),_doc(new rapidjson::Document),_val(_doc) {
std::string err;
std::string data;
do {
if (isfile) {
std::ifstream fs(str.c_str(), std::ifstream::binary);
if (!fs) {
err = "Open file["+str+"] fail.";
break;
}
std::string _tmp((std::istreambuf_iterator<char>(fs)), std::istreambuf_iterator<char>());
if (_tmp[0] == '\xEF' && _tmp[1] == '\xBB' && _tmp[2] == '\xBF')//if this formate is utf8-bom
{
data = _tmp.substr(3, _tmp.length() - 3);
}
else
{
data.swap(_tmp);
}
_doc->Parse(data);
} else {
_doc->Parse(str);
}
用的是clion自带的mingw11.0
1.0.1已经是一年前发的了,可以发一个新的么。
我准备把xpack做成conan包方便使用。
conan-io/conan-center-index#9487
请问原来x2struct中提供的xhas()方法在xpack中是否有替代函数?
后面是否考虑支持std::optional,开发者可通过判成员是否为std::nullopt,达到类似调用xhas()的目的。
新建项目是可以运行的,但是在旧项目中就提示use of undeclared identifier NULL.请问是什么原因呢,另外demo中有ATTR的用法吗,找了下没找到,ATTR是只支持encode不支持decode吗
环境:ubuntu22.01
工具:clion
error:xpack\extend.h(47): error C2039: “set”: 不是“std”的成员
solution:include
windows 10, VS2017 x64
字段从配置文件解析到结构体后,调用字段自定义校验函数
结构体解析完成后,调用结构体自定义校验函数
是否考虑在decode出错时增加 Error Enum 类型。来表示诸如 "值类型错误/ErrorValueType",“缺少必须的域/MissingmandatoryFiled”等解析错误?
或者将rapidxml/rapidJson 解析时的返回值透传回来,类似如下
bool isSuccess = false;
try
{
isSuccess = xpack::json::decode(content.toStdString(), j);
}
catch (const std::exception& e)
{
LOGERROR("decode error ");
}
return isSuccess;
map的value类型是结构体,解析有问题。
示例:
struct value {
int a;
XPACK(O(a));
};
std::map<std::string, value> map;
xpack::json::decode("{\"123\":{\"a\":1011}, \"234\":{}}", map);
cout << map.size() << ',' << map["123"].a << ',' << map["234"].a << endl;
后面空的234的a跟前面123里面的a值一样,看起来是内部实现复用了结构体成员。
您好 请问XPACK是否支持union类型变量 如果不支持 有什么替代解决方案 非常感谢
struct JanusResponse {
std::string janus;
std::string transaction;
int64_t session_id = -1;
int64_t sender = -1;
XPACK(O(janus, transaction, session_id, sender));
};
struct CreateSessionData {
int64_t id;
XPACK(O(id));
};
struct CreateSessionResponse : public JanusResponse {
CreateSessionData data;
XPACK(I(JanusResponse), O(data));
};
std::string js = "{\"janus\":\"success\",\"transaction\":\"RftQMdXKkq6q\",\"data\":{\"id\":6053426257610390}}";
vi::CreateSessionResponse model;
xpack::json::decode(js, model);
std::string result = xpack::json::encode(model);
你好,我发现decode结果是对的,encode的结果是错的,id的值不对。是我的用法不对?
(在x2struct上能正确encode)
xml支持pcdata后,麻烦发布个新版本,我的程序是通过conan引入xpack的
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.