diff --git a/library/Zend/Json.php b/library/Zend/Json.php index ad72eb5303..78c9ce0117 100644 --- a/library/Zend/Json.php +++ b/library/Zend/Json.php @@ -73,6 +73,9 @@ class Zend_Json */ public static function decode($encodedValue, $objectDecodeType = Zend_Json::TYPE_ARRAY) { + if (in_array($encodedValue, array(null, ''))) { + return null; + } $encodedValue = (string) $encodedValue; if (function_exists('json_decode') && self::$useBuiltinEncoderDecoder !== true) { $decode = json_decode($encodedValue, $objectDecodeType); diff --git a/tests/Zend/JsonTest.php b/tests/Zend/JsonTest.php index 711bc65223..9916df0c74 100644 --- a/tests/Zend/JsonTest.php +++ b/tests/Zend/JsonTest.php @@ -922,6 +922,40 @@ public function testWillDecodeStructureWithEmptyKeyToObjectProperly() $this->assertEquals('test', $object->_empty_); } + /** + * Decoding null should return null + */ + public function testWillDecodeNullToNull() + { + $this->assertNull(Zend_Json::decode(null)); + } + + /** + * Decoding null should return null + */ + public function testWillDecodeWithBuiltinNullToNull() + { + Zend_Json::$useBuiltinEncoderDecoder = true; + $this->assertNull(Zend_Json::decode(null)); + } + + /** + * Decoding empty string should return null + */ + public function testWillDecodeEmptyStringToNull() + { + $this->assertNull(Zend_Json::decode('')); + } + + /** + * Decoding empty string should return null + */ + public function testWillDecodeWithBuiltinEmptyStringToNull() + { + Zend_Json::$useBuiltinEncoderDecoder = true; + $this->assertNull(Zend_Json::decode('')); + } + } /**