Jsoncpp增量写入
jsoncpp write incrementally
本文关键字:Jsoncpp 更新时间:2023-10-16
我必须将我的应用程序的操作记录到json文件中。预计应用程序将持续数周,所以我想增量地编写json文件。
目前我正在手动编写json,但是有一些使用Jsoncpp库的日志阅读器应用程序,应该也可以使用Jsoncpp库编写日志。
但是在手册和一些例子中,我没有发现任何类似的东西。它总是像这样:
Json::Value root;
// fill the json
ofstream mFile;
mFile.open(filename.c_str(), ios::trunc);
mFile << json_string;
mFile.close();
这不是我想要的,因为它不必要地填充内存。我想循序渐进地做……一些建议吗?
我是jsoncpp的维护者。不幸的是,它不能增量地编写。它在不使用额外内存的情况下将写入流,但这对您没有帮助。
如果您可以切换到纯JSON到JSON行,如我如何在Python中从文件/流中惰性读取多个JSON对象所述?(感谢ctn提供链接),您可以这样做:
const char* myfile = "foo.json";
// Write, in append mode, opening and closing the file at each write
{
Json::FastWriter l_writer;
for (int i=0; i<100; i++)
{
std::ofstream l_ofile(myfile, std::ios_base::out | std::ios_base::app);
Json::Value l_val;
l_val["somevalue"] = i;
l_ofile << l_writer.write(l_val);
l_ofile.close();
}
}
// Read the JSON lines
{
std::ifstream l_ifile(myfile);
Json::Reader l_reader;
Json::Value l_value;
std::string l_line;
while (std::getline(l_ifile, l_line))
if (l_reader.parse(l_line, l_value))
std::cout << l_value << std::endl;
}
在这种情况下,你不再有一个JSON文件了…但它是有效的。
相关文章:
- 使用jsoncpp库时出现链接问题
- 使用JsonCpp将数据返回到带有pybind11的python会在python调用中产生Symbol not foun
- 使用 jsoncpp 解析 json 数组字符串
- 如何将 jsoncpp 库包含在 unix 上的项目中?
- JsonCpp在退出函数时抛出LogicError
- 使用jsoncpp-NuGet包时获取未解析的外部符号
- JSONCPP 只用一个元素写入 JSON 数组
- 使用 jsoncpp 读取 JSON 文件
- 使用 JSONCPP 将 JSON 数据增量写入文件
- 使用 jsoncpp 时从 JSon 剥离私有数据的最佳方法
- 如何创建和填充 jsoncpp 数组 (jsoncpp.sourceforge)
- 在这种情况下如何让 jsoncpp 抛出?
- 为什么 JsonCpp http 客户端在 201 响应代码上失败?
- 要从url解析的JsonCpp
- 将它们添加到C 中的JSONCPP模块中的数组之后
- 转义 json 的文件路径 (JsonCPP)
- 如何设置JSONCPP用于Visual C 项目
- c++ jsoncpp 读取 json 文件
- 在 jsoncpp 中区分 int 和 uint 类型
- 使用 jsonCpp 库从C++文件中解析 json 时出错