-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved JSON encoding and decoding to separate classes which expose all…
… their available parameters
- Loading branch information
Showing
3 changed files
with
189 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters