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.
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
27 require_once 'Zend/Json/Expr.php';
31 * Class for encoding to and decoding from JSON.
35 * @uses Zend_Json_Expr
36 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
37 * @license http://framework.zend.com/license/new-bsd New BSD License
42 * How objects should be encoded -- arrays or as StdClass. TYPE_ARRAY is 1
43 * so that it is a boolean true value, allowing it to be used with
44 * ext/json's functions.
47 const TYPE_OBJECT
= 0;
50 * To check the allowed nesting depth of the XML tree during xml2json conversion.
54 public static $maxRecursionDepthAllowed=25;
59 public static $useBuiltinEncoderDecoder = false;
62 * Decodes the given $encodedValue string which is
63 * encoded in the JSON format
65 * Uses ext/json's json_decode if available.
67 * @param string $encodedValue Encoded in JSON format
68 * @param int $objectDecodeType Optional; flag indicating how to decode
69 * objects. See {@link Zend_Json_Decoder::decode()} for details.
72 public static function decode($encodedValue, $objectDecodeType = Zend_Json
::TYPE_ARRAY
)
74 $encodedValue = (string) $encodedValue;
75 if (function_exists('json_decode') && self
::$useBuiltinEncoderDecoder !== true) {
76 $decode = json_decode($encodedValue, $objectDecodeType);
79 if (!function_exists('json_last_error')) {
80 if ($decode === $encodedValue) {
81 require_once 'Zend/Json/Exception.php';
82 throw new Zend_Json_Exception('Decoding failed');
85 } elseif (($jsonLastErr = json_last_error()) != JSON_ERROR_NONE
) {
86 require_once 'Zend/Json/Exception.php';
87 switch ($jsonLastErr) {
88 case JSON_ERROR_DEPTH
:
89 throw new Zend_Json_Exception('Decoding failed: Maximum stack depth exceeded');
90 case JSON_ERROR_CTRL_CHAR
:
91 throw new Zend_Json_Exception('Decoding failed: Unexpected control character found');
92 case JSON_ERROR_SYNTAX
:
93 throw new Zend_Json_Exception('Decoding failed: Syntax error');
95 throw new Zend_Json_Exception('Decoding failed');
102 require_once 'Zend/Json/Decoder.php';
103 return Zend_Json_Decoder
::decode($encodedValue, $objectDecodeType);
107 * Encode the mixed $valueToEncode into the JSON format
109 * Encodes using ext/json's json_encode() if available.
111 * NOTE: Object should not contain cycles; the JSON format
112 * does not allow object reference.
114 * NOTE: Only public variables will be encoded
116 * NOTE: Encoding native javascript expressions are possible using Zend_Json_Expr.
117 * You can enable this by setting $options['enableJsonExprFinder'] = true
119 * @see Zend_Json_Expr
121 * @param mixed $valueToEncode
122 * @param boolean $cycleCheck Optional; whether or not to check for object recursion; off by default
123 * @param array $options Additional options used during encoding
124 * @return string JSON encoded object
126 public static function encode($valueToEncode, $cycleCheck = false, $options = array())
128 if (is_object($valueToEncode) && method_exists($valueToEncode, 'toJson')) {
129 return $valueToEncode->toJson();
132 // Pre-encoding look for Zend_Json_Expr objects and replacing by tmp ids
133 $javascriptExpressions = array();
134 if(isset($options['enableJsonExprFinder'])
135 && ($options['enableJsonExprFinder'] == true)
138 * @see Zend_Json_Encoder
140 require_once "Zend/Json/Encoder.php";
141 $valueToEncode = self
::_recursiveJsonExprFinder($valueToEncode, $javascriptExpressions);
145 if (function_exists('json_encode') && self
::$useBuiltinEncoderDecoder !== true) {
146 $encodedResult = json_encode($valueToEncode);
148 require_once 'Zend/Json/Encoder.php';
149 $encodedResult = Zend_Json_Encoder
::encode($valueToEncode, $cycleCheck, $options);
152 //only do post-proccessing to revert back the Zend_Json_Expr if any.
153 if (count($javascriptExpressions) > 0) {
154 $count = count($javascriptExpressions);
155 for($i = 0; $i < $count; $i++
) {
156 $magicKey = $javascriptExpressions[$i]['magicKey'];
157 $value = $javascriptExpressions[$i]['value'];
159 $encodedResult = str_replace(
160 //instead of replacing "key:magicKey", we replace directly magicKey by value because "key" never changes.
161 '"' . $magicKey . '"',
168 return $encodedResult;
172 * Check & Replace Zend_Json_Expr for tmp ids in the valueToEncode
174 * Check if the value is a Zend_Json_Expr, and if replace its value
175 * with a magic key and save the javascript expression in an array.
177 * NOTE this method is recursive.
179 * NOTE: This method is used internally by the encode method.
182 * @param mixed $valueToCheck a string - object property to be encoded
185 protected static function _recursiveJsonExprFinder(
186 &$value, array &$javascriptExpressions, $currentKey = null
188 if ($value instanceof Zend_Json_Expr
) {
189 // TODO: Optimize with ascii keys, if performance is bad
190 $magicKey = "____" . $currentKey . "_" . (count($javascriptExpressions));
191 $javascriptExpressions[] = array(
193 //if currentKey is integer, encodeUnicodeString call is not required.
194 "magicKey" => (is_int($currentKey)) ?
$magicKey : Zend_Json_Encoder
::encodeUnicodeString($magicKey),
195 "value" => $value->__toString(),
198 } elseif (is_array($value)) {
199 foreach ($value as $k => $v) {
200 $value[$k] = self
::_recursiveJsonExprFinder($value[$k], $javascriptExpressions, $k);
202 } elseif (is_object($value)) {
203 foreach ($value as $k => $v) {
204 $value->$k = self
::_recursiveJsonExprFinder($value->$k, $javascriptExpressions, $k);
211 * fromXml - Converts XML to JSON
213 * Converts a XML formatted string into a JSON formatted string.
214 * The value returned will be a string in JSON format.
216 * The caller of this function needs to provide only the first parameter,
217 * which is an XML formatted String. The second parameter is optional, which
218 * lets the user to select if the XML attributes in the input XML string
219 * should be included or ignored in xml2json conversion.
221 * This function converts the XML formatted string into a PHP array by
222 * calling a recursive (protected static) function in this class. Then, it
223 * converts that PHP array into JSON by calling the "encode" static funcion.
225 * Throws a Zend_Json_Exception if the input not a XML formatted string.
226 * NOTE: Encoding native javascript expressions via Zend_Json_Expr is not possible.
230 * @param string $xmlStringContents XML String to be converted
231 * @param boolean $ignoreXmlAttributes Include or exclude XML attributes in
232 * the xml2json conversion process.
233 * @return mixed - JSON formatted string on success
234 * @throws Zend_Json_Exception
236 public static function fromXml ($xmlStringContents, $ignoreXmlAttributes=true) {
237 // Load the XML formatted string into a Simple XML Element object.
238 $simpleXmlElementObject = simplexml_load_string($xmlStringContents);
240 // If it is not a valid XML content, throw an exception.
241 if ($simpleXmlElementObject == null) {
242 require_once 'Zend/Json/Exception.php';
243 throw new Zend_Json_Exception('Function fromXml was called with an invalid XML formatted string.');
244 } // End of if ($simpleXmlElementObject == null)
248 // Call the recursive function to convert the XML into a PHP array.
249 $resultArray = self
::_processXml($simpleXmlElementObject, $ignoreXmlAttributes);
251 // Convert the PHP array to JSON using Zend_Json encode method.
252 // It is just that simple.
253 $jsonStringOutput = self
::encode($resultArray);
254 return($jsonStringOutput);
255 } // End of function fromXml.
258 * _processXml - Contains the logic for xml2json
260 * The logic in this function is a recursive one.
262 * The main caller of this function (i.e. fromXml) needs to provide
263 * only the first two parameters i.e. the SimpleXMLElement object and
264 * the flag for ignoring or not ignoring XML attributes. The third parameter
265 * will be used internally within this function during the recursive calls.
267 * This function converts the SimpleXMLElement object into a PHP array by
268 * calling a recursive (protected static) function in this class. Once all
269 * the XML elements are stored in the PHP array, it is returned to the caller.
271 * Throws a Zend_Json_Exception if the XML tree is deeper than the allowed limit.
275 * @param SimpleXMLElement $simpleXmlElementObject XML element to be converted
276 * @param boolean $ignoreXmlAttributes Include or exclude XML attributes in
277 * the xml2json conversion process.
278 * @param int $recursionDepth Current recursion depth of this function
279 * @return mixed - On success, a PHP associative array of traversed XML elements
280 * @throws Zend_Json_Exception
282 protected static function _processXml ($simpleXmlElementObject, $ignoreXmlAttributes, $recursionDepth=0) {
283 // Keep an eye on how deeply we are involved in recursion.
284 if ($recursionDepth > self
::$maxRecursionDepthAllowed) {
285 // XML tree is too deep. Exit now by throwing an exception.
286 require_once 'Zend/Json/Exception.php';
287 throw new Zend_Json_Exception(
288 "Function _processXml exceeded the allowed recursion depth of " .
289 self
::$maxRecursionDepthAllowed);
290 } // End of if ($recursionDepth > self::$maxRecursionDepthAllowed)
292 if ($recursionDepth == 0) {
293 // Store the original SimpleXmlElementObject sent by the caller.
294 // We will need it at the very end when we return from here for good.
295 $callerProvidedSimpleXmlElementObject = $simpleXmlElementObject;
296 } // End of if ($recursionDepth == 0)
298 if ($simpleXmlElementObject instanceof SimpleXMLElement
) {
299 // Get a copy of the simpleXmlElementObject
300 $copyOfSimpleXmlElementObject = $simpleXmlElementObject;
301 // Get the object variables in the SimpleXmlElement object for us to iterate.
302 $simpleXmlElementObject = get_object_vars($simpleXmlElementObject);
303 } // End of if (get_class($simpleXmlElementObject) == "SimpleXMLElement")
305 // It needs to be an array of object variables.
306 if (is_array($simpleXmlElementObject)) {
307 // Initialize a result array.
308 $resultArray = array();
309 // Is the input array size 0? Then, we reached the rare CDATA text if any.
310 if (count($simpleXmlElementObject) <= 0) {
311 // Let us return the lonely CDATA. It could even be
312 // an empty element or just filled with whitespaces.
313 return (trim(strval($copyOfSimpleXmlElementObject)));
314 } // End of if (count($simpleXmlElementObject) <= 0)
316 // Let us walk through the child elements now.
317 foreach($simpleXmlElementObject as $key=>$value) {
318 // Check if we need to ignore the XML attributes.
319 // If yes, you can skip processing the XML attributes.
320 // Otherwise, add the XML attributes to the result array.
321 if(($ignoreXmlAttributes == true) && (is_string($key)) && ($key == "@attributes")) {
323 } // End of if(($ignoreXmlAttributes == true) && ($key == "@attributes"))
325 // Let us recursively process the current XML element we just visited.
326 // Increase the recursion depth by one.
328 $resultArray[$key] = self
::_processXml ($value, $ignoreXmlAttributes, $recursionDepth);
330 // Decrease the recursion depth by one.
332 } // End of foreach($simpleXmlElementObject as $key=>$value) {
334 if ($recursionDepth == 0) {
335 // That is it. We are heading to the exit now.
336 // Set the XML root element name as the root [top-level] key of
337 // the associative array that we are going to return to the original
338 // caller of this recursive function.
339 $tempArray = $resultArray;
340 $resultArray = array();
341 $resultArray[$callerProvidedSimpleXmlElementObject->getName()] = $tempArray;
342 } // End of if ($recursionDepth == 0)
344 return($resultArray);
346 // We are now looking at either the XML attribute text or
347 // the text between the XML tags.
349 // In order to allow Zend_Json_Expr from xml, we check if the node
350 // matchs the pattern that try to detect if it is a new Zend_Json_Expr
351 // if it matches, we return a new Zend_Json_Expr instead of a text node
352 $pattern = '/^[\s]*new Zend_Json_Expr[\s]*\([\s]*[\"\']{1}(.*)[\"\']{1}[\s]*\)[\s]*$/';
353 $matchings = array();
354 $match = preg_match ($pattern, $simpleXmlElementObject, $matchings);
356 return new Zend_Json_Expr($matchings[1]);
358 return (trim(strval($simpleXmlElementObject)));
361 } // End of if (is_array($simpleXmlElementObject))
362 } // End of function _processXml.
365 * Pretty-print JSON string
367 * Use 'indent' option to select indentation string - by default it's a tab
369 * @param string $json Original JSON string
370 * @param array $options Encoding options
373 public static function prettyPrint($json, $options = array())
375 $tokens = preg_split('|([\{\}\]\[,])|', $json, -1, PREG_SPLIT_DELIM_CAPTURE
);
380 if(isset($options['indent'])) {
381 $ind = $options['indent'];
384 foreach($tokens as $token) {
385 if($token == "") continue;
387 $prefix = str_repeat($ind, $indent);
388 if($token == "{" ||
$token == "[") {
390 if($result != "" && $result[strlen($result)-1] == "\n") {
393 $result .= "$token\n";
394 } else if($token == "}" ||
$token == "]") {
396 $prefix = str_repeat($ind, $indent);
397 $result .= "\n$prefix$token";
398 } else if($token == ",") {
399 $result .= "$token\n";
401 $result .= $prefix.$token;