diff --git a/src/json.c b/src/json.c index 042ea9b51..4da22fe73 100644 --- a/src/json.c +++ b/src/json.c @@ -564,21 +564,22 @@ lyjson_exp_number(const struct ly_ctx *ctx, const char *in, const char *exponent /* Set a new range of 'numeric part'. Old decimal point is skipped. */ num++; num_len--; - dp_position--; /* Get the number of useless zeros between the old * and new decimal point. For example, in the number 0.005E1, * there is one useless zero. */ - zeros = lyjson_count_in_row(num, num + dp_position + 1, '0', FORWARD); + zeros = lyjson_count_in_row(num, num + dp_position, '0', FORWARD); /* If the new decimal point will be in the place of the first non-zero subnumber. */ - if (zeros == (dp_position + 1)) { + if (zeros == dp_position) { /* keep one zero as leading zero */ zeros--; /* new decimal point will be behind the leading zero */ dp_position = 1; dot = 1; } else { - dot = 0; + /* the useless zeros are skipped, no decimal point if there are no digits behind it */ + dp_position -= zeros; + dot = (dp_position < (int32_t)(num_len - zeros)) ? 1 : 0; } buf_len = minus + dot + (num_len - zeros); LY_CHECK_RET(lyjson_get_buffer_for_number(ctx, buf_len, &buf)); diff --git a/tests/utests/basic/test_json.c b/tests/utests/basic/test_json.c index f50ac0498..f2c629bae 100644 --- a/tests/utests/basic/test_json.c +++ b/tests/utests/basic/test_json.c @@ -271,6 +271,27 @@ test_number(void **state) lyd_free_all(tree); tree = NULL; + str = "0.25E2"; + assert_non_null(ly_in_memory(in, str)); + assert_int_equal(LY_SUCCESS, lyd_parse_value_fragment(ctx, "/test:num", in, LYD_JSON, 0, 0, 0, &tree)); + assert_string_equal("25", lyd_get_value(tree)); + lyd_free_all(tree); + tree = NULL; + + str = "-0.62098E5"; + assert_non_null(ly_in_memory(in, str)); + assert_int_equal(LY_SUCCESS, lyd_parse_value_fragment(ctx, "/test:num", in, LYD_JSON, 0, 0, 0, &tree)); + assert_string_equal("-62098", lyd_get_value(tree)); + lyd_free_all(tree); + tree = NULL; + + str = "0.0034E3"; + assert_non_null(ly_in_memory(in, str)); + assert_int_equal(LY_EVALID, lyd_parse_value_fragment(ctx, "/test:num", in, LYD_JSON, 0, 0, 0, &tree)); + CHECK_LOG_CTX("Invalid union value \"3.4\" - no matching subtype found:\n" + " ly2 integers: Invalid type int32 value \"3.4\".\n" + " ly2 decimal64: Invalid non-string-encoded decimal64 value \"3.4\".\n", "/test:num", 1); + /* decimal number, negative exponent */ str = "\"35.94e-1\""; assert_non_null(ly_in_memory(in, str));