Crell / Serde

Robust Serde (serialization/deserialization) library for PHP 8.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error deserializing whole-number float values

Physikbuddha opened this issue · comments

Imagine the following class:

class Foo {
    public float $floatNumber;
}

This works just fine:

$foo = new Foo();
$foo->floatNumber = 1234.5;
$json = $this->serde->serialize($foo, 'json'); // Value reads {"floatNumber":1234.5}
$bar = $this->serde->deserialize($json, 'json', Foo::class);

However, using a (valid) whole-number float causes the Deserializer to throw an exception:

$foo = new Foo();
$foo->floatNumber = 1234.0;
$json = $this->serde->serialize($foo, 'json'); // Value reads {"floatNumber":1234}
$bar = $this->serde->deserialize($json, 'json', Foo::class);

Crell\Serde\TypeMismatch
Expected value of type float when writing to property floatNumber, but found type int.
vendor\crell\serde\src\TypeMismatch.php line 16

Hm, yep, that should be allowed. I'm still not 100% certain if the type checking is done right in the system, but I've fixed it for now in 71f84c3.

I can confirm that it now works.