Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting size of deserialized bson document #2131

Closed
1 of 3 tasks
andrejlevkovitch opened this issue May 22, 2020 · 2 comments
Closed
1 of 3 tasks

Getting size of deserialized bson document #2131

andrejlevkovitch opened this issue May 22, 2020 · 2 comments
Labels
kind: question state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@andrejlevkovitch
Copy link

andrejlevkovitch commented May 22, 2020

Sometimes bson documents can be combined one after another (for example when asynchronously read from socket) and in this case they must be readed separately. First document can be readed as:

nlohmann::json::from_bson(body, false);

But now is a problem: how to read second document? Because we don't know haw match bytes already read. Of cause we can read first 4 bytes in body and get size of bson manually, but, I think, this will be a usefull feature, if function from_bson also return capacity of readed bytes (for example as 4-th argument). In this case reading combined bson documents will be match simpler:

size_t &length = 0;
nlohmann::json::from_bson(body, false, true, &length);

// and reading second document
nlohmann::json::from_bson(body.begin() + length, body.end());

What do you think about this?

Which version of the library did you use?

  • latest release version 3.7.3
  • other release - please state the version: ___
  • the develop branch
@FrancoisChabot
Copy link
Contributor

FrancoisChabot commented May 26, 2020

The correct (in my opinion) syntax for this would be:

auto current = body.begin();
auto first = nlohmann::json::from_bson(current, body.end(), false);
auto second = nlohmann::json::from_bson(current, body.end(), false);

This could be easily achieved with proper iterator-based input (which is also how I think #1813 should be tackled)

However, since bson, as a format, is prefixed with the document size, you can currently do:

std::uint32_t first_msg_size = 0;
assert(body.size() >= 4);
std::memcpy(&first_msg_size, body.data(), 4);

// insert potential endian flip here

assert(body.size() >= first_msg_size);
auto separator = std::next(body.begin(), first_msg_size);
auto first = nlohmann::json::from_bson(body.begin(), separator);
auto second = nlohmann::json::from_bson(separator , body.end());

@stale
Copy link

stale bot commented Jun 26, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Jun 26, 2020
@stale stale bot closed this as completed Jul 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated
Projects
None yet
Development

No branches or pull requests

2 participants