-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
is_number_unsigned() returns false for positive integers (int or 0 or 1 literals) #1885
Comments
Indeed I am afraid the behavior will stay as is, because changing it would break existing implementations. Any proposal to fix the documentation? |
Is that a typo? Did you mean |
IMHO the docs may say exactly that:
BTW what I wonder now is how |
- Do not rely on is_number_unsigned() of json C++ lib, which is unreliable due to its design. See: nlohmann/json#1885
Yes. Sorry. I edited the comment to avoid further confusion. |
The distinction into signed and unsigned numbers is done to accept a broader range of integer types during parsing. The check function was introduced in v2.0.0 when unsigned numbers were introduced. The check functions can then be used to postprocess the parsed values. I do not share your assessment that it is unreliable. Please let me know if there is anything left to do in this issue. |
The problem I see is the following: auto json = R"(
{
"foo": 0,
"bar": 1,
})"_json;
The answer is If later I do: unsigned int foo2 = 0;
unsigned int bar2 = 1;
json["foo2"] = foo2;
json["bar2"] = bar2; then, both However, if I do: json["foo3"] = 0;
json["bar2"] = 1; then, both This is, depending on how values are inserted into the JSON object, we get different results when checking those values once in the JSON. I understand that this json lib stores the "native" type into each JSON value. I just wonder why. Once values are inserted into a JSON object, which the native type was should not be relevant at all IMHO. So an API that exposes which the native type was (such as |
Exactly. The function does not take the value, but the stored type into account.
The reason is that the library should be able to store both negative and very large integer values without falling back to floating-point numbers. Thereby, we have the range
If you store the value manually (i.e., not during parsing), then you have the control of which type is used - just as in your example: json j_signed = int(1);
json j_unsigned = unsigned(1); |
Thanks. The problem I see is: how to know which type will be assigned to a number during parsing? i.e.: auto json = R"(
{
"foo": 0,
"bar": 1,
"baz": -1
})"_json;
|
Well, essentially by calling The parser will use This, however, should be an implementation detail. You can always case to your preferred type. |
Clear, thanks. Let's close this. |
JSON fields whose value are assigned as a
int
orintN_t
or literals withoutu
(such as0
or1
) makeis_number_unsigned()
to returnfalse
.I create a C++ JSON object and fill a field with 0 or 1 as follows:
Then I check
foo
as follows:I want to assume that
ret
istrue
sincejson.foo
is0
or1
.ret
isfalse
, probably becausejson.foo
holds a "signed" number (even if it's zero or a positive number).develop
branch?The latest 2.11.1. It happens with previous versions too.
More
I really expected that
is_unsigned_number()
would check the value of the element and not just the value type.The text was updated successfully, but these errors were encountered: