Skip to content

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

Closed
@Regfare

Description

@Regfare

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions