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

Parsing simple zeroes throws type exception #4253

Open
2 tasks
tksharpless opened this issue Dec 29, 2023 · 6 comments
Open
2 tasks

Parsing simple zeroes throws type exception #4253

tksharpless opened this issue Dec 29, 2023 · 6 comments
Labels
kind: bug state: needs more info the author of the issue needs to provide more details

Comments

@tksharpless
Copy link

Description

Not strictly a bug, but not ideal. Many apps print a simple '0' for all zero numbers, regardless of type. nlohmann-json parses those as unsigned int and throws an exception, for example in value(var, 0.0). There should be an option to promote 0 to float in this case; perhaps even for other numbers that look like an int. Or maybe there is such an option and I don't know it?

Reproduction steps

parse a Json file (not written by nlohmann, of course) that contains simple integer strings as values that you know should be float, using value(var, 0.0).

Expected vs. actual results

expect automatic promotion to double or whatever I assign the value to
get an exception complaining of incompatible types

Minimal code example

No response

Error messages

No response

Compiler and operating system

MSVCC 2022 Win10

Library version

3.9.1

Validation

@ProlificCoder17
Copy link

Hello I hope you're well, I am new but also very much ready to learn. I would like to help fix this bug

@arifghaz
Copy link

Hello, I have take some look at this issue and it seems to me that I can fix this by creating a special value function for integer/float. But, considering that this issue can potentially be handled by the user themselves during parsing, is there a need to implement the function to specifically caters for this one specific scenario.

@nlohmann
Copy link
Owner

@tksharpless @arifghaz I currently fail to reproduce the issue - I seem to not understand it. Here is a code example:

#include <nlohmann/json.hpp>

using json = nlohmann::json;

int main() {
	auto j = json::parse("{\"zero\": 0}"); // parse a 0, store it as unsigned int
	auto x = j.value("zero", 0.0);         // call the value function with 0.0 as default
}

Can you clarify?

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Nov 29, 2024
@tksharpless
Copy link
Author

tksharpless commented Nov 30, 2024 via email

@nlohmann
Copy link
Owner

You need to set up the library search path to resolve <nlohmann/json.hpp>.

@tksharpless
Copy link
Author

tksharpless commented Nov 30, 2024 via email

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

4 participants