Repository navigation
Expand file tree
/
Copy pathjson.js
More file actions
153 lines (147 loc) · 5.78 KB
/
Copy pathjson.js
File metadata and controls
153 lines (147 loc) · 5.78 KB
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
/*
Do NOT use this version as it was slightly modified to accomodate editing and returning of functions!
use this version instead: http://www.JSON.org/json2.js
*/
if (!this.JSON) {
JSON = function () {
function f(n) {return n < 10 ? '0' + n : n;}
Date.prototype.toJSON = function () {
return this.getUTCFullYear() + '-' +
f(this.getUTCMonth() + 1) + '-' +
f(this.getUTCDate()) + 'T' +
f(this.getUTCHours()) + ':' +
f(this.getUTCMinutes()) + ':' +
f(this.getUTCSeconds()) + 'Z';
};
var meta = {
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',
'\r': '\\r',
'"' : '\\"',
'\\': '\\\\'
},
escapeable = /["\\\x00-\x1f\x7f-\x9f]/g;
function quote(string) {
return escapeable.test(string) ?
'"' + string.replace(escapeable, function (a) {
var c = meta[a];
if (typeof c === 'string') {
return c;
}
c = a.charCodeAt();
return '\\u00' + Math.floor(c / 16).toString(16) +
(c % 16).toString(16);
}) + '"' :
'"' + string + '"';
}
function stringify(key, holder, replacer) {
var i,
k,
v,
length,
partial,
value = holder[key];
if (value && typeof value === 'object' &&
typeof value.toJSON === 'function') {
value = value.toJSON(key);
}
if (typeof replacer === 'function') {
value = replacer.call(holder, key, value);
}
switch (typeof value) {
case 'string':
return quote(value);
/* added for the BC Json Editor to enable editing of functions */
case 'function':
return value;
case 'number':
return isFinite(value) ? String(value) : 'null';
case 'boolean':
case 'null':
return String(value);
case 'object':
if (!value) {
return 'null';
}
partial = [];
if (typeof value.length === 'number' &&
!(value.propertyIsEnumerable('length'))) {
length = value.length;
for (i = 0; i < length; i += 1) {
partial[i] = stringify(i, value, replacer) || 'null';
}
return '[' + partial.join(',') + ']';
}
if (typeof replacer === 'object') {
length = replacer.length;
for (i = 0; i < length; i += 1) {
k = replacer[i];
if (typeof k === 'string') {
v = stringify(k, value, replacer);
if (v) {
partial.push(quote(k) + ':' + v);
}
}
}
} else {
for (k in value) {
v = stringify(k, value, replacer);
if (v) {
partial.push(quote(k) + ':' + v);
}
}
}
return '{' + partial.join(',') + '}';
}
}
return {
stringify: function (value, replacer) {
if (typeof replacer !== 'function') {
if (!replacer) {
replacer = function (key, value) {
if (!Object.hasOwnProperty.call(this, key)) {
return undefined;
}
return value;
};
} else if (typeof replacer !== 'object' ||
typeof replacer.length !== 'number') {
throw new Error('JSON.stringify');
}
}
return stringify('', {'': value}, replacer);
},
parse: function (text, reviver) {
var j;
function walk(holder, key) {
var k, v, value = holder[key];
if (value && typeof value === 'object') {
for (k in value) {
if (Object.hasOwnProperty.call(value, k)) {
v = walk(value, k);
if (v !== undefined) {
value[k] = v;
} else {
delete value[k];
}
}
}
}
return reviver.call(holder, key, value);
}
if (/^[\],:{}\s]*$/.test(text.replace(/\\["\\\/bfnrtu]/g, '@').
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
j = eval('(' + text + ')');
return typeof reviver === 'function' ?
walk({'': j}, '') : j;
}
throw new SyntaxError('JSON.parse');
},
info:{"version":"","www":"http://www.json.org/","date":"2008-03-22","description":"Open source code of a JSON parser and JSON stringifier. [Douglas Crockford]"},
quote: quote
};
}();
}