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

yyjson_is_sint() returns false for positive integers #182

Open
wridgers opened this issue Sep 25, 2024 · 1 comment
Open

yyjson_is_sint() returns false for positive integers #182

wridgers opened this issue Sep 25, 2024 · 1 comment

Comments

@wridgers
Copy link

Describe the bug
yyjson_is_sint() returns false for positive integers

Your environment

  • OS: Gentoo
  • Compiler: gcc 13.3.1

Additional context
I assume this is related to #152.

I wrote a few quick tests:

     json = "123";
     doc = yyjson_read(json, strlen(json), 0);
     val = yyjson_doc_get_root(doc);
     yy_assert(validate_val_type(val, YYJSON_TYPE_NUM, YYJSON_SUBTYPE_UINT));
     yy_assert(strcmp(yyjson_get_type_desc(val), "uint") == 0);
     yy_assert(yyjson_get_uint(val) == (u64)123);
+    yy_assert(yyjson_is_sint(val));
     yy_assert(yyjson_get_sint(val) == (i64)123);
     yy_assert(yyjson_get_int(val) == (i64)123);
     yy_assert(yyjson_get_real(val) == (f64)0);
     yy_assert(yyjson_get_num(val) == (f64)123);
     yy_assert(yyjson_get_bool(val) == false);
     yyjson_doc_free(doc);
     json = "-123";
     doc = yyjson_read(json, strlen(json), 0);
     val = yyjson_doc_get_root(doc);
     yy_assert(validate_val_type(val, YYJSON_TYPE_NUM, YYJSON_SUBTYPE_SINT));
     yy_assert(strcmp(yyjson_get_type_desc(val), "sint") == 0);
     yy_assert(yyjson_get_uint(val) == (u64)-123);
+    yy_assert(yyjson_is_sint(val));
     yy_assert(yyjson_get_sint(val) == (i64)-123);
     yy_assert(yyjson_get_int(val) == (i64)-123);
     yy_assert(yyjson_get_real(val) == (f64)0);
     yy_assert(yyjson_get_num(val) == (f64)-123);
     yyjson_doc_free(doc);

The 123 variant fails, while the -123 variant passes.

@ibireme
Copy link
Owner

ibireme commented Sep 25, 2024

Positive integers are read as uint type, and negative integers are read as sint type. So is_sint(uint_val) returns false.
I recommend using yyjson_is_int(val) instead, which returns true for both uint and sint types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants