Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Merge pull request #4534 from gwis/feature/jsonserializable-polyfill
Browse files Browse the repository at this point in the history
Introduce JsonSerializable polyfill and support in Zend\Json\Encoder
  • Loading branch information
weierophinney committed May 23, 2013
2 parents 7a59d3e + ff34a09 commit cd85bc6
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 1 deletion.
5 changes: 5 additions & 0 deletions library/Zend/Json/Encoder.php
Expand Up @@ -11,6 +11,7 @@

use Iterator;
use IteratorAggregate;
use JsonSerializable;
use ReflectionClass;
use Zend\Json\Exception\InvalidArgumentException;
use Zend\Json\Exception\RecursionException;
Expand Down Expand Up @@ -66,6 +67,10 @@ public static function encode($value, $cycleCheck = false, $options = array())
{
$encoder = new static(($cycleCheck) ? true : false, $options);

if ($value instanceof JsonSerializable) {
$value = $value->jsonSerialize();
}

return $encoder->_encodeValue($value);
}

Expand Down
28 changes: 28 additions & 0 deletions library/Zend/Stdlib/JsonSerializable.php
@@ -0,0 +1,28 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Stdlib;

if (version_compare(PHP_VERSION, '5.4.0', 'lt')) {
class_alias(
'Zend\Stdlib\JsonSerializable\PhpLegacyCompatibility',
'JsonSerializable'
);
}

/**
* Polyfill for JsonSerializable
*
* JsonSerializable was introduced in PHP 5.4.0.
*
* @see http://php.net/manual/class.jsonserializable.php
*/
interface JsonSerializable extends \JsonSerializable
{
}
28 changes: 28 additions & 0 deletions library/Zend/Stdlib/JsonSerializable/PhpLegacyCompatibility.php
@@ -0,0 +1,28 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Stdlib\JsonSerializable;

/**
* Interface compatible with the built-in JsonSerializable interface
*
* JsonSerializable was introduced in PHP 5.4.0.
*
* @see http://php.net/manual/class.jsonserializable.php
*/
interface PhpLegacyCompatibility
{
/**
* Returns data which can be serialized by json_encode().
*
* @return mixed
* @see http://php.net/manual/jsonserializable.jsonserialize.php
*/
public function jsonSerialize();
}
24 changes: 23 additions & 1 deletion tests/ZendTest/Json/JsonTest.php
Expand Up @@ -463,7 +463,29 @@ public function testToJSONSerialization()
$this->assertEquals('{"firstName":"John","lastName":"Doe","email":"john@doe.com"}', $result);
}

/**
public function testJsonSerializableWithBuiltinImplementation()
{
if (version_compare(PHP_VERSION, '5.4.0', 'lt')) {
$this->markTestSkipped('JsonSerializable does not exist in PHP <5.4.0.');
}

$encoded = Json\Encoder::encode(
new TestAsset\JsonSerializableBuiltinImpl()
);

$this->assertEquals('["jsonSerialize"]', $encoded);
}

public function testJsonSerializableWithZFImplementation()
{
$encoded = Json\Encoder::encode(
new TestAsset\JsonSerializableZFImpl()
);

$this->assertEquals('["jsonSerialize"]', $encoded);
}

/**
* test encoding array with Zend_JSON_Expr
*
* @group ZF-4946
Expand Down
26 changes: 26 additions & 0 deletions tests/ZendTest/Json/TestAsset/JsonSerializableBuiltinImpl.php
@@ -0,0 +1,26 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Json
*/

namespace ZendTest\Json\TestAsset;

use JsonSerializable;

/**
* Implementation of the built-in JsonSerializable interface.
*
* This asset will not work in PHP <5.4.0.
*/
class JsonSerializableBuiltinImpl implements JsonSerializable
{
public function jsonSerialize()
{
return array(__FUNCTION__);
}
}
21 changes: 21 additions & 0 deletions tests/ZendTest/Json/TestAsset/JsonSerializableZFImpl.php
@@ -0,0 +1,21 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Json
*/

namespace ZendTest\Json\TestAsset;

use Zend\Stdlib\JsonSerializable;

class JsonSerializableZFImpl implements JsonSerializable
{
public function jsonSerialize()
{
return array(__FUNCTION__);
}
}

0 comments on commit cd85bc6

Please sign in to comment.