-
Notifications
You must be signed in to change notification settings - Fork 2
/
json.js
242 lines (240 loc) · 9.06 KB
/
json.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// # json.js
// TurtleScript JSON.stringify implementation, written in TurtleScript.
//
// Not used in TurtleScript compiler/interpreters themselves, but often
// useful in the resulting environments. (Alternatively, a native
// implementation can be substituted.)
define(['text!json.js'], function make_json(json_source) {
var init = /*CUT HERE*/function _make_json() {
var ArrayIsArray = Array.isArray;
var hasOwnProperty = Object.prototype.hasOwnProperty;
var StringSlice = String.prototype.slice;
var ObjectCreate = Object.create;
var ObjectKeys = Object.keys;
var NumberIsFinite = Number.isFinite;
var ArrayJoin = Array.prototype.join;
var ArrayForEach = Array.prototype.forEach;
var ToIntegerOrInfinity = function _ToIntegerOrInfinity(n) {
// ToNumber
n = typeof Number === 'function' ? Number(n) : n;
if (n !== n || n === 0) { return 0; /* NaN, +0, -0 */ }
if (n === Infinity || n === -Infinity) { return n; }
var negate = n < 0;
n = Math.floor(negate ? (-n) : n);
return negate ? (-n) : n;
};
var escapes = [];
escapes[0x8] = "\\b";
escapes[0x9] = "\\t";
escapes[0xA] = "\\n";
escapes[0xC] = "\\f";
escapes[0xD] = "\\r";
escapes[0x22] = "\\\"";
escapes[0x5C] = "\\\\";
var UnicodeEscape = function _UnicodeEscape(C) {
var hex = C.toString(16);
while (hex.length < 4) {
hex = "0" + hex;
}
return "\\u" + hex;
};
var QuoteJSONString = function _QuoteJSONString(value) {
var product = "\"";
var i = 0;
var len = value.length;
while (i < len) {
var c = value.charCodeAt(i);
var e = escapes[c];
i += 1;
if (e !== undefined) {
product += e;
} else if (c < 0x20) {
product += UnicodeEscape(c);
} else if (c >= 0xD800 && c <= 0xDBFF) {
var second = value.charCodeAt(i);
if (second === second && // NaN indicates size >= i+1
second >= 0xDC00 && second <= 0xDFFF) {
// low surrogate
product += String.fromCharCode(c);
product += String.fromCharCode(second);
i += 1;
} else {
// invalid second surrogate, this is "leading surrogate"
product += UnicodeEscape(c);
}
} else if (c >= 0xDC00 && c <= 0xDFFF) {
// this is a "trailing surrogate"
product += UnicodeEscape(c);
} else {
product += String.fromCharCode(c);
}
}
product += "\"";
return product;
};
var SerializeJSONObject;
var SerializeJSONArray;
var SerializeJSONProperty;
SerializeJSONObject = function _SerializeJSONObject(state, value) {
// XXX not checking for cyclic structures
var stepback = state.indent;
state.indent += state.gap;
var K = state.propertyList;
if (K === undefined) {
K = ObjectKeys.call(Object, value);
}
var partial = [];
ArrayForEach.call(K, function(P) {
var strP = SerializeJSONProperty(state, P, value);
if (strP === undefined) {
return;
}
var member = QuoteJSONString(P) + ":";
if (state.gap !== "") {
member += " ";
}
member += strP;
partial.push(member);
});
var final = "{}";
if (partial.length) {
if (state.gap === "") {
final = "{" + ArrayJoin.call(partial, ",") + "}";
} else {
var separator = ",\n" + state.indent;
final = "{\n" + state.indent +
ArrayJoin.call(partial, separator) + "\n" +
stepback + "}";
}
}
state.indent = stepback;
return final;
};
SerializeJSONArray = function _SerializeJSONArray(state, value) {
// XXX not checking for cyclic structures
var stepback = state.indent;
state.indent += state.gap;
var partial = [];
var len = value.length;
var index = 0;
while (index < len) {
var strP = SerializeJSONProperty(state, String(index), value);
if (strP === undefined) {
partial.push("null");
} else {
partial.push(strP);
}
index += 1;
}
var final = "[]";
if (partial.length) {
if (state.gap === "") {
final = "[" + ArrayJoin.call(partial, ",") + "]";
} else {
var separator = ",\n" + state.indent;
final = "[\n" + state.indent +
ArrayJoin.call(partial, separator) + "\n" +
stepback + "]";
}
}
state.indent = stepback;
return final;
};
SerializeJSONProperty = function _SerializeJSONProperty(state, key, holder) {
var value = holder[key];
if (value!==null) {
var toJSON = value.toJSON;
if (typeof(toJSON) === "function") {
value = toJSON.call(value, key);
}
}
if (state.replacerFunction !== undefined) {
value = state.replacerFunction.call(holder, key, value);
}
// XXX Objects which are boxed primitives are converted to the
// primitives here. Instead maybe just define Number..toJSON?
if (value === null || value === true || value === false) {
return String(value);
}
if (typeof(value)==='string') {
return QuoteJSONString(value);
}
if (typeof(value)==='number') {
if (NumberIsFinite.call(Number, value)) {
return String(value);
}
return "null";
}
if (ArrayIsArray.call(Array, value)) {
return SerializeJSONArray(state, value);
}
if (typeof(value) === 'object') {
return SerializeJSONObject(state, value);
}
return undefined;
};
JSON.stringify = function(value, replacer, space) {
var propertyList = undefined;
var replacerFunction = undefined;
var gap = "";
if (typeof(replacer) === 'function') {
replacerFunction = replacer;
} else if (ArrayIsArray.call(Array, replacer)) {
propertyList = [];
var len = replacer.length;
var k = 0;
var seen = ObjectCreate.call(Object, null);
while (k < len) {
var v = String(replacer[k]); // maybe not exactly
if (!hasOwnProperty.call(seen, '$' + v)) {
propertyList.push(v);
seen['$' + v] = true;
}
k += 1;
}
}
if (typeof(space) === 'number') {
var spaceMV = ToIntegerOrInfinity(space);
if (spaceMV > 10) {
spaceMV = 10;
}
while (spaceMV >= 1) {
gap = gap + " ";
spaceMV -= 1;
}
} else if (typeof(space) === 'string') {
gap = StringSlice.call(space, 0, 10);
}
var wrapper = ObjectCreate.call(Object, null);
wrapper[""] = value;
var state = {
replacerFunction: replacerFunction,
stack: [],
indent: "",
gap: gap,
propertyList: propertyList
};
return SerializeJSONProperty(state, "", wrapper);
};
}/*CUT HERE*/;
// Define a helper function to turn the `json.init` function into the
// source text of an evaluatable statement expression.
var source = function() {
var s;
if (json_source) {
s = json_source.split('/*CUT HERE*/')[1];
} else {
s = init.toSource ? init.toSource() : init.toString();
}
s = '(' + s + ')();';
return s;
};
return {
__module_name__: 'json',
__module_init__: make_json,
__module_deps__: [],
__module_source__: json_source,
init: init,
source: source
};
});