Skip to content

Commit

Permalink
Merge pull request #18 from zumba/fix-float-i18n
Browse files Browse the repository at this point in the history
Fixing float serialization on i18n locales
  • Loading branch information
cjsaylor committed Mar 17, 2016
2 parents d1a3d7f + 0cb3698 commit 6bde2e7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Util/JsonSerializer.php
Expand Up @@ -127,7 +127,7 @@ public function unserialize($value)
protected function serializeData($value)
{
if (is_scalar($value) || $value === null) {
if (!$this->preserveZeroFractionSupport && is_float($value) && strpos((string)$value, '.') === false) {
if (!$this->preserveZeroFractionSupport && is_float($value) && ctype_digit((string)$value)) {
// Because the PHP bug #50224, the float numbers with no
// precision numbers are converted to integers when encoded
$value = static::FLOAT_ADAPTER . '(' . $value . '.0)';
Expand Down
19 changes: 19 additions & 0 deletions tests/JsonSerializerTest.php
Expand Up @@ -40,6 +40,25 @@ public function testSerializeScalar($scalar, $jsoned)
$this->assertSame($jsoned, $this->serializer->serialize($scalar));
}

/**
* Test serialization of float values with locale
*
* @return void
*/
public function testSerializeFloatLocalized()
{
$possibleLocales = ['fr_FR', 'fr_FR.utf8', 'fr', 'fra', 'French'];
$originalLocale = setlocale(LC_NUMERIC, 0);
if (!setlocale(LC_NUMERIC, $possibleLocales)) {
$this->markTestSkipped('Unable to set an i18n locale.');
}

$data = [1.0, 1.1, 0.00000000001, 1.999999999999, 223423.123456789, 1e5, 1e11];
$expected = '[1.0,1.1,1.0e-11,1.999999999999,223423.12345679,100000.0,100000000000.0]';
$this->assertSame($expected, $this->serializer->serialize($data));

setlocale(LC_NUMERIC, $originalLocale);
}
/**
* Test unserialization of scalar values
*
Expand Down

0 comments on commit 6bde2e7

Please sign in to comment.