Skip to content

Commit 092a3c4

Browse files
authored
Merge pull request #5173 from Kova101/fix-json-float-zero-preserve
Fix JSON Dbal type to properly encode whole number float values, preserving zero fractions.
2 parents 66bcb0b + 2f5661e commit 092a3c4

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/Types/JsonType.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use function json_encode;
1111
use function stream_get_contents;
1212

13+
use const JSON_PRESERVE_ZERO_FRACTION;
1314
use const JSON_THROW_ON_ERROR;
1415

1516
/**
@@ -35,7 +36,7 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform)
3536
}
3637

3738
try {
38-
return json_encode($value, JSON_THROW_ON_ERROR);
39+
return json_encode($value, JSON_THROW_ON_ERROR | JSON_PRESERVE_ZERO_FRACTION);
3940
} catch (JsonException $e) {
4041
throw ConversionException::conversionFailedSerialization($value, 'json', $e->getMessage(), $e);
4142
}

tests/Types/JsonTest.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use function fopen;
1515
use function json_encode;
1616

17+
use const JSON_PRESERVE_ZERO_FRACTION;
1718
use const JSON_THROW_ON_ERROR;
1819

1920
class JsonTest extends TestCase
@@ -62,7 +63,7 @@ public function testJsonEmptyStringConvertsToPHPValue(): void
6263
public function testJsonStringConvertsToPHPValue(): void
6364
{
6465
$value = ['foo' => 'bar', 'bar' => 'foo'];
65-
$databaseValue = json_encode($value, 0, JSON_THROW_ON_ERROR);
66+
$databaseValue = json_encode($value, 0, JSON_THROW_ON_ERROR | JSON_PRESERVE_ZERO_FRACTION);
6667
$phpValue = $this->type->convertToPHPValue($databaseValue, $this->platform);
6768

6869
self::assertEquals($value, $phpValue);
@@ -87,7 +88,10 @@ public function testJsonResourceConvertsToPHPValue(): void
8788
{
8889
$value = ['foo' => 'bar', 'bar' => 'foo'];
8990
$databaseValue = fopen(
90-
'data://text/plain;base64,' . base64_encode(json_encode($value, JSON_THROW_ON_ERROR)),
91+
'data://text/plain;base64,' . base64_encode(json_encode(
92+
$value,
93+
JSON_THROW_ON_ERROR | JSON_PRESERVE_ZERO_FRACTION
94+
)),
9195
'r'
9296
);
9397
$phpValue = $this->type->convertToPHPValue($databaseValue, $this->platform);
@@ -100,6 +104,27 @@ public function testRequiresSQLCommentHint(): void
100104
self::assertTrue($this->type->requiresSQLCommentHint($this->platform));
101105
}
102106

107+
public function testPHPNullValueConvertsToJsonNull(): void
108+
{
109+
self::assertNull($this->type->convertToDatabaseValue(null, $this->platform));
110+
}
111+
112+
public function testPHPValueConvertsToJsonString(): void
113+
{
114+
$source = ['foo' => 'bar', 'bar' => 'foo'];
115+
$databaseValue = $this->type->convertToDatabaseValue($source, $this->platform);
116+
117+
self::assertSame('{"foo":"bar","bar":"foo"}', $databaseValue);
118+
}
119+
120+
public function testPHPFloatValueConvertsToJsonString(): void
121+
{
122+
$source = ['foo' => 11.4, 'bar' => 10.0];
123+
$databaseValue = $this->type->convertToDatabaseValue($source, $this->platform);
124+
125+
self::assertSame('{"foo":11.4,"bar":10.0}', $databaseValue);
126+
}
127+
103128
public function testSerializationFailure(): void
104129
{
105130
$object = (object) [];

0 commit comments

Comments
 (0)