You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
I just ran into this foot-gun; it's kinda strange, although your explanation makes sense it is extremely unintuitive from a C perspective. 9223372036854775807 is not sint, is uint but is also int - however get_int implicitly down-casts it to int32. I think what tripped me up was that the API is not symmetrical with the mut API.
I don't know how open you are to API changes, but honestly I think the integer stuff (at least on the immutable side) needs a bit of a re-work or at least fixes to the documentation, because nobody that reads it or the source of the getters/is_ functions will understand that "sint" does not actually mean what we think of as a signed integer, it means negative integer.
I ended up just replacing the integral API entirely with ones that return fixed types and ensure they are in range of their respective types, so that the end user does not need to think about the distinction (ie, yyjson_is_int64 will always be true if the integer stored in the val is representable is int64, and yyjson_get_int16 will always return an int16 but will clamp it to the representable range just in case somebody calls it without type-checking first; I guess you could use an arg to determine whether you want it to overflow or wrap).
Describe the bug
yyjson_is_sint()
returns false for positive integersYour environment
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.The text was updated successfully, but these errors were encountered: