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

Invalid parsed value for big integer #1548

Closed
ammarfaizi2 opened this issue Mar 26, 2019 · 2 comments
Closed

Invalid parsed value for big integer #1548

ammarfaizi2 opened this issue Mar 26, 2019 · 2 comments
Labels
kind: bug solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@ammarfaizi2
Copy link

What is the issue you have?

I have a JSON string like this

{
    "message": {
        "chat": {
            "id": -1001128970273,
            "title": "Private Cloud",
            "type": "supergroup"
        }
    }
}

Then, I want to get the ["message"]["chat"]["id"], but cannot get the correct value.

Please describe the steps to reproduce the issue. Can you provide a small but working code example?

Example code

#include <cstdio>
#include "include/json.hpp"

using json = nlohmann::json;

int main() {
	json me = json::parse("{\"message\":{\"chat\":{\"id\":-1001128970273,\"title\":\"Private Cloud\",\"type\":\"supergroup\"}}}");

	int me_int = me["message"]["chat"].value("id", 0);
	long me_long = me["message"]["chat"].value("id", 0);
	long long me_long_long = me["message"]["chat"].value("id", 0);

	printf("Int: %d\n", me_int);
	printf("Long: %ld\n", me_long);
	printf("Long long: %lld\n", me_long_long);
}

What is the actual behavior?

Program output:

ammarfaizi2@CraynerCenter:~/project/now/bot-s6$ g++ test.cpp -o test
ammarfaizi2@CraynerCenter:~/project/now/bot-s6$ ./test
Int: -401590305
Long: -401590305
Long long: -401590305

What is the expected behavior?

Expected result

How can I get the -1001128970273 value?

Which compiler and operating system are you using? Is it a supported compiler?

Compiler Detail:

ammarfaizi2@CraynerCenter:~/project/now/bot-s6/build$ g++ --version
g++ (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

OS Detail:

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Linux Lite 4.0"
NAME="Ubuntu"
VERSION="18.04.2 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.2 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic

Did you use a released version of the library or the version from the develop branch?

I used the released version (downloaded from: https://github.com/nlohmann/json/releases/download/v3.6.1/json.hpp)

@nlohmann
Copy link
Owner

The problem seems to be that the literal 0 is used to derive the return type of value. This is the overload:

template<class ValueType, typename std::enable_if<
             std::is_convertible<basic_json_t, ValueType>::value, int>::type = 0>
ValueType value(const typename object_t::key_type& key, const ValueType& default_value)

In case for 0, the return type is int all the time. If you change the calls to 0L or 0LL the result is -1001128970273.

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Mar 26, 2019
@ammarfaizi2
Copy link
Author

@nlohmann Thank you for your assistance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants