diff --git a/src/Stringy.php b/src/Stringy.php index 1008d1c..97c6a6d 100644 --- a/src/Stringy.php +++ b/src/Stringy.php @@ -1199,11 +1199,9 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess */ public function isJson() { - if (!$this->endsWith('}') && !$this->endsWith(']')) { - return false; - } + json_decode($this->str); - return !is_null(json_decode($this->str)); + return (json_last_error() === JSON_ERROR_NONE); } diff --git a/tests/CommonTest.php b/tests/CommonTest.php index 1258c1f..b4285db 100644 --- a/tests/CommonTest.php +++ b/tests/CommonTest.php @@ -730,14 +730,14 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase public function isJsonProvider() { return array( - array(false, ''), - array(false, '123'), + array(true, ''), + array(true, '123'), array(true, '{"foo": "bar"}'), array(false, '{"foo":"bar",}'), array(false, '{"foo"}'), array(true, '["foo"]'), array(false, '{"foo": "bar"]'), - array(false, '123', 'UTF-8'), + array(true, '123', 'UTF-8'), array(true, '{"fòô": "bàř"}', 'UTF-8'), array(false, '{"fòô":"bàř",}', 'UTF-8'), array(false, '{"fòô"}', 'UTF-8'),