|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Serializer |
|
17 * @subpackage Adapter |
|
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id: Json.php 20574 2010-01-24 17:39:14Z mabe $ |
|
21 */ |
|
22 |
|
23 /** @see Zend_Serializer_Adapter_AdapterAbstract */ |
|
24 require_once 'Zend/Serializer/Adapter/AdapterAbstract.php'; |
|
25 |
|
26 /** @see Zend_Json */ |
|
27 require_once 'Zend/Json.php'; |
|
28 |
|
29 /** |
|
30 * @category Zend |
|
31 * @package Zend_Serializer |
|
32 * @subpackage Adapter |
|
33 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
34 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
35 */ |
|
36 class Zend_Serializer_Adapter_Json extends Zend_Serializer_Adapter_AdapterAbstract |
|
37 { |
|
38 /** |
|
39 * @var array Default options |
|
40 */ |
|
41 protected $_options = array( |
|
42 'cycleCheck' => false, |
|
43 'enableJsonExprFinder' => false, |
|
44 'objectDecodeType' => Zend_Json::TYPE_ARRAY, |
|
45 ); |
|
46 |
|
47 /** |
|
48 * Serialize PHP value to JSON |
|
49 * |
|
50 * @param mixed $value |
|
51 * @param array $opts |
|
52 * @return string |
|
53 * @throws Zend_Serializer_Exception on JSON encoding exception |
|
54 */ |
|
55 public function serialize($value, array $opts = array()) |
|
56 { |
|
57 $opts = $opts + $this->_options; |
|
58 |
|
59 try { |
|
60 return Zend_Json::encode($value, $opts['cycleCheck'], $opts); |
|
61 } catch (Exception $e) { |
|
62 require_once 'Zend/Serializer/Exception.php'; |
|
63 throw new Zend_Serializer_Exception('Serialization failed', 0, $e); |
|
64 } |
|
65 } |
|
66 |
|
67 /** |
|
68 * Deserialize JSON to PHP value |
|
69 * |
|
70 * @param string $json |
|
71 * @param array $opts |
|
72 * @return mixed |
|
73 */ |
|
74 public function unserialize($json, array $opts = array()) |
|
75 { |
|
76 $opts = $opts + $this->_options; |
|
77 |
|
78 try { |
|
79 $ret = Zend_Json::decode($json, $opts['objectDecodeType']); |
|
80 } catch (Exception $e) { |
|
81 require_once 'Zend/Serializer/Exception.php'; |
|
82 throw new Zend_Serializer_Exception('Unserialization failed by previous error', 0, $e); |
|
83 } |
|
84 |
|
85 // json_decode returns null for invalid JSON |
|
86 if ($ret === null && $json !== 'null') { |
|
87 require_once 'Zend/Serializer/Exception.php'; |
|
88 throw new Zend_Serializer_Exception('Invalid json data'); |
|
89 } |
|
90 |
|
91 return $ret; |
|
92 } |
|
93 } |