Skip to content

Commit f0d9af0

Browse files
committed
feature #9876 [Serializer] error handling inconsistencies fixed in the serializer decoders (fabpot)
This PR was merged into the 2.5-dev branch. Discussion ---------- [Serializer] error handling inconsistencies fixed in the serializer decoders | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #9393 | License | MIT | Doc PR | none see #9586 for the original PR Commits ------- a1ab939 [Serializer] fixed CS 6d9f0be Json encoder classes now throws UnexpectedValueException as XML classes
2 parents f9dff06 + a1ab939 commit f0d9af0

File tree

6 files changed

+60
-3
lines changed

6 files changed

+60
-3
lines changed

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ CHANGELOG
55
-----
66

77
* added `$context` support for XMLEncoder.
8+
* [DEPRECATION] JsonEncode and JsonDecode where modified to throw
9+
an exception if error found. No need for get*Error() functions
810

911
2.3.0
1012
-----

src/Symfony/Component/Serializer/Encoder/DecoderInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Serializer\Encoder;
1313

14+
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
15+
1416
/**
1517
* Defines the interface of decoders
1618
*
@@ -31,6 +33,8 @@ interface DecoderInterface
3133
* phpdoc comment.
3234
*
3335
* @return mixed
36+
*
37+
* @throws UnexpectedValueException
3438
*/
3539
public function decode($data, $format, array $context = array());
3640

src/Symfony/Component/Serializer/Encoder/EncoderInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Serializer\Encoder;
1313

14+
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
15+
1416
/**
1517
* Defines the interface of encoders
1618
*
@@ -26,6 +28,8 @@ interface EncoderInterface
2628
* @param array $context options that normalizers/encoders have access to.
2729
*
2830
* @return scalar
31+
*
32+
* @throws UnexpectedValueException
2933
*/
3034
public function encode($data, $format, array $context = array());
3135

src/Symfony/Component/Serializer/Encoder/JsonDecode.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Serializer\Encoder;
1313

14+
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
15+
1416
/**
1517
* Decodes JSON data
1618
*
@@ -33,6 +35,7 @@ class JsonDecode implements DecoderInterface
3335
private $recursionDepth;
3436

3537
private $lastError = JSON_ERROR_NONE;
38+
3639
protected $serializer;
3740

3841
/**
@@ -52,6 +55,8 @@ public function __construct($associative = false, $depth = 512)
5255
*
5356
* @return integer
5457
*
58+
* @deprecated since 2.5, decode() throws an exception if error found, will be removed in 3.0
59+
*
5560
* @see http://php.net/manual/en/function.json-last-error.php json_last_error
5661
*/
5762
public function getLastError()
@@ -82,6 +87,8 @@ public function getLastError()
8287
*
8388
* @return mixed
8489
*
90+
* @throws UnexpectedValueException
91+
*
8592
* @see http://php.net/json_decode json_decode
8693
*/
8794
public function decode($data, $format, array $context = array())
@@ -98,7 +105,9 @@ public function decode($data, $format, array $context = array())
98105
$decodedData = json_decode($data, $associative, $recursionDepth);
99106
}
100107

101-
$this->lastError = json_last_error();
108+
if (JSON_ERROR_NONE !== $this->lastError = json_last_error()) {
109+
throw new UnexpectedValueException(JsonEncoder::getLastErrorMessage());
110+
}
102111

103112
return $decodedData;
104113
}

src/Symfony/Component/Serializer/Encoder/JsonEncode.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Serializer\Encoder;
1313

14+
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
15+
1416
/**
1517
* Encodes JSON data
1618
*
@@ -27,10 +29,12 @@ public function __construct($bitmask = 0)
2729
}
2830

2931
/**
30-
* Returns the last encoding error (if any)
32+
* Returns the last encoding error (if any).
3133
*
3234
* @return integer
3335
*
36+
* @deprecated since 2.5, encode() throws an exception if error found, will be removed in 3.0
37+
*
3438
* @see http://php.net/manual/en/function.json-last-error.php json_last_error
3539
*/
3640
public function getLastError()
@@ -48,7 +52,10 @@ public function encode($data, $format, array $context = array())
4852
$context = $this->resolveContext($context);
4953

5054
$encodedJson = json_encode($data, $context['json_encode_options']);
51-
$this->lastError = json_last_error();
55+
56+
if (JSON_ERROR_NONE !== $this->lastError = json_last_error()) {
57+
throw new UnexpectedValueException(JsonEncoder::getLastErrorMessage());
58+
}
5259

5360
return $encodedJson;
5461
}

src/Symfony/Component/Serializer/Encoder/JsonEncoder.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodin
4040
* Returns the last encoding error (if any)
4141
*
4242
* @return integer
43+
*
44+
* @deprecated since 2.5, JsonEncode throws exception if an error is found, will be removed in 3.0
4345
*/
4446
public function getLastEncodingError()
4547
{
@@ -50,6 +52,8 @@ public function getLastEncodingError()
5052
* Returns the last decoding error (if any)
5153
*
5254
* @return integer
55+
*
56+
* @deprecated since 2.5, JsonDecode throws exception if an error is found, will be removed in 3.0
5357
*/
5458
public function getLastDecodingError()
5559
{
@@ -87,4 +91,31 @@ public function supportsDecoding($format)
8791
{
8892
return self::FORMAT === $format;
8993
}
94+
95+
/**
96+
* Resolves json_last_error message.
97+
*
98+
* @return string
99+
*/
100+
public static function getLastErrorMessage()
101+
{
102+
if (function_exists('json_last_error_msg')) {
103+
return json_last_error_msg();
104+
}
105+
106+
switch (json_last_error()) {
107+
case JSON_ERROR_DEPTH:
108+
return 'Maximum stack depth exceeded';
109+
case JSON_ERROR_STATE_MISMATCH:
110+
return 'Underflow or the modes mismatch';
111+
case JSON_ERROR_CTRL_CHAR:
112+
return 'Unexpected control character found';
113+
case JSON_ERROR_SYNTAX:
114+
return 'Syntax error, malformed JSON';
115+
case JSON_ERROR_UTF8:
116+
return 'Malformed UTF-8 characters, possibly incorrectly encoded';
117+
default:
118+
return 'Unknown error';
119+
}
120+
}
90121
}

0 commit comments

Comments
 (0)