Json encoder classes now throws UnexpectedValueException as XML classes

This commit is contained in:
Rodrigo Díez Villamuera 2013-11-23 10:35:55 +01:00 committed by Fabien Potencier
parent f9dff0616b
commit 6d9f0be0a0
6 changed files with 62 additions and 3 deletions

View File

@ -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
-----

View File

@ -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());

View File

@ -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());

View File

@ -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 @@ class JsonDecode implements DecoderInterface
*
* @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 @@ class JsonDecode implements DecoderInterface
*
* @return mixed
*
* @throws UnexpectedValueException
*
* @see http://php.net/json_decode json_decode
*/
public function decode($data, $format, array $context = array())
@ -98,7 +104,10 @@ class JsonDecode implements DecoderInterface
$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;
}

View File

@ -11,6 +11,8 @@
namespace Symfony\Component\Serializer\Encoder;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
/**
* Encodes JSON data
*
@ -27,10 +29,11 @@ class JsonEncode implements EncoderInterface
}
/**
* 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 @@ class JsonEncode implements EncoderInterface
$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;
}

View File

@ -37,9 +37,12 @@ class JsonEncoder implements EncoderInterface, DecoderInterface
}
/**
*
* 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 @@ class JsonEncoder implements EncoderInterface, DecoderInterface
}
/**
*
* 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 @@ class JsonEncoder implements EncoderInterface, DecoderInterface
{
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';
}
}
}