From 9399f7a6943ea1ed76d4e3b261310e904e5aa346 Mon Sep 17 00:00:00 2001 From: "Daniel St. Jules" Date: Thu, 13 Feb 2014 06:32:54 -0500 Subject: [PATCH] Fix isJSON Thanks to Drainedsoul --- src/Stringy.php | 6 ++---- tests/CommonTest.php | 6 +++--- 2 files changed, 5 insertions(+), 7 deletions(-) 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'),