8000 Json encoder classes now throws UnexpectedValueException as XML classes · symfony/symfony@6d9f0be · GitHub
[go: up one dir, main page]

Skip to content

Commit 6d9f0be

Browse files
Rodrigo Díez Villamuerafabpot
Rodrigo Díez Villamuera
authored andcommitted
Json encoder classes now throws UnexpectedValueException as XML classes
1 parent f9dff06 commit 6d9f0be

File tree

6 files changed

+62
-3
lines changed

6 files changed

+62
-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,7 @@ public function __construct($associative = false, $depth = 512)
5255
*
5356
* @return integer
5457
*
58+
* @deprecated decode() throws an exception if error found
5559
* @see http://php.net/manual/en/function.json-last-error.php json_last_error
5660
*/
5761
public function getLastError()
@@ -82,6 +86,8 @@ public function getLastError()
8286
*
8387
* @return mixed
8488
*
89+
* @throws UnexpectedValueException
90+
*
8591
* @see http://php.net/json_decode json_decode
8692
*/
8793
public function decode($data, $format, array $context = array())
@@ -98,7 +104,10 @@ public function decode($data, $format, array $context = array())
98104
$decodedData = json_decode($data, $associative, $recursionDepth);
99105
}
100106

101-
$this->lastError = json_last_error();
107+
if (JSON_ERROR_NONE !== $this->lastError = json_last_error()) {
108+
$message = JsonEncoder::getLastErrorMessage();
109+
throw new UnexpectedValueException($message);
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,11 @@ 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 encode() throws an exception if error found
3437
* @see http://php.net/manual/en/function.json-last-error.php json_last_error
3538
*/
3639
public function getLastError()
@@ -48,7 +51,11 @@ public function encode($data, $format, array $context = array())
4851
$context = $this->resolveContext($context);
4952

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

5360
return $encodedJson;
5461
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,25 @@ public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodin
3737
}
3838

3939
/**
40+
*
4041
* Returns the last encoding error (if any)
4142
*
4243
* @return integer
44+
*
45+
* @deprecated JsonEncode throws exception if an error is found
4346
*/
4447
public function getLastEncodingError()
4548
{
4649
return $this->encodingImpl->getLastError();
4750
}
4851

4952
/**
53+
*
5054
* Returns the last decoding error (if any)
5155
*
5256
* @return integer
57+
*
58+
* @deprecated JsonDecode throws exception if an error is found
5359
*/
5460
public function getLastDecodingError()
5561
{
@@ -87,4 +93,31 @@ public function supportsDecoding($format)
8793
{
8894
return self::FORMAT === $format;
8995
}
96+
97+
/**
98+
* Resolves json_last_error message
99+
*
100+
* @return string
101+
*/
102+
public static function getLastErrorMessage()
103+
{
104+
if (function_exists('json_last_error_msg')) {
105+
return json_last_error_msg();
106+
}
107+
108+
switch (json_last_error()) {
109+
case JSON_ERROR_DEPTH:
110+
return 'Maximum stack depth exceeded';
111+
case JSON_ERROR_STATE_MISMATCH:
112+
return 'Underflow or the modes mismatch';
113+
case JSON_ERROR_CTRL_CHAR:
114+
return 'Unexpected control character found';
115+
case JSON_ERROR_SYNTAX:
116+
return 'Syntax error, malformed JSON';
117+
case JSON_ERROR_UTF8:
118+
return 'Malformed UTF-8 characters, possibly incorrectly encoded';
119+
default:
120+
return 'Unknown error';
121+
}
122+
}
90123
}

0 commit comments

Comments
 (0)
0