home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / HTML / AJAX / Serializer / JSON.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  2.4 KB  |  97 lines

  1. <?php
  2. require_once 'HTML/AJAX/JSON.php';
  3. // $Id$
  4. /**
  5.  * JSON Serializer
  6.  *
  7.  * @category   HTML
  8.  * @package    AJAX
  9.  * @author     Joshua Eichorn <josh@bluga.net>
  10.  * @copyright  2005 Joshua Eichorn
  11.  * @license    http://www.opensource.org/licenses/lgpl-license.php  LGPL
  12.  * @version    Release: 0.5.6
  13.  * @link       http://pear.php.net/package/PackageName
  14.  */
  15. // {{{ class HTMLA_AJAX_Serialize_JSON
  16. class HTML_AJAX_Serializer_JSON 
  17. {
  18.     // {{{ variables-properties
  19.     /**
  20.      * JSON instance
  21.      * @var HTML_AJAX_JSON
  22.      * @access private
  23.      */
  24.     var $_json;
  25.  
  26.     /**
  27.      * use json php extension http://www.aurore.net/projects/php-json/
  28.      * @access private
  29.      */
  30.     var $_jsonext;
  31.  
  32.     /**
  33.      * use loose typing to decode js objects into php associative arrays
  34.      * @access public
  35.      */
  36.     var $loose_type;
  37.     
  38.     // }}}
  39.     // {{{ constructor
  40.     function HTML_AJAX_Serializer_JSON($use_loose_type = true) 
  41.     {
  42.         $this->loose_type = (bool) $use_loose_type;
  43.         $this->_jsonext = $this->_detect();
  44.         if(!$this->_jsonext) {
  45.             $use_loose_type = ($this->loose_type) ? SERVICES_JSON_LOOSE_TYPE : 0;
  46.             $this->_json = new HTML_AJAX_JSON($use_loose_type);
  47.         }
  48.     }
  49.     // }}}
  50.     // {{{ serialize
  51.     /**
  52.      * This function serializes and input passed to it.
  53.      *
  54.      * @access public
  55.      * @param  string $input   The input to serialize.
  56.      * @return string $input   The serialized input.
  57.      */
  58.     function serialize($input) 
  59.     {
  60.         if($this->_jsonext) {
  61.             return json_encode($input);
  62.         } else {
  63.             return $this->_json->encode($input);
  64.         }
  65.     }
  66.     // }}}
  67.     // {{{ unserialize
  68.     /**
  69.      * this function unserializes the input passed to it.
  70.      *
  71.      * @access public
  72.      * @param  string $input   The input to unserialize
  73.      * @return string $input   The unserialized input.
  74.      */
  75.     function unserialize($input) 
  76.     {
  77.         if($this->_jsonext) {
  78.             return json_decode($input, $this->loose_type);
  79.         } else {
  80.             return $this->_json->decode($input);
  81.         }
  82.     }
  83.     // }}}
  84.     // {{{ _detect
  85.     /**
  86.      * detects the loaded extension
  87.      */
  88.     function _detect()
  89.     {
  90.         return extension_loaded('json');
  91.     }
  92.     // }}}
  93. }
  94. // }}}
  95. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  96. ?>
  97.