Just starting working with the new Zend_Json::prettyPrint function which is new to Zend Framework 1.10. Was trying to output a json array to the browser in a nice 'pretty' format/indenting but found that when printed to the browser that Zend_Json::prettyPrint did nothing - it only works when viewing the output in a text editor
Below is the adjusted Zend_Json::prettyPrint function for work all nice when outputting json to the browser/html for viewing
-- attached and inline is the diff patch
/**
* Pretty-print JSON string
*
* Use 'format' option to select output format - currently html and txt supported, txt is default
* Use 'indent' option to override the indentation string set in the format - by default for the 'txt' format it's a tab
*
* @param string $json Original JSON string
* @param array $options Encoding options
* @return string
*/
public static function prettyPrint($json, $options = array())
{
$tokens = preg_split('|([\{\}\]\[,])|', $json, -1, PREG_SPLIT_DELIM_CAPTURE);
$result = "";
$indent = 0;
$format= "txt";
$ind = "\t";
if(isset($options['format'])) {
$format = $options['format'];
}
switch ($format):
case 'html':
$line_break = "<br />";
$ind = "\$nbsp;\$nbsp;\$nbsp;\$nbsp;";
break;
default:
case 'txt':
$line_break = "\n";
$ind = "\t";
break;
endswitch;
//override the defined indent setting with the supplied option
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] == $line_break) {
$result .= $prefix;
}
$result .= "$token$line_break";
} else if($token == "}" || $token == "]") {
$indent--;
$prefix = str_repeat($ind, $indent);
$result .= "$line_break$prefix$token";
} else if($token == ",") {
$result .= "$token$line_break" ;
} else {
$result .= $prefix.$token;
}
}
return $result;
}
}
SVN diff patch
Index: Json.php
===================================================================
--- Json.php (revision 21694)
+++ Json.php (working copy)
@@ -364,7 +364,8 @@
/**
* Pretty-print JSON string
*
- * Use 'indent' option to select indentation string - by default it's a tab
+ * Use 'format' option to select output format - currently html and txt supported, txt is default
+ * Use 'indent' option to override the indentation string set in the format - by default for the 'txt' format it's a tab
*
* @param string $json Original JSON string
* @param array $options Encoding options
@@ -375,28 +376,48 @@
$tokens = preg_split('|([\{\}\]\[,])|', $json, -1, PREG_SPLIT_DELIM_CAPTURE);
$result = "";
$indent = 0;
+
+ $format= "txt";
+
+ $ind = "\t";
- $ind = "\t";
+ if(isset($options['format'])) {
+ $format = $options['format'];
+ }
+
+ switch ($format):
+ case 'html':
+ $line_break = "<br />";
+ $ind = "\$nbsp;\$nbsp;\$nbsp;\$nbsp;";
+ break;
+ default:
+ case 'txt':
+ $line_break = "\n";
+ $ind = "\t";
+ break;
+ endswitch;
+
+ //override the defined indent setting with the supplied option
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") {
+ if($result != "" && $result[strlen($result)-1] == $line_break) {
$result .= $prefix;
}
- $result .= "$token\n";
+ $result .= "$token$line_break";
} else if($token == "}" || $token == "]") {
$indent--;
$prefix = str_repeat($ind, $indent);
- $result .= "\n$prefix$token";
+ $result .= "$line_break$prefix$token";
} else if($token == ",") {
- $result .= "$token\n";
+ $result .= "$token$line_break" ;
} else {
$result .= $prefix.$token;
} ;
Usage:
$message = Zend_Json::encode($data);
return Zend_Json::prettyPrint($message, array("format" => "html"));
Patch submitted to Zend Framework: http://framework.zend.com/issues/browse/ZF-9577