Json encoder classes now throws UnexpectedValueException as XML classes
This commit is contained in:
parent
f9dff0616b
commit
6d9f0be0a0
@ -5,6 +5,8 @@ CHANGELOG
|
|||||||
-----
|
-----
|
||||||
|
|
||||||
* added `$context` support for XMLEncoder.
|
* 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
|
2.3.0
|
||||||
-----
|
-----
|
||||||
|
@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Serializer\Encoder;
|
namespace Symfony\Component\Serializer\Encoder;
|
||||||
|
|
||||||
|
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines the interface of decoders
|
* Defines the interface of decoders
|
||||||
*
|
*
|
||||||
@ -31,6 +33,8 @@ interface DecoderInterface
|
|||||||
* phpdoc comment.
|
* phpdoc comment.
|
||||||
*
|
*
|
||||||
* @return mixed
|
* @return mixed
|
||||||
|
*
|
||||||
|
* @throws UnexpectedValueException
|
||||||
*/
|
*/
|
||||||
public function decode($data, $format, array $context = array());
|
public function decode($data, $format, array $context = array());
|
||||||
|
|
||||||
|
@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Serializer\Encoder;
|
namespace Symfony\Component\Serializer\Encoder;
|
||||||
|
|
||||||
|
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines the interface of encoders
|
* Defines the interface of encoders
|
||||||
*
|
*
|
||||||
@ -26,6 +28,8 @@ interface EncoderInterface
|
|||||||
* @param array $context options that normalizers/encoders have access to.
|
* @param array $context options that normalizers/encoders have access to.
|
||||||
*
|
*
|
||||||
* @return scalar
|
* @return scalar
|
||||||
|
*
|
||||||
|
* @throws UnexpectedValueException
|
||||||
*/
|
*/
|
||||||
public function encode($data, $format, array $context = array());
|
public function encode($data, $format, array $context = array());
|
||||||
|
|
||||||
|
@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Serializer\Encoder;
|
namespace Symfony\Component\Serializer\Encoder;
|
||||||
|
|
||||||
|
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decodes JSON data
|
* Decodes JSON data
|
||||||
*
|
*
|
||||||
@ -33,6 +35,7 @@ class JsonDecode implements DecoderInterface
|
|||||||
private $recursionDepth;
|
private $recursionDepth;
|
||||||
|
|
||||||
private $lastError = JSON_ERROR_NONE;
|
private $lastError = JSON_ERROR_NONE;
|
||||||
|
|
||||||
protected $serializer;
|
protected $serializer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -52,6 +55,7 @@ class JsonDecode implements DecoderInterface
|
|||||||
*
|
*
|
||||||
* @return integer
|
* @return integer
|
||||||
*
|
*
|
||||||
|
* @deprecated decode() throws an exception if error found
|
||||||
* @see http://php.net/manual/en/function.json-last-error.php json_last_error
|
* @see http://php.net/manual/en/function.json-last-error.php json_last_error
|
||||||
*/
|
*/
|
||||||
public function getLastError()
|
public function getLastError()
|
||||||
@ -82,6 +86,8 @@ class JsonDecode implements DecoderInterface
|
|||||||
*
|
*
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*
|
*
|
||||||
|
* @throws UnexpectedValueException
|
||||||
|
*
|
||||||
* @see http://php.net/json_decode json_decode
|
* @see http://php.net/json_decode json_decode
|
||||||
*/
|
*/
|
||||||
public function decode($data, $format, array $context = array())
|
public function decode($data, $format, array $context = array())
|
||||||
@ -98,7 +104,10 @@ class JsonDecode implements DecoderInterface
|
|||||||
$decodedData = json_decode($data, $associative, $recursionDepth);
|
$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;
|
return $decodedData;
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Serializer\Encoder;
|
namespace Symfony\Component\Serializer\Encoder;
|
||||||
|
|
||||||
|
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encodes JSON data
|
* 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
|
* @return integer
|
||||||
*
|
*
|
||||||
|
* @deprecated encode() throws an exception if error found
|
||||||
* @see http://php.net/manual/en/function.json-last-error.php json_last_error
|
* @see http://php.net/manual/en/function.json-last-error.php json_last_error
|
||||||
*/
|
*/
|
||||||
public function getLastError()
|
public function getLastError()
|
||||||
@ -48,7 +51,11 @@ class JsonEncode implements EncoderInterface
|
|||||||
$context = $this->resolveContext($context);
|
$context = $this->resolveContext($context);
|
||||||
|
|
||||||
$encodedJson = json_encode($data, $context['json_encode_options']);
|
$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;
|
return $encodedJson;
|
||||||
}
|
}
|
||||||
|
@ -37,9 +37,12 @@ class JsonEncoder implements EncoderInterface, DecoderInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
*
|
||||||
* Returns the last encoding error (if any)
|
* Returns the last encoding error (if any)
|
||||||
*
|
*
|
||||||
* @return integer
|
* @return integer
|
||||||
|
*
|
||||||
|
* @deprecated JsonEncode throws exception if an error is found
|
||||||
*/
|
*/
|
||||||
public function getLastEncodingError()
|
public function getLastEncodingError()
|
||||||
{
|
{
|
||||||
@ -47,9 +50,12 @@ class JsonEncoder implements EncoderInterface, DecoderInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
*
|
||||||
* Returns the last decoding error (if any)
|
* Returns the last decoding error (if any)
|
||||||
*
|
*
|
||||||
* @return integer
|
* @return integer
|
||||||
|
*
|
||||||
|
* @deprecated JsonDecode throws exception if an error is found
|
||||||
*/
|
*/
|
||||||
public function getLastDecodingError()
|
public function getLastDecodingError()
|
||||||
{
|
{
|
||||||
@ -87,4 +93,31 @@ class JsonEncoder implements EncoderInterface, DecoderInterface
|
|||||||
{
|
{
|
||||||
return self::FORMAT === $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';
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user