├── .gitattributes ├── .gitignore ├── .htaccess ├── README.mdown ├── example-1.php ├── example.json └── nicejson.php /.gitattributes: -------------------------------------------------------------------------------- 1 | *.sh text eol=lf 2 | *.php text eol=lf 3 | *.inc text eol=lf 4 | *.html text eol=lf 5 | *.js text eol=lf 6 | *.css text eol=lf 7 | *.ini text eol=lf 8 | *.txt text eol=lf 9 | *.xml text eol=lf 10 | *.md text eol=lf 11 | *.markdown text eol=lf 12 | 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Apple MAC cruft 2 | .DS_Store 3 | 4 | # Editor backup files 5 | *.bak 6 | *~ 7 | -------------------------------------------------------------------------------- /.htaccess: -------------------------------------------------------------------------------- 1 | ######################################## 2 | # Locale settings 3 | ######################################## 4 | 5 | # See: http://php.net/manual/en/timezones.php 6 | php_value date.timezone "Europe/Amsterdam" 7 | 8 | SetEnv LC_ALL nl_NL.UTF-8 9 | 10 | ######################################## 11 | # Set up UTF-8 encoding 12 | ######################################## 13 | 14 | AddDefaultCharset UTF-8 15 | AddCharset UTF-8 .php 16 | 17 | php_value default_charset "UTF-8" 18 | 19 | php_value iconv.input_encoding "UTF-8" 20 | php_value iconv.internal_encoding "UTF-8" 21 | php_value iconv.output_encoding "UTF-8" 22 | 23 | php_value mbstring.internal_encoding UTF-8 24 | php_value mbstring.http_output UTF-8 25 | php_value mbstring.encoding_translation On 26 | php_value mbstring.func_overload 6 27 | 28 | # See also php functions: 29 | # mysql_set_charset 30 | # mysql_client_encoding 31 | 32 | # database settings 33 | #CREATE DATABASE db_name 34 | # CHARACTER SET utf8 35 | # DEFAULT CHARACTER SET utf8 36 | # COLLATE utf8_general_ci 37 | # DEFAULT COLLATE utf8_general_ci 38 | # ; 39 | # 40 | #ALTER DATABASE db_name 41 | # CHARACTER SET utf8 42 | # DEFAULT CHARACTER SET utf8 43 | # COLLATE utf8_general_ci 44 | # DEFAULT COLLATE utf8_general_ci 45 | # ; 46 | 47 | #ALTER TABLE tbl_name 48 | # DEFAULT CHARACTER SET utf8 49 | # COLLATE utf8_general_ci 50 | # ; -------------------------------------------------------------------------------- /README.mdown: -------------------------------------------------------------------------------- 1 | # NiceJSON-PHP 2 | 3 | Small lib to nicely format JSON strings with PHP 4 | 5 | ## Credits 6 | 7 | * recursive-design.com: http://recursive-design.com/blog/2008/03/11/format-json-with-php/ 8 | -------------------------------------------------------------------------------- /example-1.php: -------------------------------------------------------------------------------- 1 | 17 | 18 | 19 | 20 | 21 |

JSON dump

22 |
23 | EOT;
24 | 
25 | // Example usage 
26 | 
27 | 
28 | $file = dirname(__FILE__) . '/example.json';
29 | $json = file_get_contents($file);
30 | // strip off optional Unicode BOM:
31 | if (substr($json, 0, 3) == "\xEF\xBB\xBF") {
32 |   $json = substr($json, 3);
33 | }
34 | echo htmlspecialchars(json_format($json));
35 | 
36 | 


--------------------------------------------------------------------------------
/example.json:
--------------------------------------------------------------------------------
1 | {"hey": "guy","anumber": 243,"anobject": {"whoa": "nuts","anarray": [1,2,"thr

ee"], "more":"stuff"},"awesome": true,"bogus": false,"meaning": null, "japanese":"明日がある。", "link": "http://jsonview.com", "notLink": "http://jsonview.com is great", "colon-ed-item":"text: \"that was a colon!\""} -------------------------------------------------------------------------------- /nicejson.php: -------------------------------------------------------------------------------- 1 | = 5.4.0 5 | 6 | /** 7 | * Format a flat JSON string to make it more human-readable 8 | * 9 | * @param string $json The original JSON string to process 10 | * When the input is not a string it is assumed the input is RAW 11 | * and should be converted to JSON first of all. 12 | * @return string Indented version of the original JSON string 13 | */ 14 | function json_format($json) { 15 | if (!is_string($json)) { 16 | if (phpversion() && phpversion() >= 5.4) { 17 | return json_encode($json, JSON_PRETTY_PRINT); 18 | } 19 | $json = json_encode($json); 20 | } 21 | $result = ''; 22 | $pos = 0; // indentation level 23 | $strLen = strlen($json); 24 | $indentStr = "\t"; 25 | $newLine = "\n"; 26 | $prevChar = ''; 27 | $outOfQuotes = true; 28 | 29 | for ($i = 0; $i < $strLen; $i++) { 30 | // Speedup: copy blocks of input which don't matter re string detection and formatting. 31 | $copyLen = strcspn($json, $outOfQuotes ? " \t\r\n\",:[{}]" : "\\\"", $i); 32 | if ($copyLen >= 1) { 33 | $copyStr = substr($json, $i, $copyLen); 34 | // Also reset the tracker for escapes: we won't be hitting any right now 35 | // and the next round is the first time an 'escape' character can be seen again at the input. 36 | $prevChar = ''; 37 | $result .= $copyStr; 38 | $i += $copyLen - 1; // correct for the for(;;) loop 39 | continue; 40 | } 41 | 42 | // Grab the next character in the string 43 | $char = substr($json, $i, 1); 44 | 45 | // Are we inside a quoted string encountering an escape sequence? 46 | if (!$outOfQuotes && $prevChar === '\\') { 47 | // Add the escaped character to the result string and ignore it for the string enter/exit detection: 48 | $result .= $char; 49 | $prevChar = ''; 50 | continue; 51 | } 52 | // Are we entering/exiting a quoted string? 53 | if ($char === '"' && $prevChar !== '\\') { 54 | $outOfQuotes = !$outOfQuotes; 55 | } 56 | // If this character is the end of an element, 57 | // output a new line and indent the next line 58 | else if ($outOfQuotes && ($char === '}' || $char === ']')) { 59 | $result .= $newLine; 60 | $pos--; 61 | for ($j = 0; $j < $pos; $j++) { 62 | $result .= $indentStr; 63 | } 64 | } 65 | // eat all non-essential whitespace in the input as we do our own here and it would only mess up our process 66 | else if ($outOfQuotes && false !== strpos(" \t\r\n", $char)) { 67 | continue; 68 | } 69 | 70 | // Add the character to the result string 71 | $result .= $char; 72 | // always add a space after a field colon: 73 | if ($outOfQuotes && $char === ':') { 74 | $result .= ' '; 75 | } 76 | 77 | // If the last character was the beginning of an element, 78 | // output a new line and indent the next line 79 | else if ($outOfQuotes && ($char === ',' || $char === '{' || $char === '[')) { 80 | $result .= $newLine; 81 | if ($char === '{' || $char === '[') { 82 | $pos++; 83 | } 84 | for ($j = 0; $j < $pos; $j++) { 85 | $result .= $indentStr; 86 | } 87 | } 88 | $prevChar = $char; 89 | } 90 | 91 | return $result; 92 | } 93 | 94 | --------------------------------------------------------------------------------