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

Source for file json-encoder.php

Documentation is available at json-encoder.php

  1. <?php
  2. /**
  3. *****************************************************
  4. * This module has been modified by ccHost developers *
  5. *****************************************************
  6. *
  7. * Zend Framework
  8. *
  9. * LICENSE
  10. *
  11. * This source file is subject to the new BSD license that is bundled
  12. * with this package in the file LICENSE.txt.
  13. * It is also available through the world-wide-web at this URL:
  14. * http://framework.zend.com/license/new-bsd
  15. * If you did not receive a copy of the license and are unable to
  16. * obtain it through the world-wide-web, please send an email
  17. * to license@zend.com so we can send you a copy immediately.
  18. *
  19. * @category Zend
  20. * @package Zend_Json
  21. * @copyright Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com)
  22. * @license http://framework.zend.com/license/new-bsd New BSD License
  23. */
  24.  
  25.  
  26. /**
  27. * Encode PHP constructs to JSON (modified for ccHost)
  28. *
  29. * @package core
  30. * @copyright Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class CCZend_Json_Encoder
  34. {
  35. /**
  36. * Array of visited objects; used to prevent cycling.
  37. *
  38. * @var array
  39. */
  40. var $_visited = array();
  41.  
  42. /**
  43. * Constructor
  44. */
  45. function __construct()
  46. {
  47. }
  48.  
  49. /**
  50. * Use the JSON encoding scheme for the value specified
  51. *
  52. * @param mixed $value value the object to be encoded
  53. * @return string The encoded value
  54. */
  55. function encode($value)
  56. {
  57. $encoder = new CCZend_Json_Encoder();
  58.  
  59. return $encoder->_encodeValue($value);
  60. }
  61.  
  62. /**
  63. * Recursive driver which determines the type of value to be encoded
  64. * and then dispatches to the appropriate method. $values are either
  65. * - objects (returns from {@link _encodeObject()})
  66. * - arrays (returns from {@link _encodeArray()})
  67. * - basic datums (e.g. numbers or strings) (returns from {@link _encodeDatum()})
  68. *
  69. * @param $value mixed The value to be encoded
  70. * @return string Encoded value
  71. */
  72. function _encodeValue(&$value)
  73. {
  74. if (is_object($value)) {
  75. return $this->_encodeObject($value);
  76. } else if (is_array($value)) {
  77. return $this->_encodeArray($value);
  78. }
  79.  
  80. return $this->_encodeDatum($value);
  81. }
  82.  
  83.  
  84.  
  85. /**
  86. * Encode an object to JSON by encoding each of the properties
  87. *
  88. * A special property is added to the JSON object called '__className'
  89. * that contains the name of the class of $value. This is used to decode
  90. * the object on the client into a specific class.
  91. *
  92. * @param $value object
  93. * @return string
  94. */
  95. function _encodeObject(&$value)
  96. {
  97. if ($this->_wasVisited($value)) {
  98. trigger_error(
  99. 'Cycles not supported in JSON encoding, cycle introduced by '
  100. . 'class "' . get_class($value) . '"'
  101. );
  102. }
  103.  
  104. $this->_visited[] = $value;
  105.  
  106. $props = '';
  107. foreach (get_object_vars($value) as $name => $propValue) {
  108. if (isset($propValue)) {
  109. $props .= ', '
  110. . $this->_encodeValue($name)
  111. . ' : '
  112. . $this->_encodeValue($propValue);
  113. }
  114. }
  115.  
  116. return '{' . '"__className": "' . get_class($value) . '"'
  117. . $props . '}';
  118. }
  119.  
  120.  
  121. /**
  122. * Determine if an object has been serialized already
  123. *
  124. * @access protected
  125. * @param mixed $value
  126. * @return boolean
  127. */
  128. function _wasVisited(&$value)
  129. {
  130. if (in_array($value, $this->_visited, true)) {
  131. return true;
  132. }
  133.  
  134. return false;
  135. }
  136.  
  137.  
  138. /**
  139. * JSON encode an array value
  140. *
  141. * Recursively encodes each value of an array and returns a JSON encoded
  142. * array string.
  143. *
  144. * Arrays are defined as integer-indexed arrays starting at index 0, where
  145. * the last index is (count($array) -1); any deviation from that is
  146. * considered an associative array, and will be encoded as such.
  147. *
  148. * @param $array array
  149. * @return string
  150. */
  151. function _encodeArray(&$array)
  152. {
  153. $tmpArray = array();
  154.  
  155. // Check for associative array
  156. if (array_keys($array) !== range(0, count($array) - 1)) {
  157. // Associative array
  158. $result = '{';
  159. foreach ($array as $key => $value) {
  160. $key = (string) $key;
  161. $tmpArray[] = $this->_encodeString($key)
  162. . ' : '
  163. . $this->_encodeValue($value);
  164. }
  165. $result .= implode(', ', $tmpArray);
  166. $result .= '}';
  167. } else {
  168. // Indexed array
  169. $result = '[';
  170. $length = count($array);
  171. for ($i = 0; $i < $length; $i++) {
  172. $tmpArray[] = $this->_encodeValue($array[$i]);
  173. }
  174. $result .= implode(', ', $tmpArray);
  175. $result .= ']';
  176. }
  177.  
  178. return $result;
  179. }
  180.  
  181.  
  182. /**
  183. * JSON encode a basic data type (string, number, boolean, null)
  184. *
  185. * If value type is not a string, number, boolean, or null, the string
  186. * 'null' is returned.
  187. *
  188. * @param $value mixed
  189. * @return string
  190. */
  191. function _encodeDatum(&$value)
  192. {
  193. $result = 'null';
  194.  
  195. if (is_numeric($value)) {
  196. $result = (string)$value;
  197. } elseif (is_string($value)) {
  198. $result = $this->_encodeString($value);
  199. } elseif (is_bool($value)) {
  200. $result = $value ? 'true' : 'false';
  201. }
  202.  
  203. return $result;
  204. }
  205.  
  206.  
  207. /**
  208. * JSON encode a string value by escaping characters as necessary
  209. *
  210. * @param $value string
  211. * @return string
  212. */
  213. function _encodeString(&$string)
  214. {
  215. // Escape these characters with a backslash:
  216. // " \ / \n \r \t \b \f
  217. $search = array('\\', "\n", "\t", "\r", "\b", "\f", '"');
  218. $replace = array('\\\\', '\\n', '\\t', '\\r', '\\b', '\\f', '\"');
  219. $string = str_replace($search, $replace, $string);
  220.  
  221. // Escape certain ASCII characters:
  222. // 0x08 => \b
  223. // 0x0c => \f
  224. $string = str_replace(array(chr(0x08), chr(0x0C)), array('\b', '\f'), $string);
  225.  
  226. return '"' . $string . '"';
  227. }
  228.  
  229. }

Documentation generated on Sat, 17 Nov 2007 01:04:59 +0000 by phpDocumentor 1.3.0RC4