Skip to content

Commit

Permalink
Fix incorrect parse of integer values for double vars (#1024)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
soloth authored Jun 27, 2024
1 parent 63e7833 commit 49a1bf4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion runtime/from-json-processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 23 additions & 0 deletions tests/phpt/json/37_json_float_read.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
require_once 'kphp_tester_include.php';

class MoneyRequest {
private float $amount;

public function __construct(float $amount) {
$this->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();

0 comments on commit 49a1bf4

Please sign in to comment.