Skip to content

Commit 05b8cca

Browse files
committed
better can be decoded check
1 parent 1d74b35 commit 05b8cca

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/Json.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ private static function assertCanEncode($value) : void
3030
}
3131
}
3232

33-
private static function assertCanDecode($value) : void
33+
private static function assertCanDecode(string $value) : void
3434
{
35+
$value = \trim($value);
36+
3537
$ends = \mb_substr($value, 0, 1) . \mb_substr($value, -1);
3638

3739
if (! \in_array($ends, ['[]', '{}'], true)) {

tests/JsonDecodeAssocTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,28 @@ public function decode_assoc_decodes_key_value_object_as_assoc_array() : void
4141

4242
$this->assertEquals($expectedOutput, $output);
4343
}
44+
45+
/** @test */
46+
public function string_starting_or_ending_with_whitespace_can_be_decoded() : void
47+
{
48+
$input = '
49+
{
50+
"first": 1,
51+
"second": 2,
52+
"third": 3
53+
}
54+
';
55+
56+
$output = Json::decodeAssoc($input);
57+
58+
$this->assertIsArray($output);
59+
60+
$expectedOutput = [
61+
'first' => 1,
62+
'second' => 2,
63+
'third' => 3,
64+
];
65+
66+
$this->assertEquals($expectedOutput, $output);
67+
}
4468
}

0 commit comments

Comments
 (0)