nomango / configor Goto Github PK
View Code? Open in Web Editor NEWA light weight configuration library for C++
License: MIT License
A light weight configuration library for C++
License: MIT License
vs2017报错:“_snprintf”: 不是“std”的成员
非常不错的轮子,但是我在使用中遇到如下问题
using namespace std;
using namespace configor;
json j;
j["123"] = "12345";
string s = j["123"];
vs2017会报错: configor::detail::json_serializer<configor::basic_configconfigor::json_args,configor::basic<_Args>::default_encoding,cofigor::basic_config<_Args>::default_encoding>类没有构造函数
json.value_.data.vector->push_back(BasicJsonTy());
parse_value(json.value.data.vector->back());
如果是 "Array":[] 处理不出来
###例程:
#include
#include
#include
#include <configor/json.hpp>
using namespace configor;
// 用户类
struct User
{
int user_id;
std::string user_name;
JSON_BIND(User, user_id, user_name);
};
int main(int argc, char** argv)
{
std::stringstream s("{"user_id": 10001, "user_name": "John"}");
// 解析json内容,并反序列化到user对象
User user;
s >> json::wrap(user);
// 序列化user对象并输出
std::cout << json::wrap(user) << std::endl; // {"user_id":10001,"user_name":"John"}
return 0;
}
###错误:
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 C2679 二进制“=”: 没有找到接受“bool”类型的右操作数的运算符(或没有可接受的转换) jsontest\jsontest\include\configor\configor_parser.hpp 125
使用MinGW编译时,出现如下错误:
F:\jzyou\local-repos\Code\D-Phone\thirtylib\configor\configor_basic.hpp:705: error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum)
typename = typename std::enable_if<!is_config<_Ty>::value && detail::is_configor_getable<basic_config, _Ty>::value && !std::is_pointer<_Ty>::value>::type>
~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~
编译时,基于c++17
我在用VS17的build tool和nvcc在Windows10上编译的时候会出现namespace "std" has no member "isdigit"
这个error,然后在cpp reference上查到isdigit这个是在里面的,在json_parser.hpp里加上include <cctype>
就可以编译通过了,建议在json_parser.hpp里加上这一行
我记得老版解析json的时候可以通过Reader.parse的返回值判断解析是否成功,新版的怎么判断?
When I ask if there exist a key in the json object, I use:
bool ifhaskey(configor::json js, std::string key) {return !js[key].is_null();}
But it will add null type assoicated with this key to original json. When I just want to ask a key if it is can be accessiable and don't want to set an additional key to mark it null value, which method can be used?
./include/configor/configor_conversion.hpp:106:123: fatal error: template instantiation depth exceeds maximum of 2048 (use -ftemplate-depth= to increase the maximum)
_ConfTy, _Ty, typename std::enable_if<std::is_void<decltype(config_bind<_Ty>::from_config(std::declval<const _ConfTy&>(), std::declval<_Ty&>()))>::value, void>::type>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
compilation terminated.
vs2022 c++14
json j = {
{"name", "JSON 解析和序列化"},
{"item", "测试JSON文件加载"}
};
wjson wj = {
{L"name", L"JSON 解析和序列化"},
{L"item", L"测试JSON文件加载"}
};
wcout << L"wchar_t\t" << wj << endl;
cout << "char\t" << j << endl; // configor_serialization_error
处理中文只能用wjson吗
struct Config
{
std::string name;
}
Config config;
config.name = XXX;//从MySQL中取值,MySQL设置了字符集为utf8,假定XXX=“**”,此时debug时看到的name就是乱码的
json test;
test["test"] ="中文Test123434";
test["config"] = config;
test.dump()的结果,name显示**,但"中文Test123434"却显示乱码了,请问该如何处理?
结果是以res.set_content(json_result.dump(), "text/plain");形式返回的
伪代码:
// 1638341381.633359
double time = get_current_time();
// 初始化赋值
json j = {
{"time": time}
};
// 获取值时变成float
j["time"].get()
// 打印 1.63834e+09
是需要额外设置什么参数吗?
作者有没有考虑加入追加数组元素功能呢
auto arr = se.get<json::array_type>(); for (unsigned int i = 0; i < arr.size(); i++) { getversion Ver; stringstream ss; ss << arr[i]; ss >> json::wrap(Ver); }
这样可以正常获取arr[i]中的内容并写入Ver,但是
getversion Ver; arr[i].wrap(Ver);
这样获取不到任何内容
所以arr[i].wrap(Ver)作用是啥啊)
应该在json.hpp的json_lexer::scan函数内的switch中加入一个case:
case '\0':
result = token_type::end_of_input;
break;
以应对可能从流上读入C字符串末尾的结束符,以免抛出异常:
config deserialization error: unexpected character '\u0000'
你好,在使用configor时,json包含中文字符串时在序列化时会出错,具体代码如下
#include <iostream>
#include "json.hpp"
int main() {
using namespace std;
using namespace configor;
json j = "宽带连接";
cout << j.dump() << endl;
return 0;
}
dump()异常
0x76D7B4B2 处(位于 Project1.exe 中)引发的异常: Microsoft C++ 异常: configor::configor_serialization_error,位于内存位置 0x00DEEA98 处。
0x76D7B4B2 处(位于 Project1.exe 中)引发的异常: Microsoft C++ 异常: [rethrow],位于内存位置 0x00000000 处。
0x76D7B4B2 处(位于 Project1.exe 中)有未经处理的异常: Microsoft C++ 异常: configor::configor_serialization_error,位于内存位置 0x00DEEA98 处。
我想在构造对象中包含数组,构造函数如下:
listFileJson=json::object({"filecount",namelist.size(),"fileList",json::array({})});
for(int i=0;i<namelist.size();++i)
listFileJson["fileList"].push_back({"filename",namelist[i]});
结果push的时候报错
假如我下面一段代码
json makeJson(int arr[2]) {
json res = json::array(arr); // 没有这样的重载
return res;
}
或者有这样一个结构体
struct S {
int a[5];
JSON_BIND(S,a); // 绑定a失败
};
是否有相应的解决方案?
请教下如何构建value为int64类型的json。
我如下写法ts会超出范围:
jsonxx::json j = {
{"msgId", “12345”},
{"ts", 1621835751000},
};
std::string strSend = j.dump();
bool server::removeSongsFromPlaylist(configor::json song_list,size_t pid,size_t sid){
int uid = session_map[sid];
for(int i = 0;i < song_list;i++){
auto iter = sobj.config_file["users"][uid]["playlists"][pid].find(song_list[i]);
sobj.config_file["users"][uid]["playlists"][pid].erase(iter);
}
return sobj.updateConfig();
}
我在这个函数中使用了erase函数,但是编译时报错了:
In file included from ./include/configor/configor.hpp:23,
from ./include/configor/json.hpp:22,
from ./include/storage.cpp:13,
from ./include/main.hpp:1,
from ./include/main.cpp:1,
from ./media_center.cpp:1:
./include/configor/configor_basic.hpp: In instantiation of ‘_IterTy configor::basic_config<_Args>::erase(_IterTy) [with _IterTy = configor::detail::iterator<const configor::basic_config<configor::json_template_args> >; <template-parameter-2-2> = void; _Args = configor::json_template_args]’:
./include/main.cpp:115:68: required from here
./include/configor/configor_basic.hpp:317:17: error: conversion from ‘iterator<basic_config<[...]>>’ to non-scalar type ‘iterator<basic_config<[...]>>’ requested
317 | _IterTy result = end();
| ^~~~~~
./include/configor/configor_basic.hpp:323:20: error: ‘struct configor::detail::iterator<const configor::basic_config<configor::json_template_args> >’ has no member named ‘it_’
323 | result.it_.object_iter = value_.data.object->erase(pos.it_.object_iter);
| ~~~~~~~^~~
./include/configor/configor_basic.hpp:323:68: error: ‘struct configor::detail::iterator<const configor::basic_config<configor::json_template_args> >’ has no member named ‘it_’
323 | result.it_.object_iter = value_.data.object->erase(pos.it_.object_iter);
| ~~~~^~~
./include/configor/configor_basic.hpp:329:20: error: ‘struct configor::detail::iterator<const configor::basic_config<configor::json_template_args> >’ has no member named ‘it_’
329 | result.it_.array_iter = value_.data.vector->erase(pos.it_.array_iter);
| ~~~~~~~^~~
./include/configor/configor_basic.hpp:329:67: error: ‘struct configor::detail::iterator<const configor::basic_config<configor::json_template_args> >’ has no member named ‘it_’
329 | result.it_.array_iter = value_.data.vector->erase(pos.it_.array_iter);
这是库的问题吗,如果不是的话,我哪里写错了 qwp
复现代码
using namespace jsonxx;
using namespace std;
int main()
{
json j;
j["a_float"] = 1.0f;
cout << j << endl;
json p = json::parse("{\"a_float\":1}"); // 上面一行会输出这里的json字符串,然而浮点数输出时没有了小数点
float f = p["a_float"]; //转换时会出错
printf("%f",f);
return 0;
}
个人猜想是在使用std流时没有强制要求输出小数点,因为比如说std::cout<<1.0f<<std::endl
会直接输出1
而不输出小数点,而使用例如printf("%f", 1.0f)
时可以正常输出小数点。
最近比较忙没时间看源码了,作者麻烦修一下吧,或者过几周我再交个PR
std::string的data()内容如下:
{
"code": 0,
"session": "sIMIG9Fe"
}
抛出一下错误:
terminate called after throwing an instance of 'configor::configor_type_error'
what(): config type error: cannot convert type 'string' to type 'integer' (implicitly)
eg
class address {
string city;
string street;
};
class userInfo {
string name;
int age;
address& addInfo;
}
struct fifo_json_args : json_args
{
template <class _Kty, class _Ty, class... _Args>
using object_type = nlohmann::fifo_map<_Kty, _Ty, _Args...>;
};
// fifo_json 是按插入序排列的
using fifo_json = configor::basic_config<fifo_json_args>;
例程里,用fifom_map替换std::map,使用时编译错误,vs2022
fifo_json j1 = { {"2.name", "2"}, {"1.name", "1"}, {"0.name", "0"} };
fifo_map.hpp(341,19): error C2039: "add_key": 不是 "std::less<std::basic_string<char,std::char_traits,std::allocator>>" 的成员
如何解析要求传入iter_begin和iter_end的数据?或如何从char数组中解析?
我们打算在一个系统中使用这个来频繁读一个配置文件。这里面会建立大量configor::json的临时对象。请问当前的实现的所有数据成员都在栈区吗,如果在堆区,其析构问题是不是已经搞定了?
我看您的文档里面没有提到析构的问题。
建议添加对regex的支持/^nihao/
我注意到README个段代码:
取值的同时判断类型
int n;
bool ret = j["boolean"].get_value(&n); // 若取值成功,ret 为 true
c++在新标准中已经对这种情况做出标准做法:使用std::optional.
修改成标准代码:
取值的同时判断类型
int n;
auto ret = j["boolean"].get_value(); // 若取值成功,ret 为 true
//get_value()应该返回std::optional<int>
if(ret.has_value()) n = ret.value();
else { /*do some thing*/}
如果没有什么特殊需求,都应该使用标准库中的方法.
Fix by add namespace 'configor':
#define JSON_BIND(value_type, ...) CONFIGOR_BIND_WITH_CONF(configor::json, value_type, __VA_ARGS__)
configor/include/configor/json.hpp
Line 74 in 14f10d5
代码如下:
json j;
char * BrokerID = "测试中文";
j["BrokerID"] = BrokerID;
std::string json_str = j.dump();
生成json为:
{"BrokerID":"\uffb2\uffe2\uffca\uffd4\uffd6\uffd0\uffce\uffc4"}
这里生成的unicode编码使用站长工具的unicode转中文工具,转换出来是乱码
#include
#include
#include
#include <configor/json.hpp>
using namespace configor;
// 用户类
struct User
{
int user_id;
std::string user_name;
JSON_BIND(User, user_id, user_name);
};
int main(int argc, char** argv)
{
std::stringstream s("{"user_id": 10001, "user_name": "John"}");
// 解析json内容,并反序列化到user对象
User user;
s >> json::wrap(user);
// 序列化user对象并输出
std::cout << configor::json::wrap(user) << std::endl; // {"user_id":10001,"user_name":"John"}
return 0;
}
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 C2064 项不会计算为接受 2 个参数的函数 WindscribeFindNode f:\usr\local\configor-0.9.15\include\configor\configor_basic.hpp 916
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 C2064 项不会计算为接受 3 个参数的函数 WindscribeFindNode f:\usr\local\configor-0.9.15\include\configor\configor_basic.hpp 886
我是完全按照例子上写的啊)
Json序列化带有中文会出错,也无法正常迭代
迭代器在碰到中文字符时会抛出异常,wjson也是一样
用一个双核A9的板子传一张base46的jpeg图片,大小1M左右,序列化到std::string要花6秒。请问问题主要出在哪里,谢谢。
请问枚举类型该怎么使用JSON_BIND这个宏呢?
例如如下代码
enum TT{
a,
b,
// JSON_BIND(TT,a,b);
};
class Person
{
public:
int age;
string name;
TT mi;
std::map<std::string, int> m;
int tt;
JSON_BIND(Person,age,name,mi,m,tt);
};``
代码没变,终端输出的东西少了一些,但还有错误
In file included from ./include/configor/configor.hpp:22,
from ./include/configor/json.hpp:22,
from ./include/storage.cpp:13,
from ./include/main.hpp:1,
from ./include/main.cpp:1,
from ./media_center.cpp:1:
./include/configor/configor_basic.hpp: In instantiation of ‘_IterTy configor::basic_config<_Args>::erase(_IterTy) [with _IterTy = configor::detail::iterator<const configor::basic_config<configor::json_args> >; <template-parameter-2-2> = void; _Args = configor::json_args]’:
./include/main.cpp:115:68: required from here
./include/configor/configor_basic.hpp:317:17: error: conversion from ‘iterator<basic_config<[...]>>’ to non-scalar type ‘iterator<basic_config<[...]>>’ requested
317 | _IterTy result = end();
| ^~~~~~
是一定要用下标吗
(xiaokang00010和leadsoft-ware)的使用者是同一个人,不用介意 ( ̄∇ ̄)
$ g++ test.cpp -I jsonxx/include/
test.cpp:17:8: error: explicit specialization of ‘template<class _Ty, class _BasicJsonTy, typename std::enable_if<jsonxx::is_basic_json<_BasicJsonTy>::value, int>::type > struct jsonxx::json_bind’ outside its namespace must use a nested-name-specifier [-fpermissive]
17 | struct json_bind {
| ^~~~~~~~~~~~~~~
可以支持int64么
如题,我序列化了一个空的json对象到文件,然后第二次读取的时候直接报错了
提示
terminate called after throwing an instance of 'configor::configor_deserialization_error'
what(): config deserialization error: unexpected token 'end_of_input'
Aborted
这是我使用的方式错了还是库原本就有bug
快速上手--使用 std::initializer_list
为 JSON 对象赋值代码
// 第二个对象
json obj2 = ...
{ "object", {
{ "key", "value" },
{ "key2", "value2" },
}, //此处漏了一个 }
};
比如:{ “id”: 1 , "test": "test"} ,想删除test键值
json对象内部委托std::map实现,key值是有序的,不是插入序,可以留个接口让key值按照插入序来序列化吗?
平时用vcpkg比较多,算是一个比较好的cpp代码包管理器。
异常:
terminate called after throwing an instance of 'configor::configor_deserialization_error'
what(): config deserialization error: unexpected token 'end_of_input'
环境:
gcc: 11.1.0
clang: 12.0.1
第一次运行时无错,后来运行就会抛出异常。
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.