Skip to content

Commit

Permalink
Moved JSON encoding and decoding to separate classes which expose all…
Browse files Browse the repository at this point in the history
… their available parameters
  • Loading branch information
scoolen committed May 14, 2012
1 parent 46ffbd5 commit 38cbbe7
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 13 deletions.
72 changes: 72 additions & 0 deletions src/Symfony/Component/Serializer/Encoder/JsonDecode.php
Original file line number Diff line number Diff line change
@@ -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 changes: 67 additions & 0 deletions src/Symfony/Component/Serializer/Encoder/JsonEncode.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
63 changes: 50 additions & 13 deletions src/Symfony/Component/Serializer/Encoder/JsonEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,78 @@
*/
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);
}

/**
* {@inheritdoc}
*/
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;
}
}
}

0 comments on commit 38cbbe7

Please sign in to comment.