ibireme / yyjson

The fastest JSON library in C
https://ibireme.github.io/yyjson/doc/doxygen/html/
MIT License
3.11k stars 266 forks source link

yyjson_is_sint() returns false for positive integers #182

Open wridgers opened 1 month ago

wridgers commented 1 month ago

Describe the bug yyjson_is_sint() returns false for positive integers

Your environment

Additional context I assume this is related to https://github.com/ibireme/yyjson/issues/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 commented 1 month ago

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.