-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Comments
Hello I hope you're well, I am new but also very much ready to learn. I would like to help fix this bug |
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. |
@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? |
Your test example fails to build with Microsoft C++ (via vsCode). The
folder 'json' is the top level of a fresh clone of your GitHub project.
Here is the console listing:
cmd /c chcp 65001>nul && cl.exe /Zi /EHsc /nologo
/IC:/Users/tksha/Projects/lohmann/json/include
/FeC:\Users\tksha\Projects\lohmann\lohmann-zero.exe
C:\Users\tksha\Projects\lohmann\lohmann-zero.cpp
lohmann-zero.cpp
C:\Users\tksha\Projects\lohmann\lohmann-zero.cpp(4): error C2653: 'json':
is not a class or namespace name
C:\Users\tksha\Projects\lohmann\lohmann-zero.cpp(4): error C3861: 'parse':
identifier not found
Build finished with error(s).
…On Fri, Nov 29, 2024 at 11:29 AM Niels Lohmann ***@***.***> wrote:
@tksharpless <https://github.com/tksharpless> @arifghaz
<https://github.com/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?
—
Reply to this email directly, view it on GitHub
<#4253 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACBEZZVQQ5N2DZJDVADUQEL2DCI5VAVCNFSM6AAAAABSXL4TR2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDKMBYGEYTMNBVGI>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
You need to set up the library search path to resolve |
I did that. The compiler is seeing json.hpp.
…On Sat, Nov 30, 2024 at 9:40 AM Niels Lohmann ***@***.***> wrote:
You need to set up the library search path to resolve <nlohmann/json.hpp>.
—
Reply to this email directly, view it on GitHub
<#4253 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACBEZZT6OFWH6FAYMTUJ5JT2DHE6BAVCNFSM6AAAAABSXL4TR2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDKMBYHE4DENJRG4>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
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
develop
branch is used.The text was updated successfully, but these errors were encountered: