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 = "
"; + $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; }