Skip to content

Commit 649a129

Browse files
committed
Json::encode() uses by default JSON_PRESERVE_ZERO_FRACTION (BC break) [Closes #89]
1 parent 584ea53 commit 649a129

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/Utils/Json.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ final public function __construct()
3636
*/
3737
public static function encode($value, $options = 0)
3838
{
39-
$flags = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | ($options & self::PRETTY ? JSON_PRETTY_PRINT : 0);
39+
$flags = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
40+
| ($options & self::PRETTY ? JSON_PRETTY_PRINT : 0)
41+
| (PHP_VERSION_ID >= 50606 ? JSON_PRESERVE_ZERO_FRACTION : 0);
4042

4143
$json = json_encode($value, $flags);
4244
if ($error = json_last_error()) {

tests/Utils/Json.encode().phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ Assert::same("[\n 1,\n 2,\n 3\n]", Json::encode([1, 2, 3], Json::PRETTY
3737
Assert::exception(function () {
3838
Json::encode(NAN);
3939
}, Nette\Utils\JsonException::class, 'Inf and NaN cannot be JSON encoded');
40+
41+
42+
// JSON_PRESERVE_ZERO_FRACTION
43+
Assert::same(PHP_VERSION_ID < 50606 ? '1' : '1.0', Json::encode(1.0));

0 commit comments

Comments
 (0)