Skip to content

[Serializer] error handling inconsistencies fixed in the serializer decoders #9876

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 28, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Serializer/Encoder/DecoderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Serializer\Encoder;

use Symfony\Component\Serializer\Exception\UnexpectedValueException;

/**
* Defines the interface of decoders
*
Expand All @@ -31,6 +33,8 @@ interface DecoderInterface
* phpdoc comment.
*
* @return mixed
*
* @throws UnexpectedValueException
*/
public function decode($data, $format, array $context = array());

Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Serializer/Encoder/EncoderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Serializer\Encoder;

use Symfony\Component\Serializer\Exception\UnexpectedValueException;

/**
* Defines the interface of encoders
*
Expand All @@ -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());

Expand Down
11 changes: 10 additions & 1 deletion src/Symfony/Component/Serializer/Encoder/JsonDecode.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Serializer\Encoder;

use Symfony\Component\Serializer\Exception\UnexpectedValueException;

/**
* Decodes JSON data
*
Expand All @@ -33,6 +35,7 @@ class JsonDecode implements DecoderInterface
private $recursionDepth;

private $lastError = JSON_ERROR_NONE;

protected $serializer;

/**
Expand All @@ -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()
Expand Down Expand Up @@ -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())
Expand All @@ -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;
}
Expand Down
11 changes: 9 additions & 2 deletions src/Symfony/Component/Serializer/Encoder/JsonEncode.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Serializer\Encoder;

use Symfony\Component\Serializer\Exception\UnexpectedValueException;

/**
* Encodes JSON data
*
Expand All @@ -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()
Expand All @@ -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;
}
Expand Down
31 changes: 31 additions & 0 deletions src/Symfony/Component/Serializer/Encoder/JsonEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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()
{
Expand Down Expand Up @@ -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';
}
}
}