toJson(); } $javascriptExpressions = array(); if(isset($options['enableJsonExprFinder']) && ($options['enableJsonExprFinder'] == true) ) { require_once "Zend/Json/Encoder.php"; $valueToEncode = self::_recursiveJsonExprFinder($valueToEncode, $javascriptExpressions); } if (function_exists('json_encode') && self::$useBuiltinEncoderDecoder !== true) { $encodedResult = json_encode($valueToEncode); } else { require_once 'Zend/Json/Encoder.php'; $encodedResult = Zend_Json_Encoder::encode($valueToEncode, $cycleCheck, $options); } if (count($javascriptExpressions) > 0) { $count = count($javascriptExpressions); for($i = 0; $i < $count; $i++) { $magicKey = $javascriptExpressions[$i]['magicKey']; $value = $javascriptExpressions[$i]['value']; $encodedResult = str_replace( '"' . $magicKey . '"', $value, $encodedResult ); } } return $encodedResult; } protected static function _recursiveJsonExprFinder( &$value, array &$javascriptExpressions, $currentKey = null ) { if ($value instanceof Zend_Json_Expr) { $magicKey = "____" . $currentKey . "_" . (count($javascriptExpressions)); $javascriptExpressions[] = array( "magicKey" => (is_int($currentKey)) ? $magicKey : Zend_Json_Encoder::encodeUnicodeString($magicKey), "value" => $value->__toString(), ); $value = $magicKey; } elseif (is_array($value)) { foreach ($value as $k => $v) { $value[$k] = self::_recursiveJsonExprFinder($value[$k], $javascriptExpressions, $k); } } elseif (is_object($value)) { foreach ($value as $k => $v) { $value->$k = self::_recursiveJsonExprFinder($value->$k, $javascriptExpressions, $k); } } return $value; } public static function fromXml ($xmlStringContents, $ignoreXmlAttributes=true) { $simpleXmlElementObject = simplexml_load_string($xmlStringContents); if ($simpleXmlElementObject == null) { require_once 'Zend/Json/Exception.php'; throw new Zend_Json_Exception('Function fromXml was called with an invalid XML formatted string.'); } $resultArray = null; $resultArray = self::_processXml($simpleXmlElementObject, $ignoreXmlAttributes); $jsonStringOutput = self::encode($resultArray); return($jsonStringOutput); } protected static function _processXml ($simpleXmlElementObject, $ignoreXmlAttributes, $recursionDepth=0) { if ($recursionDepth > self::$maxRecursionDepthAllowed) { require_once 'Zend/Json/Exception.php'; throw new Zend_Json_Exception( "Function _processXml exceeded the allowed recursion depth of " . self::$maxRecursionDepthAllowed); } if ($recursionDepth == 0) { $callerProvidedSimpleXmlElementObject = $simpleXmlElementObject; } if ($simpleXmlElementObject instanceof SimpleXMLElement) { $copyOfSimpleXmlElementObject = $simpleXmlElementObject; $simpleXmlElementObject = get_object_vars($simpleXmlElementObject); } if (is_array($simpleXmlElementObject)) { $resultArray = array(); if (count($simpleXmlElementObject) <= 0) { return (trim(strval($copyOfSimpleXmlElementObject))); } foreach($simpleXmlElementObject as $key=>$value) { if(($ignoreXmlAttributes == true) && (is_string($key)) && ($key == "@attributes")) { continue; } $recursionDepth++; $resultArray[$key] = self::_processXml ($value, $ignoreXmlAttributes, $recursionDepth); $recursionDepth--; } if ($recursionDepth == 0) { $tempArray = $resultArray; $resultArray = array(); $resultArray[$callerProvidedSimpleXmlElementObject->getName()] = $tempArray; } return($resultArray); } else { $pattern = '/^[\s]*new Zend_Json_Expr[\s]*\([\s]*[\"\']{1}(.*)[\"\']{1}[\s]*\)[\s]*$/'; $matchings = array(); $match = preg_match ($pattern, $simpleXmlElementObject, $matchings); if ($match) { return new Zend_Json_Expr($matchings[1]); } else { return (trim(strval($simpleXmlElementObject))); } } } public static function prettyPrint($json, $options = array()) { $tokens = preg_split('|([\{\}\]\[,])|', $json, -1, PREG_SPLIT_DELIM_CAPTURE); $result = ""; $indent = 0; $ind = "\t"; if(isset($options['indent'])) { $ind = $options['indent']; } foreach($tokens as $token) { if($token == "") continue; $prefix = str_repeat($ind, $indent); if($token == "{" || $token == "[") { $indent++; if($result != "" && $result[strlen($result)-1] == "\n") { $result .= $prefix; } $result .= "$token\n"; } else if($token == "}" || $token == "]") { $indent--; $prefix = str_repeat($ind, $indent); $result .= "\n$prefix$token"; } else if($token == ",") { $result .= "$token\n"; } else { $result .= $prefix.$token; } } return $result; } }