Skip to content

Instantly share code, notes, and snippets.

@mdrakibhossainhawlader
Created July 9, 2018 00:50
Show Gist options
  • Save mdrakibhossainhawlader/243ebec30b86d8ab1df22d755357e10e to your computer and use it in GitHub Desktop.
Save mdrakibhossainhawlader/243ebec30b86d8ab1df22d755357e10e to your computer and use it in GitHub Desktop.

Revisions

  1. @sepehr sepehr revised this gist Jun 23, 2014. 1 changed file with 13 additions and 7 deletions.
    20 changes: 13 additions & 7 deletions json_prettyprint.php
    Original file line number Diff line number Diff line change
    @@ -14,6 +14,12 @@ function json_prettyprint($json)
    return json_encode($json, JSON_PRETTY_PRINT);
    }

    // Array passed?
    if (is_array($json))
    {
    $json = json_encode($json);
    }

    $result = '';
    $level = 0;
    $in_quotes = FALSE;
    @@ -36,25 +42,25 @@ function json_prettyprint($json)
    if ($in_escape)
    {
    $in_escape = false;
    }
    }

    else if ($char === '"')
    {
    $in_quotes = !$in_quotes;
    }
    }

    else if (! $in_quotes)
    {
    switch ($char)
    {
    case '}':
    case '}':
    case ']':
    $level--;
    $ends_line_level = NULL;
    $new_line_level = $level;
    break;

    case '{':
    case '{':
    case '[':
    $level++;

    @@ -66,9 +72,9 @@ function json_prettyprint($json)
    $post = " ";
    break;

    case " ":
    case "\t":
    case "\n":
    case " ":
    case "\t":
    case "\n":
    case "\r":
    $char = "";
    $ends_line_level = $new_line_level;
  2. @sepehr sepehr created this gist Jun 23, 2014.
    94 changes: 94 additions & 0 deletions json_prettyprint.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,94 @@
    <?php

    /**
    * Pretty print JSON objects.
    *
    * @param array $json JSON array to be printed.
    *
    * @return string
    */
    function json_prettyprint($json)
    {
    if (defined("JSON_PRETTY_PRINT"))
    {
    return json_encode($json, JSON_PRETTY_PRINT);
    }

    $result = '';
    $level = 0;
    $in_quotes = FALSE;
    $in_escape = FALSE;
    $ends_line_level = NULL;
    $json_length = strlen($json);

    for ($i = 0; $i < $json_length; $i++)
    {
    $post = "";
    $new_line_level = NULL;
    $char = $json[$i];

    if ($ends_line_level !== NULL)
    {
    $new_line_level = $ends_line_level;
    $ends_line_level = NULL;
    }

    if ($in_escape)
    {
    $in_escape = false;
    }

    else if ($char === '"')
    {
    $in_quotes = !$in_quotes;
    }

    else if (! $in_quotes)
    {
    switch ($char)
    {
    case '}':
    case ']':
    $level--;
    $ends_line_level = NULL;
    $new_line_level = $level;
    break;

    case '{':
    case '[':
    $level++;

    case ',':
    $ends_line_level = $level;
    break;

    case ':':
    $post = " ";
    break;

    case " ":
    case "\t":
    case "\n":
    case "\r":
    $char = "";
    $ends_line_level = $new_line_level;
    $new_line_level = NULL;
    break;
    } // switch
    } // if

    else if ($char === '\\')
    {
    $in_escape = TRUE;
    }

    if ($new_line_level !== NULL)
    {
    $result .= "\n" . str_repeat("\t", $new_line_level);
    }

    $result .= $char . $post;
    }

    return $result;
    }