From 6d9f0be0a0b362f1f0dc3484a3b0331693940159 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Di=CC=81ez=20Villamuera?= Date: Sat, 23 Nov 2013 10:35:55 +0100 Subject: [PATCH 1/2] Json encoder classes now throws UnexpectedValueException as XML classes --- src/Symfony/Component/Serializer/CHANGELOG.md | 2 ++ .../Serializer/Encoder/DecoderInterface.php | 4 +++ .../Serializer/Encoder/EncoderInterface.php | 4 +++ .../Serializer/Encoder/JsonDecode.php | 11 ++++++- .../Serializer/Encoder/JsonEncode.php | 11 +++++-- .../Serializer/Encoder/JsonEncoder.php | 33 +++++++++++++++++++ 6 files changed, 62 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Serializer/CHANGELOG.md b/src/Symfony/Component/Serializer/CHANGELOG.md index 5b859623fef0..66081baa0585 100644 --- a/src/Symfony/Component/Serializer/CHANGELOG.md +++ b/src/Symfony/Component/Serializer/CHANGELOG.md @@ -5,6 +5,8 @@ CHANGELOG ----- * added `$context` support for XMLEncoder. + * [DEPRECATION] JsonEncode and JsonDecode where modified to throw + an exception if error found. No need for get*Error() functions 2.3.0 ----- diff --git a/src/Symfony/Component/Serializer/Encoder/DecoderInterface.php b/src/Symfony/Component/Serializer/Encoder/DecoderInterface.php index 9dd336d5f785..9bd5fcbe1692 100644 --- a/src/Symfony/Component/Serializer/Encoder/DecoderInterface.php +++ b/src/Symfony/Component/Serializer/Encoder/DecoderInterface.php @@ -11,6 +11,8 @@ namespace Symfony\Component\Serializer\Encoder; +use Symfony\Component\Serializer\Exception\UnexpectedValueException; + /** * Defines the interface of decoders * @@ -31,6 +33,8 @@ interface DecoderInterface * phpdoc comment. * * @return mixed + * + * @throws UnexpectedValueException */ public function decode($data, $format, array $context = array()); diff --git a/src/Symfony/Component/Serializer/Encoder/EncoderInterface.php b/src/Symfony/Component/Serializer/Encoder/EncoderInterface.php index 2290db7f50d8..b928b165294b 100644 --- a/src/Symfony/Component/Serializer/Encoder/EncoderInterface.php +++ b/src/Symfony/Component/Serializer/Encoder/EncoderInterface.php @@ -11,6 +11,8 @@ namespace Symfony\Component\Serializer\Encoder; +use Symfony\Component\Serializer\Exception\UnexpectedValueException; + /** * Defines the interface of encoders * @@ -26,6 +28,8 @@ interface EncoderInterface * @param array $context options that normalizers/encoders have access to. * * @return scalar + * + * @throws UnexpectedValueException */ public function encode($data, $format, array $context = array()); diff --git a/src/Symfony/Component/Serializer/Encoder/JsonDecode.php b/src/Symfony/Component/Serializer/Encoder/JsonDecode.php index f8dfab35a889..67c199228a0f 100644 --- a/src/Symfony/Component/Serializer/Encoder/JsonDecode.php +++ b/src/Symfony/Component/Serializer/Encoder/JsonDecode.php @@ -11,6 +11,8 @@ namespace Symfony\Component\Serializer\Encoder; +use Symfony\Component\Serializer\Exception\UnexpectedValueException; + /** * Decodes JSON data * @@ -33,6 +35,7 @@ class JsonDecode implements DecoderInterface private $recursionDepth; private $lastError = JSON_ERROR_NONE; + protected $serializer; /** @@ -52,6 +55,7 @@ public function __construct($associative = false, $depth = 512) * * @return integer * + * @deprecated decode() throws an exception if error found * @see http://php.net/manual/en/function.json-last-error.php json_last_error */ public function getLastError() @@ -82,6 +86,8 @@ public function getLastError() * * @return mixed * + * @throws UnexpectedValueException + * * @see http://php.net/json_decode json_decode */ public function decode($data, $format, array $context = array()) @@ -98,7 +104,10 @@ public function decode($data, $format, array $context = array()) $decodedData = json_decode($data, $associative, $recursionDepth); } - $this->lastError = json_last_error(); + if (JSON_ERROR_NONE !== $this->lastError = json_last_error()) { + $message = JsonEncoder::getLastErrorMessage(); + throw new UnexpectedValueException($message); + } return $decodedData; } diff --git a/src/Symfony/Component/Serializer/Encoder/JsonEncode.php b/src/Symfony/Component/Serializer/Encoder/JsonEncode.php index 4e0de3ed7589..8bca8a970e7e 100644 --- a/src/Symfony/Component/Serializer/Encoder/JsonEncode.php +++ b/src/Symfony/Component/Serializer/Encoder/JsonEncode.php @@ -11,6 +11,8 @@ namespace Symfony\Component\Serializer\Encoder; +use Symfony\Component\Serializer\Exception\UnexpectedValueException; + /** * Encodes JSON data * @@ -27,10 +29,11 @@ public function __construct($bitmask = 0) } /** - * Returns the last encoding error (if any) + * Returns the last encoding error (if any). * * @return integer * + * @deprecated encode() throws an exception if error found * @see http://php.net/manual/en/function.json-last-error.php json_last_error */ public function getLastError() @@ -48,7 +51,11 @@ public function encode($data, $format, array $context = array()) $context = $this->resolveContext($context); $encodedJson = json_encode($data, $context['json_encode_options']); - $this->lastError = json_last_error(); + + if (JSON_ERROR_NONE !== $this->lastError = json_last_error()) { + $message = JsonEncoder::getLastErrorMessage(); + throw new UnexpectedValueException($message); + } return $encodedJson; } diff --git a/src/Symfony/Component/Serializer/Encoder/JsonEncoder.php b/src/Symfony/Component/Serializer/Encoder/JsonEncoder.php index 95dae7c8c667..ff57345ab491 100644 --- a/src/Symfony/Component/Serializer/Encoder/JsonEncoder.php +++ b/src/Symfony/Component/Serializer/Encoder/JsonEncoder.php @@ -37,9 +37,12 @@ public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodin } /** + * * Returns the last encoding error (if any) * * @return integer + * + * @deprecated JsonEncode throws exception if an error is found */ public function getLastEncodingError() { @@ -47,9 +50,12 @@ public function getLastEncodingError() } /** + * * Returns the last decoding error (if any) * * @return integer + * + * @deprecated JsonDecode throws exception if an error is found */ public function getLastDecodingError() { @@ -87,4 +93,31 @@ public function supportsDecoding($format) { return self::FORMAT === $format; } + + /** + * Resolves json_last_error message + * + * @return string + */ + public static function getLastErrorMessage() + { + if (function_exists('json_last_error_msg')) { + return json_last_error_msg(); + } + + switch (json_last_error()) { + case JSON_ERROR_DEPTH: + return 'Maximum stack depth exceeded'; + case JSON_ERROR_STATE_MISMATCH: + return 'Underflow or the modes mismatch'; + case JSON_ERROR_CTRL_CHAR: + return 'Unexpected control character found'; + case JSON_ERROR_SYNTAX: + return 'Syntax error, malformed JSON'; + case JSON_ERROR_UTF8: + return 'Malformed UTF-8 characters, possibly incorrectly encoded'; + default: + return 'Unknown error'; + } + } } From a1ab939c35400bd3eb13b1953f1e2d1fc0a2b539 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 28 Dec 2013 13:18:58 +0100 Subject: [PATCH 2/2] [Serializer] fixed CS --- src/Symfony/Component/Serializer/Encoder/JsonDecode.php | 6 +++--- src/Symfony/Component/Serializer/Encoder/JsonEncode.php | 6 +++--- src/Symfony/Component/Serializer/Encoder/JsonEncoder.php | 8 +++----- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Component/Serializer/Encoder/JsonDecode.php b/src/Symfony/Component/Serializer/Encoder/JsonDecode.php index 67c199228a0f..e649bffff9e3 100644 --- a/src/Symfony/Component/Serializer/Encoder/JsonDecode.php +++ b/src/Symfony/Component/Serializer/Encoder/JsonDecode.php @@ -55,7 +55,8 @@ public function __construct($associative = false, $depth = 512) * * @return integer * - * @deprecated decode() throws an exception if error found + * @deprecated since 2.5, decode() throws an exception if error found, will be removed in 3.0 + * * @see http://php.net/manual/en/function.json-last-error.php json_last_error */ public function getLastError() @@ -105,8 +106,7 @@ public function decode($data, $format, array $context = array()) } if (JSON_ERROR_NONE !== $this->lastError = json_last_error()) { - $message = JsonEncoder::getLastErrorMessage(); - throw new UnexpectedValueException($message); + throw new UnexpectedValueException(JsonEncoder::getLastErrorMessage()); } return $decodedData; diff --git a/src/Symfony/Component/Serializer/Encoder/JsonEncode.php b/src/Symfony/Component/Serializer/Encoder/JsonEncode.php index 8bca8a970e7e..5869f969c09f 100644 --- a/src/Symfony/Component/Serializer/Encoder/JsonEncode.php +++ b/src/Symfony/Component/Serializer/Encoder/JsonEncode.php @@ -33,7 +33,8 @@ public function __construct($bitmask = 0) * * @return integer * - * @deprecated encode() throws an exception if error found + * @deprecated since 2.5, encode() throws an exception if error found, will be removed in 3.0 + * * @see http://php.net/manual/en/function.json-last-error.php json_last_error */ public function getLastError() @@ -53,8 +54,7 @@ public function encode($data, $format, array $context = array()) $encodedJson = json_encode($data, $context['json_encode_options']); if (JSON_ERROR_NONE !== $this->lastError = json_last_error()) { - $message = JsonEncoder::getLastErrorMessage(); - throw new UnexpectedValueException($message); + throw new UnexpectedValueException(JsonEncoder::getLastErrorMessage()); } return $encodedJson; diff --git a/src/Symfony/Component/Serializer/Encoder/JsonEncoder.php b/src/Symfony/Component/Serializer/Encoder/JsonEncoder.php index ff57345ab491..78f2e945e9bc 100644 --- a/src/Symfony/Component/Serializer/Encoder/JsonEncoder.php +++ b/src/Symfony/Component/Serializer/Encoder/JsonEncoder.php @@ -37,12 +37,11 @@ public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodin } /** - * * Returns the last encoding error (if any) * * @return integer * - * @deprecated JsonEncode throws exception if an error is found + * @deprecated since 2.5, JsonEncode throws exception if an error is found, will be removed in 3.0 */ public function getLastEncodingError() { @@ -50,12 +49,11 @@ public function getLastEncodingError() } /** - * * Returns the last decoding error (if any) * * @return integer * - * @deprecated JsonDecode throws exception if an error is found + * @deprecated since 2.5, JsonDecode throws exception if an error is found, will be removed in 3.0 */ public function getLastDecodingError() { @@ -95,7 +93,7 @@ public function supportsDecoding($format) } /** - * Resolves json_last_error message + * Resolves json_last_error message. * * @return string */