Hal icon indicating copy to clipboard operation
Hal copied to clipboard

Hal Resource should have a method to enable JSON_PRETTY_PRINT

Open logistiker opened this issue 12 years ago • 3 comments

Have some pity on us humans who find it hard to read everything on one line:

/**
 * Enable json pretty print
 *
 * @var boolean
 */
private $printPrint = false;

/**
 * Set pretty print
 *
 * @param boolean $prettyPrint
 */
public setPrettyPrint($prettyPrint)
{
    $this->prettyPrint = $prettyPrint;
}

/**
 * Get pretty print
 *
 * @return $prettyPrint
 */
public getPrettyPrint()
{
    return $this->prettyPrint;
}

/**
 * Get human-readable json from a one-liner json string
 *
 * @return string human-readable json
 */
public function __toJson()
{
    if (! $this->prettyPrint || $this->prettyPrint && ! defined(JSON_PRETTY_PRINT)) {
        if (defined(JSON_NUMERIC_CHECK) && $this->jsonNumericCheck) {
            $json = json_encode($this->toArray(), JSON_NUMERIC_CHECK);
        } else {
            $json = json_encode($this->toArray());
        }
        if (! $this->prettyPrint) {
            return $json;
        }
    }

    // pretty print if requested
    //json pretty printing is supported in php >=5.4
    if (defined(JSON_PRETTY_PRINT)) {
        if (defined(JSON_NUMERIC_CHECK) && $this->jsonNumericCheck) {
            return json_encode($this->toArray(), JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT);
        }
        return json_encode($this->toArray(), JSON_PRETTY_PRINT);
    }
    $tab = '  ';
    $newJson = '';
    $indentLevel = 0;
    $inString = false;

    $len = strlen($json);
    for ($c = 0; $c < $len; $c++) {
        $char = $json[$c];
        switch($char) {
            case '{':
            case '[':
                if (! $inString) {
                    $newJson .= $char . "\n" . str_repeat($tab, $indentLevel + 1);
                    $indentLevel++;
                } else {
                    $newJson .= $char;
                }
                break;
            case '}':
            case ']':
                if (! $inString) {
                    $indentLevel--;
                    $newJson .= "\n" . str_repeat($tab, $indentLevel) . $char;
                } else {
                    $newJson .= $char;
                }
                break;
            case ',':
                if (! $inString) {
                    $newJson .= ",\n" . str_repeat($tab, $indentLevel);
                } else {
                    $newJson .= $char;
                }
                break;
            case ':':
                if (! $inString) {
                    $newJson .= ": ";
                } else {
                    $newJson .= $char;
                }
                break;
            case '"':
                if ($c > 0 && $json[$c - 1] != '\\') {
                    $inString = ! $inString;
                }
            default:
                $newJson .= $char;
                break;
        }
    }
    return $newJson;
}

logistiker avatar Jul 17 '13 18:07 logistiker

Isn't that what

print_r(json_decode($json));

would do?

baldurrensch avatar Jul 18 '13 00:07 baldurrensch

That is most definitely not the same thing. Nowhere in my code am I decoding the json and nor would they add a json_encode option to pretty print it if there already was a simple method to do this. The point is to display the same output but tabbed and with carriage returns to make it human readable but not to change it so drastically that it could no longer be processed by a program. This is especially useful for testing purposes to inspect the output to ensure it's correct.

logistiker avatar Jul 18 '13 04:07 logistiker

I am happy to add this in if we accept https://github.com/zircote/Hal/issues/18

hjr3 avatar Oct 28 '14 05:10 hjr3