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

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

Open
2 tasks done
askraskr opened this issue May 19, 2024 · 5 comments
Open
2 tasks done

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

askraskr opened this issue May 19, 2024 · 5 comments
Labels
kind: bug state: needs more info the author of the issue needs to provide more details

Comments

@askraskr
Copy link

askraskr commented May 19, 2024

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
Copy link
Owner

Can you try with unsigned long?

@askraskr
Copy link
Author

askraskr commented May 19, 2024

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?

@Ranj8521Kumar
Copy link

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

@nlohmann
Copy link
Owner

Go ahead!

@nlohmann
Copy link
Owner

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 nlohmann added the state: needs more info the author of the issue needs to provide more details label Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug state: needs more info the author of the issue needs to provide more details
Projects
None yet
Development

No branches or pull requests

3 participants