note 80339 added to function.json-encode
| From: | umbrae at gmail dot com | Date: | Thu, 10 Jan 2008 18:21:33 +0000 |
| Subject: | note 80339 added to function.json-encode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-135010@lists.php.net to get a copy of this message | ||
A couple bug fixes to my own code.. heh.
1. [] would return false before, because an empty array() == false. Needed ===.
2. Backslashed quotes would mess up formatting. Fixed.
// Pretty print some JSON
function json_format($json)
{
$tab = " ";
$new_json = "";
$indent_level = 0;
$in_string = false;
$json_obj = json_decode($json);
if($json_obj === false)
return false;
$json = json_encode($json_obj);
$len = strlen($json);
for($c = 0; $c < $len; $c++)
{
$char = $json[$c];
switch($char)
{
case '{':
case '[':
if(!$in_string)
{
$new_json .= $char . "\n" . str_repeat($tab, $indent_level+1);
$indent_level++;
}
else
{
$new_json .= $char;
}
break;
case '}':
case ']':
if(!$in_string)
{
$indent_level--;
$new_json .= "\n" . str_repeat($tab, $indent_level) . $char;
}
else
{
$new_json .= $char;
}
break;
case ',':
if(!$in_string)
{
$new_json .= ",\n" . str_repeat($tab, $indent_level);
}
else
{
$new_json .= $char;
}
break;
case ':':
if(!$in_string)
{
$new_json .= ": ";
}
else
{
$new_json .= $char;
}
break;
case '"':
if($c > 0 && $json[$c-1] != '\\')
{
$in_string = !$in_string;
}
default:
$new_json .= $char;
break;
}
}
return $new_json;
}
----
Server IP: 69.147.83.197
Probable Submitter: 38.98.105.130
----
Manual Page -- http://www.php.net/manual/en/function.json-encode.php
Edit -- https://master.php.net/note/edit/80339
Del: integrated -- https://master.php.net/note/delete/80339/integrated
Del: useless -- https://master.php.net/note/delete/80339/useless
Del: bad code -- https://master.php.net/note/delete/80339/bad+code
Del: spam -- https://master.php.net/note/delete/80339/spam
Del: non-english -- https://master.php.net/note/delete/80339/non-english
Del: in docs -- https://master.php.net/note/delete/80339/in+docs
Del: other reasons-- https://master.php.net/note/delete/80339
Reject -- https://master.php.net/note/reject/80339
Search -- https://master.php.net/manage/user-notes.php