This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Serializer/Encoder/JsonEncoder.php
Fabien Potencier 2314328b16 Merge branch '2.4'
* 2.4:
  fixed types in phpdocs
  fixed types in phpdocs

Conflicts:
	src/Symfony/Component/Security/Core/Encoder/BasePasswordEncoder.php
	src/Symfony/Component/Security/Http/RememberMe/TokenBasedRememberMeServices.php
	src/Symfony/Component/Serializer/Encoder/JsonEncoder.php
	src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php
	src/Symfony/Component/Validator/Mapping/ClassMetadata.php
	src/Symfony/Component/Validator/Mapping/ElementMetadata.php
	src/Symfony/Component/Validator/Mapping/MemberMetadata.php
	src/Symfony/Component/Validator/MetadataFactoryInterface.php
2014-04-16 12:36:21 +02:00

122 lines
2.9 KiB
PHP

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Encoder;
/**
* Encodes JSON data
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class JsonEncoder implements EncoderInterface, DecoderInterface
{
const FORMAT = 'json';
/**
* @var JsonEncode
*/
protected $encodingImpl;
/**
* @var JsonDecode
*/
protected $decodingImpl;
public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodingImpl = null)
{
$this->encodingImpl = $encodingImpl ?: new JsonEncode();
$this->decodingImpl = $decodingImpl ?: new JsonDecode(true);
}
/**
* Returns the last encoding error (if any)
*
* @return int
*
* @deprecated since 2.5, JsonEncode throws exception if an error is found, will be removed in 3.0
*/
public function getLastEncodingError()
{
return $this->encodingImpl->getLastError();
}
/**
* Returns the last decoding error (if any)
*
* @return int
*
* @deprecated since 2.5, JsonDecode throws exception if an error is found, will be removed in 3.0
*/
public function getLastDecodingError()
{
return $this->decodingImpl->getLastError();
}
/**
* {@inheritdoc}
*/
public function encode($data, $format, array $context = array())
{
return $this->encodingImpl->encode($data, self::FORMAT, $context);
}
/**
* {@inheritdoc}
*/
public function decode($data, $format, array $context = array())
{
return $this->decodingImpl->decode($data, self::FORMAT, $context);
}
/**
* {@inheritdoc}
*/
public function supportsEncoding($format)
{
return self::FORMAT === $format;
}
/**
* {@inheritdoc}
*/
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';
}
}
}