JsonEncoder.php in Zircon Profile 8
File
vendor/symfony/serializer/Encoder/JsonEncoder.php
View source
<?php
namespace Symfony\Component\Serializer\Encoder;
class JsonEncoder implements EncoderInterface, DecoderInterface {
const FORMAT = 'json';
protected $encodingImpl;
protected $decodingImpl;
public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodingImpl = null) {
$this->encodingImpl = $encodingImpl ?: new JsonEncode();
$this->decodingImpl = $decodingImpl ?: new JsonDecode(true);
}
public function getLastEncodingError() {
@trigger_error('The ' . __METHOD__ . ' method is deprecated since version 2.5 and will be removed in 3.0. Catch the exception raised by the Symfony\\Component\\Serializer\\Encoder\\JsonEncode::encode() method instead to get the last JSON encoding error.', E_USER_DEPRECATED);
return $this->encodingImpl
->getLastError();
}
public function getLastDecodingError() {
@trigger_error('The ' . __METHOD__ . ' method is deprecated since version 2.5 and will be removed in 3.0. Catch the exception raised by the Symfony\\Component\\Serializer\\Encoder\\JsonDecode::decode() method instead to get the last JSON decoding error.', E_USER_DEPRECATED);
return $this->decodingImpl
->getLastError();
}
public function encode($data, $format, array $context = array()) {
return $this->encodingImpl
->encode($data, self::FORMAT, $context);
}
public function decode($data, $format, array $context = array()) {
return $this->decodingImpl
->decode($data, self::FORMAT, $context);
}
public function supportsEncoding($format) {
return self::FORMAT === $format;
}
public function supportsDecoding($format) {
return self::FORMAT === $format;
}
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';
}
}
}