Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabpot merged 2 commits intosymfony:masterfromfabpot:serializer-errors
Dec 28, 2013
Merged
Show file tree
Hide file tree
Changes from1 commit
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
NextNext commit
Json encoder classes now throws UnexpectedValueException as XML classes
  • Loading branch information
@fabpot
Rodrigo Díez Villamuera authored andfabpot committedDec 28, 2013
commit6d9f0be0a0b362f1f0dc3484a3b0331693940159
2 changes: 2 additions & 0 deletionssrc/Symfony/Component/Serializer/CHANGELOG.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsrc/Symfony/Component/Serializer/Encoder/JsonDecode.php
View file
Open in desktop
Original file line numberDiff line numberDiff 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,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()
Expand DownExpand Up@@ -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())
Expand All@@ -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;
}
Expand Down
11 changes: 9 additions & 2 deletionssrc/Symfony/Component/Serializer/Encoder/JsonEncode.php
View file
Open in desktop
Original file line numberDiff line numberDiff 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,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()
Expand All@@ -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;
}
Expand Down
33 changes: 33 additions & 0 deletionssrc/Symfony/Component/Serializer/Encoder/JsonEncoder.php
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -37,19 +37,25 @@ 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()
{
return $this->encodingImpl->getLastError();
}

/**
*
* Returns the last decoding error (if any)
*
* @return integer
*
* @deprecated JsonDecode throws exception if an error is found
*/
public function getLastDecodingError()
{
Expand DownExpand Up@@ -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';
}
}
}

[8]ページ先頭

©2009-2025 Movatter.jp