json icon indicating copy to clipboard operation
json copied to clipboard

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

Open askraskr opened this issue 1 year ago • 5 comments

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

askraskr avatar May 19 '24 12:05 askraskr

Can you try with unsigned long?

nlohmann avatar May 19 '24 12:05 nlohmann

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?

askraskr avatar May 19 '24 12:05 askraskr

I want to tackle this issue, please assign this issue to me.

Ranj8521Kumar avatar Aug 28 '24 18:08 Ranj8521Kumar

Go ahead!

nlohmann avatar Aug 28 '24 19:08 nlohmann

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

nlohmann avatar Nov 18 '24 18:11 nlohmann

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.

github-actions[bot] avatar Jan 30 '25 00:01 github-actions[bot]