1 | <?php
|
---|
2 | /**
|
---|
3 | * PHP Integration of Open Flash Chart
|
---|
4 | * Copyright (C) 2008 John Glazebrook <open-flash-chart@teethgrinder.co.uk>
|
---|
5 | *
|
---|
6 | * This library is free software; you can redistribute it and/or
|
---|
7 | * modify it under the terms of the GNU Lesser General Public
|
---|
8 | * License as published by the Free Software Foundation; either
|
---|
9 | * version 2.1 of the License, or (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This library is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | * Lesser General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU Lesser General Public
|
---|
17 | * License along with this library; if not, write to the Free Software
|
---|
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
19 | */
|
---|
20 |
|
---|
21 | // Pretty print some JSON
|
---|
22 | function json_format($json)
|
---|
23 | {
|
---|
24 | $tab = " ";
|
---|
25 | $new_json = "";
|
---|
26 | $indent_level = 0;
|
---|
27 | $in_string = false;
|
---|
28 |
|
---|
29 | /*
|
---|
30 | commented out by monk.e.boy 22nd May '08
|
---|
31 | because my web server is PHP4, and
|
---|
32 | json_* are PHP5 functions...
|
---|
33 |
|
---|
34 | $json_obj = json_decode($json);
|
---|
35 |
|
---|
36 | if($json_obj === false)
|
---|
37 | return false;
|
---|
38 |
|
---|
39 | $json = json_encode($json_obj);
|
---|
40 | */
|
---|
41 | $len = strlen($json);
|
---|
42 |
|
---|
43 | for($c = 0; $c < $len; $c++)
|
---|
44 | {
|
---|
45 | $char = $json[$c];
|
---|
46 | switch($char)
|
---|
47 | {
|
---|
48 | case '{':
|
---|
49 | case '[':
|
---|
50 | if(!$in_string)
|
---|
51 | {
|
---|
52 | $new_json .= $char . "\n" . str_repeat($tab, $indent_level+1);
|
---|
53 | $indent_level++;
|
---|
54 | }
|
---|
55 | else
|
---|
56 | {
|
---|
57 | $new_json .= $char;
|
---|
58 | }
|
---|
59 | break;
|
---|
60 | case '}':
|
---|
61 | case ']':
|
---|
62 | if(!$in_string)
|
---|
63 | {
|
---|
64 | $indent_level--;
|
---|
65 | $new_json .= "\n" . str_repeat($tab, $indent_level) . $char;
|
---|
66 | }
|
---|
67 | else
|
---|
68 | {
|
---|
69 | $new_json .= $char;
|
---|
70 | }
|
---|
71 | break;
|
---|
72 | case ',':
|
---|
73 | if(!$in_string)
|
---|
74 | {
|
---|
75 | $new_json .= ",\n" . str_repeat($tab, $indent_level);
|
---|
76 | }
|
---|
77 | else
|
---|
78 | {
|
---|
79 | $new_json .= $char;
|
---|
80 | }
|
---|
81 | break;
|
---|
82 | case ':':
|
---|
83 | if(!$in_string)
|
---|
84 | {
|
---|
85 | $new_json .= ": ";
|
---|
86 | }
|
---|
87 | else
|
---|
88 | {
|
---|
89 | $new_json .= $char;
|
---|
90 | }
|
---|
91 | break;
|
---|
92 | case '"':
|
---|
93 | if($c > 0 && $json[$c-1] != '\\')
|
---|
94 | {
|
---|
95 | $in_string = !$in_string;
|
---|
96 | }
|
---|
97 | default:
|
---|
98 | $new_json .= $char;
|
---|
99 | break;
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | return $new_json;
|
---|
104 | }
|
---|
105 |
|
---|