vvr's avatar
Level 1

json test outputs properly formated

I'm doing 'my first app' series and I see Jeffrey usually returns some json outcome to test something before even building views however I noticed in the screen cast the his outcomes in the browser are properly formated while mine are a mess ;) how can I get them prettier?

0 likes
4 replies
jimmck's avatar

In my own JSON output library I use this to clean up my JSON output. Warning, if the JSON coming in is bad it will BURP. So I wrap it an exception.

        /*
         * From GitHub  Gist - by - GloryFish, with enhancement by roktir.  For tabs.
         * Nice work!  Saved some time.
         * Need to move this JSON writer and upcoming XML writer out of this class.
         */
        static public function format_json($json, $html = false, $tabspaces = null)
        {
            $tabcount = 0;
            $result = '';
            $inquote = false;
            $ignorenext = false;
            if ($html) {
                $tab = str_repeat(" ", ($tabspaces == null ? 4 : $tabspaces));
                $newline = "<br/>";
            } else {
                $tab = ($tabspaces == null ? "\t" : str_repeat(" ", $tabspaces));
                $newline = "\n";
            }
            for ($i = 0; $i < strlen($json); $i++) {
                $char = $json[$i];
                if ($ignorenext) {
                    $result .= $char;
                    $ignorenext = false;
                } else {
                    switch ($char) {
                        case ':':
                            $result .= $char . (!$inquote ? " " : "");
                            break;
                        case '{':
                            if (!$inquote) {
                                $tabcount++;
                                $result .= $char . $newline . str_repeat($tab, $tabcount);
                            } else {
                                $result .= $char;
                            }
                            break;
                        case '}':
                            if (!$inquote) {
                                $tabcount--;
                                if ($tabcount < 0) {
                                    throw new JSONFormatException("Parse Error: $result");
                                }
                                $result = trim($result) . $newline . str_repeat($tab, $tabcount) . $char;
                            } else {
                                $result .= $char;
                            }
                            break;
                        case ',':
                            if (!$inquote) {
                                $result .= $char . $newline . str_repeat($tab, $tabcount);
                            } else {
                                $result .= $char;
                            }
                            break;
                        case '"':
                            $inquote = !$inquote;
                            $result .= $char;
                            break;
                        case '\\':
                            if ($inquote) {
                                $ignorenext = true;
                            }
                            $result .= $char;
                            break;
                        default:
                            $result .= $char;
                    }
                }
            }

            return $result;
        }

Change JSONExceptioin class.

Please or to participate in this conversation.