Error 302: type must be number, but is it number
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.
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
- [x] The bug also occurs if the latest version from the
developbranch is used. - [X] I can successfully compile and run the unit tests.
Can you try with unsigned long?
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?
I want to tackle this issue, please assign this issue to me.
Go ahead!
I cannot reproduce the issue. Here is my try:
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main() {
json record = R"({
"Latitude": 0,
"Longitude": 0,
"PassedTime": 1715948646000,
"post_timeout_ms": 15000,
"redirect_count": 0
})"_json;
std::cout << record["PassedTime"].template get<long>() << std::endl;
}
Output:
1715948646000
This issue has been closed after being marked as stale for 10 days without any further activity. If this was done in error or the issue is still relevant, please feel free to reopen it or create a new issue. We appreciate your understanding and contributions.