diff --git a/src/Symfony/Component/Serializer/CHANGELOG.md b/src/Symfony/Component/Serializer/CHANGELOG.md index 5b859623fef09..66081baa05854 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 9dd336d5f7857..9bd5fcbe1692e 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 2290db7f50d85..b928b165294bb 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 f8dfab35a8894..e649bffff9e3a 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,8 @@ public function __construct($associative = false, $depth = 512) * * @return integer * + * @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() @@ -82,6 +87,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 +105,9 @@ 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()) { + 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 4e0de3ed75890..5869f969c09f9 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,12 @@ public function __construct($bitmask = 0) } /** - * Returns the last encoding error (if any) + * Returns the last encoding error (if any). * * @return integer * + * @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() @@ -48,7 +52,10 @@ 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()) { + 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 95dae7c8c6674..78f2e945e9bcc 100644 --- a/src/Symfony/Component/Serializer/Encoder/JsonEncoder.php +++ b/src/Symfony/Component/Serializer/Encoder/JsonEncoder.php @@ -40,6 +40,8 @@ public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodin * Returns the last encoding error (if any) * * @return integer + * + * @deprecated since 2.5, JsonEncode throws exception if an error is found, will be removed in 3.0 */ public function getLastEncodingError() { @@ -50,6 +52,8 @@ public function getLastEncodingError() * Returns the last decoding error (if any) * * @return integer + * + * @deprecated since 2.5, JsonDecode throws exception if an error is found, will be removed in 3.0 */ public function getLastDecodingError() { @@ -87,4 +91,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'; + } + } }