File indexing completed on 2025-01-26 05:25:25

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Serializer
0017  * @subpackage Adapter
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /** @see Zend_Serializer_Adapter_AdapterAbstract */
0024 // require_once 'Zend/Serializer/Adapter/AdapterAbstract.php';
0025 
0026 /** @see Zend_Json */
0027 // require_once 'Zend/Json.php';
0028 
0029 /**
0030  * @category   Zend
0031  * @package    Zend_Serializer
0032  * @subpackage Adapter
0033  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0034  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0035  */
0036 class Zend_Serializer_Adapter_Json extends Zend_Serializer_Adapter_AdapterAbstract
0037 {
0038     /**
0039      * @var array Default options
0040      */
0041     protected $_options = array(
0042         'cycleCheck'           => false,
0043         'enableJsonExprFinder' => false,
0044         'objectDecodeType'     => Zend_Json::TYPE_ARRAY,
0045     );
0046 
0047     /**
0048      * Serialize PHP value to JSON
0049      *
0050      * @param  mixed $value
0051      * @param  array $opts
0052      * @return string
0053      * @throws Zend_Serializer_Exception on JSON encoding exception
0054      */
0055     public function serialize($value, array $opts = array())
0056     {
0057         $opts = $opts + $this->_options;
0058 
0059         try  {
0060             return Zend_Json::encode($value, $opts['cycleCheck'], $opts);
0061         } catch (Exception $e) {
0062             // require_once 'Zend/Serializer/Exception.php';
0063             throw new Zend_Serializer_Exception('Serialization failed', 0, $e);
0064         }
0065     }
0066 
0067     /**
0068      * Deserialize JSON to PHP value
0069      *
0070      * @param  string $json
0071      * @param  array $opts
0072      * @return mixed
0073      */
0074     public function unserialize($json, array $opts = array())
0075     {
0076         $opts = $opts + $this->_options;
0077 
0078         try {
0079             $ret = Zend_Json::decode($json, $opts['objectDecodeType']);
0080         } catch (Zend_Json_Exception $e) {
0081             // require_once 'Zend/Serializer/Exception.php';
0082             throw new Zend_Serializer_Exception('Invalid json data');
0083         } catch (Exception $e) {
0084             // require_once 'Zend/Serializer/Exception.php';
0085             throw new Zend_Serializer_Exception('Unserialization failed by previous error', 0, $e);
0086         }
0087 
0088         return $ret;
0089     }
0090 }