From 49a1bf48f766af04add35054d49e716e9e4f7a73 Mon Sep 17 00:00:00 2001 From: Dmitry Solomennikov <144122424+soloth@users.noreply.github.com> Date: Thu, 27 Jun 2024 16:18:32 +0300 Subject: [PATCH] Fix incorrect parse of integer values for double vars (#1024) Values like `{"a": 101}` were parsed incorrectly during JSON parsing. They're internally interpreted as being integer and during assignment to double variable it were bit-casted from `int` to `double`, which is wrong. This PR fixes this behavior. Now integer values are converted to double explicitly. --- runtime/from-json-processor.h | 2 +- tests/phpt/json/37_json_float_read.php | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 tests/phpt/json/37_json_float_read.php diff --git a/runtime/from-json-processor.h b/runtime/from-json-processor.h index 3c54975c3a..49dd106529 100644 --- a/runtime/from-json-processor.h +++ b/runtime/from-json-processor.h @@ -79,7 +79,7 @@ class FromJsonVisitor { on_input_type_mismatch(json); return; } - value = json.as_double(); + value = json.is_int() ? json.as_int() : json.as_double(); } void do_set(string &value, const mixed &json) noexcept { diff --git a/tests/phpt/json/37_json_float_read.php b/tests/phpt/json/37_json_float_read.php new file mode 100644 index 0000000000..5c77de2f7f --- /dev/null +++ b/tests/phpt/json/37_json_float_read.php @@ -0,0 +1,23 @@ +amount = $amount; + } + + public function getAmount(): float { + return $this->amount; + } +} + +function test_json_float_read(): void { + $raw = '{"amount":101}'; + + $res = JsonEncoder::decode($raw, MoneyRequest::class); + var_dump($res->getAmount()); +} + +test_json_float_read();