AJAX
[ class tree: AJAX ] [ index: AJAX ] [ all elements ]

Source for file JSON.php

Documentation is available at JSON.php

  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: @package_version@
  13.  * @link       http://pear.php.net/package/PackageName
  14.  */
  15. // {{{ class HTMLA_AJAX_Serialize_JSON
  16. {
  17.     // {{{ variables-properties
  18.     
  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.     
  41.     function HTML_AJAX_Serializer_JSON($use_loose_type = true
  42.     {
  43.         $this->loose_type = (bool) $use_loose_type;
  44.         $this->_jsonext $this->_detect();
  45.         if(!$this->_jsonext{
  46.             $use_loose_type ($this->loose_typeSERVICES_JSON_LOOSE_TYPE : 0;
  47.             $this->_json = new HTML_AJAX_JSON($use_loose_type);
  48.         }
  49.     }
  50.     // }}}
  51.     // {{{ serialize
  52.     
  53.     /**
  54.      * This function serializes and input passed to it.
  55.      *
  56.      * @access public
  57.      * @param  string $input   The input to serialize.
  58.      * @return string $input   The serialized input.
  59.      */
  60.     function serialize($input
  61.     {
  62.         if($this->_jsonext{
  63.             return json_encode($input);
  64.         else {
  65.             return $this->_json->encode($input);
  66.         }
  67.     }
  68.     // }}}
  69.     // {{{ unserialize
  70.     
  71.     /**
  72.      * this function unserializes the input passed to it.
  73.      *
  74.      * @access public
  75.      * @param  string $input   The input to unserialize
  76.      * @return string $input   The unserialized input.
  77.      */
  78.     function unserialize($input
  79.     {
  80.         if($this->_jsonext{
  81.             return json_decode($input$this->loose_type);
  82.         else {
  83.             return $this->_json->decode($input);
  84.         }
  85.     }
  86.     // }}}
  87.     // {{{ _detect
  88.     
  89.     /**
  90.      * detects the loaded extension
  91.      */
  92.     function _detect()
  93.     {
  94.         return extension_loaded('json');
  95.     }
  96.     // }}}
  97.  
  98. }
  99. // }}}
  100. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  101. ?>

Documentation generated on Sat, 05 May 2007 18:00:16 -0400 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.