I need your help about nlohmann's json library. I wrote this code.
std::vector<std::uint8_t> v = nlohmann::json::to_bson(jInit);
std::vector<std::uint8_t> s;
for (auto& i : v)
{
s.push_back(v[i]);
}
std::ofstream ofs(s_basePath + "dump.txt");
ofs << nlohmann::json::from_bson(s).dump();
This simply converts json to bson, then convert the bson to json and dump it as a text file.
For some reasons, I have to use push_back() and get json back.
The problem is, size of the dumped text file is 0 kb, nothing will be dumped.
I tried also this code and it is working.
std::vector<std::uint8_t> v = nlohmann::json::to_bson(jInit);
std::ofstream ofs(s_basePath + "dump.txt");
ofs << nlohmann::json::from_bson(v).dump();
I have no idea what's the difference between vector v and s.
The problem has nothing to do with json/bson. It's just that you use every
uint8_t
in the original bson data as an index into the very same data, which scrambles everything up.Wrong:
The correct loop should look like this:
If you need a copy of the vector with the bson data, you can simply copy it though: