nlohmann / json

JSON for Modern C++
https://json.nlohmann.me
MIT License
41.25k stars 6.57k forks source link

Error 302: type must be number, but is it number #4374

Open askraskr opened 1 month ago

askraskr commented 1 month ago

Description

Just compiled and installed the last version of nlohmann_json library with CMake. I was reading JSON documents from Mongo. Converted Mongo documents to nlohmann_json::jsons.

after compiling and running it, I got this error: type must be number, but is it number.

number

Reproduction steps

std::vector<nlohmann::json> jsonv;
for (auto cursor = client[db][cl].find(make_document(kvp("_id", make_document(kvp("$gt", bsoncxx::oid(some_id)))))); const auto &doc: cursor) {
        json = std::move(nlohmann::json::parse(bsoncxx::to_json(doc)));
        jsonv.push_back(std::move(json));
    }

Expected vs. actual results

I expected an integer to be printed. As simple as that.

Minimal code example

sample json document from Mongo without _id:
{
    "Latitude": 0,
    "Longitude": 0,
    "PassedTime": 1715948646000,
    "post_timeout_ms": 15000,
    "redirect_count": 0,
}

std::cout << record["PassedTime"].template get<long>() << std::endl;

Error messages

terminate called after throwing an instance of 'nlohmann::json_abi_v3_11_3::detail::type_error'
  what():  [json.exception.type_error.302] type must be number, but is number

Compiler and operating system

Debian with g++-11

Library version

3.11.3

Validation

nlohmann commented 1 month ago

Can you try with unsigned long?

askraskr commented 1 month ago

I did. After a few hundred records read from Mongo, it failed again with the same error. BTW, all PassedTime values are greater than zero (0).

P.S. How does nlohmann::json work with numbers? What makes a the type of a number nlohmann::json::value_t::number_integer or nlohmann::json::value_t::number_unsigned?