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

json::to_bson does not handle unsigned 64 bit integers #3894

Open
1 of 2 tasks
Regfare opened this issue Dec 20, 2022 · 3 comments · May be fixed by #4380
Open
1 of 2 tasks

json::to_bson does not handle unsigned 64 bit integers #3894

Regfare opened this issue Dec 20, 2022 · 3 comments · May be fixed by #4380
Labels

Comments

@Regfare
Copy link

Regfare commented Dec 20, 2022

Description

Trying to convert a nlohmann::json object which contains an unsigned int of 64 bit length breaks BSON conversion.
I do consider this to be a bug, as BSON should be capable of handling those values [1] , but the corresponding header-bits are not set in write_bson_unsigned. Only entry headers 0x10 (for int32) and 0x12 (for int64) are written, but 11 (for uint_64) is not.

Reproduction steps

This bug is triggered whenever one uses the json::to_bson function on any object that contains an uint64_t, that really uses all 64 bits.

Expected vs. actual results

I expect BSON to generated, but I get an error message (see below)

Minimal code example

#include <map>
#include <string>
#include <nlohmann/json.hpp>

int main(){
  std::map<size_t,std::string> m={{14631357879723465589u /*really just any large enough uint64_number*/,"To_Big"}};
  nlohmann::json j;
  j["map"]=m;
  auto bson=nlohmann::json::to_bson(j); //can not be converted
}

Error messages

terminate called after throwing an instance of 'nlohmann::detail::out_of_range'
  what():  [json.exception.out_of_range.407] integer number 14631357879723465589 cannot be represented by BSON as it does not fit int64

Compiler and operating system

Clang-13 on Mint 21 64bit, Clang Trunk (via godbolt)

Library version

3.11.2 (develop branch via godbolt) , 3.10.5

Validation

@nlohmann nlohmann added the aspect: binary formats BSON, CBOR, MessagePack, UBJSON label Dec 21, 2022
@nlohmann
Copy link
Owner

It seems I was confused with the alias "timestamp" when I implemented BSON. You are right - it makes no sense to not support uint64 numbers.

An extension should be straightforward, but to do it properly with tests may take some time.

@anshulchou-123
Copy link

@nlohmann Can I raise a PR for this?

@nlohmann
Copy link
Owner

Sure!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants