merged branch scoolen/json-decode-params (PR #4283)
Commits -------38cbbe7
Moved JSON encoding and decoding to separate classes which expose all their available parameters Discussion ---------- [Serializer][JsonEncoder] Exposed json_encode and json_decode params In `JsonEncoder::decode()` you are unable to change the `$assoc` parameter from `json_decode`. I created two sub-classes that handle JSON encoding and decoding while exposing all the available parameters from `json_encode` and `json_decode`. You can now do this: ```php $jsonEncoder = new JsonEncoder(new JsonEncode(JSON_HEX_TAG), new JsonDecode(true, 1024)); $serializer = new Serializer(array(), array($jsonEncoder)); ``` Additionally I made `json_last_error()` available from `JsonEncoder`: ```php $jsonEncoder->getLastEncodingError(); $jsonEncoder->getLastDecodingError(); ``` Bug fix: no Feature addition: yes Backwards compatibility break: no Symfony2 tests pass: yes Fixes the following tickets: N/A Todo: - License of the code: MIT Documentation PR: - --------------------------------------------------------------------------- by travisbot at 2012-05-14T18:46:16Z This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1329496) (merged38cbbe71
into46ffbd52
). --------------------------------------------------------------------------- by fabpot at 2012-05-15T05:07:04Z ping @Seldaek / @lsmith77 --------------------------------------------------------------------------- by Seldaek at 2012-05-15T09:47:48Z This looks fine to me, I asked him to submit the PR, but I wanted to get feedback from others.
This commit is contained in:
commit
00108fbd62
72
src/Symfony/Component/Serializer/Encoder/JsonDecode.php
Normal file
72
src/Symfony/Component/Serializer/Encoder/JsonDecode.php
Normal file
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Serializer\Encoder;
|
||||
|
||||
/**
|
||||
* Decodes JSON data
|
||||
*
|
||||
* @author Sander Coolen <sander@jibber.nl>
|
||||
*/
|
||||
class JsonDecode implements DecoderInterface
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $associative = false;
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $recursionDepth = 512;
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $lastError = JSON_ERROR_NONE;
|
||||
|
||||
public function __construct($assoc = false, $depth = 512)
|
||||
{
|
||||
$this->associative = $assoc;
|
||||
$this->recursionDepth = $depth;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last decoding error (if any)
|
||||
*
|
||||
* @return int
|
||||
* @see http://php.net/manual/en/function.json-last-error.php json_last_error
|
||||
*/
|
||||
public function getLastError()
|
||||
{
|
||||
return $this->lastError;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes a JSON string into PHP data
|
||||
*
|
||||
* @param string $data JSON
|
||||
* @return mixed
|
||||
*/
|
||||
public function decode($data, $format)
|
||||
{
|
||||
$decodedData = json_decode($data, $this->associative, $this->recursionDepth);
|
||||
$this->lastError = json_last_error();
|
||||
|
||||
return $decodedData;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsDecoding($format)
|
||||
{
|
||||
return JsonEncoder::FORMAT === $format;
|
||||
}
|
||||
}
|
67
src/Symfony/Component/Serializer/Encoder/JsonEncode.php
Normal file
67
src/Symfony/Component/Serializer/Encoder/JsonEncode.php
Normal file
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Serializer\Encoder;
|
||||
|
||||
/**
|
||||
* Encodes JSON data
|
||||
*
|
||||
* @author Sander Coolen <sander@jibber.nl>
|
||||
*/
|
||||
class JsonEncode implements EncoderInterface
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $options = 0;
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $lastError = JSON_ERROR_NONE;
|
||||
|
||||
public function __construct($bitmask = 0)
|
||||
{
|
||||
$this->options = $bitmask;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last encoding error (if any)
|
||||
*
|
||||
* @return int
|
||||
* @see http://php.net/manual/en/function.json-last-error.php json_last_error
|
||||
*/
|
||||
public function getLastError()
|
||||
{
|
||||
return $this->lastError;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes PHP data to a JSON string
|
||||
*
|
||||
* @param mixed $data
|
||||
* @return string
|
||||
*/
|
||||
public function encode($data, $format)
|
||||
{
|
||||
$encodedJson = json_encode($data, $this->options);
|
||||
$this->lastError = json_last_error();
|
||||
|
||||
return $encodedJson;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsEncoding($format)
|
||||
{
|
||||
return JsonEncoder::FORMAT === $format;
|
||||
}
|
||||
}
|
@ -18,12 +18,55 @@ namespace Symfony\Component\Serializer\Encoder;
|
||||
*/
|
||||
class JsonEncoder implements EncoderInterface, DecoderInterface
|
||||
{
|
||||
const FORMAT = 'json';
|
||||
|
||||
/**
|
||||
* @var JsonEncode
|
||||
*/
|
||||
protected $encodingImpl;
|
||||
/**
|
||||
* @var JsonDecode
|
||||
*/
|
||||
protected $decodingImpl;
|
||||
|
||||
public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodingImpl = null)
|
||||
{
|
||||
if (null === $encodingImpl) {
|
||||
$encodingImpl = new JsonEncode;
|
||||
}
|
||||
$this->encodingImpl = $encodingImpl;
|
||||
if (null === $decodingImpl) {
|
||||
$decodingImpl = new JsonDecode(true);
|
||||
}
|
||||
$this->decodingImpl = $decodingImpl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last encoding error (if any)
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getLastEncodingError()
|
||||
{
|
||||
return $this->encodingImpl->getLastError();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last decoding error (if any)
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getLastDecodingError()
|
||||
{
|
||||
return $this->decodingImpl->getLastError();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function encode($data, $format)
|
||||
{
|
||||
return json_encode($data);
|
||||
return $this->encodingImpl->encode($data, self::FORMAT);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -31,28 +74,22 @@ class JsonEncoder implements EncoderInterface, DecoderInterface
|
||||
*/
|
||||
public function decode($data, $format)
|
||||
{
|
||||
return json_decode($data, true);
|
||||
return $this->decodingImpl->decode($data, self::FORMAT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the serializer can encode to given format
|
||||
*
|
||||
* @param string $format format name
|
||||
* @return Boolean
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsEncoding($format)
|
||||
{
|
||||
return 'json' === $format;
|
||||
return self::FORMAT === $format;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the serializer can decode from given format
|
||||
*
|
||||
* @param string $format format name
|
||||
* @return Boolean
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsDecoding($format)
|
||||
{
|
||||
return 'json' === $format;
|
||||
return self::FORMAT === $format;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user