-
Notifications
You must be signed in to change notification settings - Fork 1
/
JSON-js-prettyPrint.js
executable file
·162 lines (148 loc) · 5.11 KB
/
JSON-js-prettyPrint.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
json.js
2006-04-28
2006-05-27 added prettyPrint argument
This file adds these methods to JavaScript:
object.toJSONString(prettyPrint)
This method produces a JSON text from an object. The
object must not contain any cyclical references.
array.toJSONString(prettyPrint)
This method produces a JSON text from an array. The
array must not contain any cyclical references.
string.parseJSON()
This method parses a JSON text to produce an object or
array. It will return false if there is an error.
+ added prettyPrint argument
prettyPrint ... if set to true the resulting string will be formated
with tabs and returns to be more human readable.
*/
(function () {
// var INTEND = "\t";
// var NEWLINE = "\n";
var INTEND = " ";
var NEWLINE = "<br>";
var pPr = false;
var intendLevel = 0;
var intend = function(a) {
if (!pPr) return a;
for (var l=0; l<intendLevel; l++) {
a[a.length] = INTEND;
}
return a;
};
var newline = function(a) {
if (pPr) a[a.length] = NEWLINE;
return a;
};
var m = {
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',
'\r': '\\r',
'"' : '\\"',
'\\': '\\\\'
},
s = {
array: function (x) {
var a = ['['], b, f, i, l = x.length, v;
a = newline(a);
intendLevel++;
for (i = 0; i < l; i += 1) {
v = x[i];
f = s[typeof v];
if (f) {
v = f(v);
if (typeof v == 'string') {
if (b) {
a[a.length] = ',';
a = newline(a);
}
a = intend(a);
a[a.length] = v;
b = true;
}
}
}
intendLevel--;
a = newline(a);
a = intend(a);
a[a.length] = ']';
return a.join('');
},
'boolean': function (x) {
return String(x);
},
'null': function (x) {
return "null";
},
number: function (x) {
return isFinite(x) ? String(x) : 'null';
},
object: function (x, formatedOutput) {
if (x) {
if (x instanceof Array) {
return s.array(x);
}
var a = ['{'], b, f, i, v;
a = newline(a);
intendLevel++;
for (i in x) {
v = x[i];
f = s[typeof v];
if (f) {
v = f(v);
if (typeof v == 'string') {
if (b) {
a[a.length] = ',';
a = newline(a);
}
a = intend(a);
a.push(s.string(i), ((pPr) ? ' : ' : ':'), v);
b = true;
}
}
}
intendLevel--;
a = newline(a);
a = intend(a);
a[a.length] = '}';
return a.join('');
}
return 'null';
},
string: function (x) {
if (/["\\\x00-\x1f]/.test(x)) {
x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
var c = m[b];
if (c) {
return c;
}
c = b.charCodeAt();
return '\\u00' +
Math.floor(c / 16).toString(16) +
(c % 16).toString(16);
});
}
return '"' + x + '"';
}
};
Object.prototype.toJSONString = function (prettyPrint) {
pPr = prettyPrint;
return s.object(this);
};
Array.prototype.toJSONString = function (prettyPrint) {
pPr = prettyPrint;
return s.array(this);
};
})();
String.prototype.parseJSON = function () {
try {
return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
this.replace(/"(\\.|[^"\\])*"/g, ''))) &&
eval('(' + this + ')');
} catch (e) {
return false;
}
};