nicejson.php in Ultimate Cron 7.2
File
includes/nicejson.php
View source
<?php
function json_format($json) {
if (!is_string($json)) {
if (phpversion() && phpversion() >= 5.4) {
return json_encode($json, JSON_PRETTY_PRINT);
}
$json = json_encode($json);
}
$result = '';
$pos = 0;
$strLen = strlen($json);
$indentStr = "\t";
$newLine = "\n";
$prevChar = '';
$outOfQuotes = true;
for ($i = 0; $i < $strLen; $i++) {
$char = substr($json, $i, 1);
if ($char == '"' && $prevChar != '\\') {
$outOfQuotes = !$outOfQuotes;
}
else {
if (($char == '}' || $char == ']') && $outOfQuotes) {
$result .= $newLine;
$pos--;
for ($j = 0; $j < $pos; $j++) {
$result .= $indentStr;
}
}
else {
if ($outOfQuotes && false !== strpos(" \t\r\n", $char)) {
continue;
}
}
}
$result .= $char;
if ($char == ':' && $outOfQuotes) {
$result .= ' ';
}
if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) {
$result .= $newLine;
if ($char == '{' || $char == '[') {
$pos++;
}
for ($j = 0; $j < $pos; $j++) {
$result .= $indentStr;
}
}
$prevChar = $char;
}
return $result;
}
Functions
Name |
Description |
json_format |
Format a flat JSON string to make it more human-readable |