Issues

ZF-9577: Zend_Json::prettyPrint edited for browser/html output

Issue Type: Patch Created: 2010-03-30T21:07:07.000+0000 Last Updated: 2011-08-27T17:19:46.000+0000 Status: Resolved Fix version(s): - 1.11.11 (29/Sep/11)

Reporter: Justin Kelly (justinkelly) Assignee: Ralph Schindler (ralph) Tags: - Zend_Json

  • zf-crteam-padraic
  • zf-crteam-priority

Related issues: Attachments: - Zend_Json_svn_diff.diff

Description

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 -- inline is the diff patch

<pre class="highlight">
    /**
     * 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></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

<pre class="highlight">
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></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:

<pre class="highlight">
$message = Zend_Json::encode($data);
return Zend_Json::prettyPrint($message, array("format" => "html"));

refer: http://blog.justin.kelly.org.au/zendjsonprettyprin…

Comments

Posted by Justin Kelly (justinkelly) on 2010-03-30T21:07:58.000+0000

diff now attached

Posted by Ralph Schindler (ralph) on 2011-08-27T17:19:46.000+0000

Fixed in trunk in r24412. in r24413 in release branch 1.11

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts