Skip to content

Commit 2af3e68

Browse files
authored
Merge pull request #8 from adhocore/develop
Develop
2 parents 7a90fb2 + 7e12597 commit 2af3e68

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/Comment.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,24 @@ protected function hasCommentEnded($char, $charnext)
8888
*
8989
* @see http://php.net/json_decode [JSON decode native function]
9090
*
91+
* @throws \RuntimeException When decode fails.
92+
*
9193
* @return mixed
9294
*/
9395
public function decode($json, $assoc = false, $depth = 512, $options = 0)
9496
{
95-
return \json_decode($this->strip($json), $assoc, $depth, $options);
97+
$decoded = \json_decode($this->strip($json), $assoc, $depth, $options);
98+
99+
if (\JSON_ERROR_NONE !== $err = \json_last_error()) {
100+
$msg = 'JSON decode failed';
101+
102+
if (\function_exists('json_last_error_msg')) {
103+
$msg .= ': ' . \json_last_error_msg();
104+
}
105+
106+
throw new \RuntimeException($msg, $err);
107+
}
108+
109+
return $decoded;
96110
}
97111
}

tests/CommentTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ public function testDecode($json)
2727
$this->assertArrayHasKey('b', $actual);
2828
}
2929

30+
/**
31+
* @expectedException \RuntimeException
32+
* @expectedExceptionMessage JSON decode failed
33+
*/
34+
public function testDecodeThrows()
35+
{
36+
(new Comment)->decode('{"a":1, /* comment */, "b":}', true);
37+
}
38+
3039
public function theTests()
3140
{
3241
return [

0 commit comments

Comments
 (0)