Skip to content

Commit b60ee03

Browse files
marmichalskistaabm
authored andcommitted
Skip json_last_error check when JSON_THROW_ON_ERROR flag is set in json_decode
1 parent 4b73aa4 commit b60ee03

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/special_cases.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
function json_decode(string $json, bool $assoc = false, int $depth = 512, int $flags = 0): mixed
3838
{
3939
$data = \json_decode($json, $assoc, $depth, $flags);
40-
if (JSON_ERROR_NONE !== json_last_error()) {
40+
if (!($flags & JSON_THROW_ON_ERROR) && JSON_ERROR_NONE !== json_last_error()) {
4141
throw JsonException::createFromPhpError();
4242
}
4343
return $data;

tests/JsonDecodeTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class JsonDecodeTest extends TestCase
8+
{
9+
public function testJsonLastErrorIsNotCheckedIfFlagIsSet(): void
10+
{
11+
\json_decode('{');
12+
self::assertNotSame(JSON_ERROR_NONE, \json_last_error());
13+
14+
\Safe\json_decode('[]', flags: \JSON_THROW_ON_ERROR);
15+
}
16+
}

0 commit comments

Comments
 (0)