comparison vendor/symfony/serializer/Encoder/JsonEncode.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
9 * file that was distributed with this source code. 9 * file that was distributed with this source code.
10 */ 10 */
11 11
12 namespace Symfony\Component\Serializer\Encoder; 12 namespace Symfony\Component\Serializer\Encoder;
13 13
14 use Symfony\Component\Serializer\Exception\UnexpectedValueException; 14 use Symfony\Component\Serializer\Exception\NotEncodableValueException;
15 15
16 /** 16 /**
17 * Encodes JSON data. 17 * Encodes JSON data.
18 * 18 *
19 * @author Sander Coolen <sander@jibber.nl> 19 * @author Sander Coolen <sander@jibber.nl>
20 */ 20 */
21 class JsonEncode implements EncoderInterface 21 class JsonEncode implements EncoderInterface
22 { 22 {
23 private $options; 23 private $options;
24 private $lastError = JSON_ERROR_NONE;
25 24
26 public function __construct($bitmask = 0) 25 public function __construct($bitmask = 0)
27 { 26 {
28 $this->options = $bitmask; 27 $this->options = $bitmask;
29 } 28 }
37 { 36 {
38 $context = $this->resolveContext($context); 37 $context = $this->resolveContext($context);
39 38
40 $encodedJson = json_encode($data, $context['json_encode_options']); 39 $encodedJson = json_encode($data, $context['json_encode_options']);
41 40
42 if (JSON_ERROR_NONE !== $this->lastError = json_last_error()) { 41 if (JSON_ERROR_NONE !== json_last_error() && (false === $encodedJson || !($context['json_encode_options'] & JSON_PARTIAL_OUTPUT_ON_ERROR))) {
43 throw new UnexpectedValueException(json_last_error_msg()); 42 throw new NotEncodableValueException(json_last_error_msg());
44 } 43 }
45 44
46 return $encodedJson; 45 return $encodedJson;
47 } 46 }
48 47
55 } 54 }
56 55
57 /** 56 /**
58 * Merge default json encode options with context. 57 * Merge default json encode options with context.
59 * 58 *
60 * @param array $context
61 *
62 * @return array 59 * @return array
63 */ 60 */
64 private function resolveContext(array $context = array()) 61 private function resolveContext(array $context = array())
65 { 62 {
66 return array_merge(array('json_encode_options' => $this->options), $context); 63 return array_merge(array('json_encode_options' => $this->options), $context);