zircote / Hal

A PHP implementation of HAL http://stateless.co/hal_specification.html
93 stars 6 forks source link

Hal Resource should have a method to enable JSON_PRETTY_PRINT #14

Open logistiker opened 11 years ago

logistiker commented 11 years ago

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;
}
baldurrensch commented 11 years ago

Isn't that what

print_r(json_decode($json));

would do?

logistiker commented 11 years ago

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.

hjr3 commented 10 years ago

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