Json.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /** @see Zend_Serializer_Adapter_AdapterAbstract */
  23. require_once 'Zend/Serializer/Adapter/AdapterAbstract.php';
  24. /** @see Zend_Json */
  25. require_once 'Zend/Json.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Serializer
  29. * @subpackage Adapter
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Serializer_Adapter_Json extends Zend_Serializer_Adapter_AdapterAbstract
  34. {
  35. /**
  36. * @var array Default options
  37. */
  38. protected $_options = array(
  39. 'cycleCheck' => false,
  40. 'enableJsonExprFinder' => false,
  41. 'objectDecodeType' => Zend_Json::TYPE_ARRAY,
  42. );
  43. /**
  44. * Serialize PHP value to JSON
  45. *
  46. * @param mixed $value
  47. * @param array $opts
  48. * @return string
  49. * @throws Zend_Serializer_Exception on JSON encoding exception
  50. */
  51. public function serialize($value, array $opts = array())
  52. {
  53. $opts = $opts + $this->_options;
  54. try {
  55. return Zend_Json::encode($value, $opts['cycleCheck'], $opts);
  56. } catch (Exception $e) {
  57. require_once 'Zend/Serializer/Exception.php';
  58. throw new Zend_Serializer_Exception('Serialization failed', 0, $e);
  59. }
  60. }
  61. /**
  62. * Deserialize JSON to PHP value
  63. *
  64. * @param string $json
  65. * @param array $opts
  66. * @return mixed
  67. */
  68. public function unserialize($json, array $opts = array())
  69. {
  70. $opts = $opts + $this->_options;
  71. try {
  72. $ret = Zend_Json::decode($json, $opts['objectDecodeType']);
  73. } catch (Zend_Json_Exception $e) {
  74. require_once 'Zend/Serializer/Exception.php';
  75. throw new Zend_Serializer_Exception('Invalid json data');
  76. } catch (Exception $e) {
  77. require_once 'Zend/Serializer/Exception.php';
  78. throw new Zend_Serializer_Exception('Unserialization failed by previous error', 0, $e);
  79. }
  80. return $ret;
  81. }
  82. }