This repository has been archived by the owner on Feb 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathACT_json.js
456 lines (425 loc) · 14.4 KB
/
ACT_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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
* Copyright 2016, Yahoo Inc.
* Copyrights licensed under the New BSD License.
* See the accompanying LICENSE file for terms.
*
* Most of the code in this file uses Public Domain code from https://github.com/douglascrockford/JSON-js/blob/master/json_parse.js
*
*/
/* global ACT */
/* eslint consistent-return: 0, no-nested-ternary: 0, default-case: 0, no-useless-escape: 0 */
ACT.define('Json', [/*@<*/'Debug'/*>@*/], function (ACT) {
'use strict';
/**
* JSON encode functionality.
*
* @type Mixed
* @private
* @final
* @static
*/
var escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
var gap;
var indent;
var meta = { '\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"': '\\"', '\\': '\\\\' };
var rep;
var jsonParse;
/*@<*/
var Debug = ACT.Debug;
Debug.log('[ ACT_Json.js ]: loaded');
/*>@*/
/**
* @method quote
* @param {String} string
* @private
*/
function quote(string) {
escapable.lastIndex = 0;
return escapable.test(string) ? '"' + string.replace(escapable, function (a) {
var c = meta[a];
return typeof c === 'string' ? c :
'\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
}) + '"' : '"' + string + '"';
}
/**
* @private
* @method str
* @param {String} key
* @param {Object} holder
*/
function str(key, holder) {
// Let's allow this special function to have more complexity than normal
/* jshint maxcomplexity:false */
var i;
var k;
var v;
var length;
var mind = gap;
var partial;
var value = holder[key];
if (value && typeof value === 'object' && typeof value.toJSON === 'function') {
value = value.toJSON(key);
}
if (typeof rep === 'function') {
value = rep.call(holder, key, value);
}
switch (typeof value) {
case 'string':
return quote(value);
case 'number':
return isFinite(value) ? String(value) : 'null';
case 'boolean':
case 'null':
return String(value);
case 'object':
if (!value) { return 'null'; }
gap += indent;
partial = [];
if (Object.prototype.toString.apply(value) === '[object Array]') {
length = value.length;
for (i = 0; i < length; i += 1) {
partial[i] = str(i, value) || 'null';
}
v = partial.length === 0 ? '[]' : gap ?
'[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']' :
'[' + partial.join(',') + ']';
gap = mind;
return v;
}
if (rep && typeof rep === 'object') {
length = rep.length;
for (i = 0; i < length; i += 1) {
k = rep[i];
if (typeof k === 'string') {
v = str(k, value);
if (v) {
partial.push(quote(k) + (gap ? ': ' : ':') + v);
}
}
}
} else {
for (k in value) {
/* istanbul ignore else */
if (Object.prototype.hasOwnProperty.call(value, k)) {
v = str(k, value);
/* istanbul ignore else */
if (v) {
partial.push(quote(k) + (gap ? ': ' : ':') + v);
}
}
}
}
v = partial.length === 0 ? '{}' : gap ?
'{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}' :
'{' + partial.join(',') + '}';
gap = mind;
return v;
}
}
/**
* @private
* @method stringify
* @param {Object} value
* @param {String} replacer
* @param {String} space
*/
function stringify(value, replacer, space) {
var i;
if (window.JSON && JSON && JSON.stringify) {
/*@<*/
Debug.log('[ ACT_json.js ]: using the native strigify function.');
/*>@*/
return JSON.stringify(value);
}
gap = '';
indent = '';
if (typeof space === 'number') {
for (i = 0; i < space; i += 1) {
indent += ' ';
}
} else if (typeof space === 'string') {
indent = space;
}
rep = replacer;
return str('', { '': value });
}
jsonParse = (function () {
var at; // The index of the current character
var ch; // The current character
var text;
var escapee = {
'"': '"',
'\\': '\\',
'/': '/',
b: '\b',
f: '\f',
n: '\n',
r: '\r',
t: '\t'
};
var error = function (m) {
var thrownError = {
name: 'SyntaxError',
message: m,
at: at,
text: text
};
// Call error when something is wrong.
throw thrownError;
};
var next = function (c) {
// If a c parameter is provided, verify that it matches the current character.
if (c && c !== ch) {
error("Expected '" + c + "' instead of '" + ch + "'");
}
if (!text) {
return '';
}
// Get the next character. When there are no more characters,
// return the empty string.
ch = text.charAt(at);
at += 1;
return ch;
};
var number = function () {
// Let's allow this special function to have more complexity than normal
/* jshint maxcomplexity:false */
// Parse a number value.
var num;
var string = '';
if (ch === '-') {
string = '-';
next('-');
}
while (ch >= '0' && ch <= '9') {
string += ch;
next();
}
if (ch === '.') {
string += '.';
while (next() && ch >= '0' && ch <= '9') {
string += ch;
}
}
if (ch === 'e' || ch === 'E') {
string += ch;
next();
/* istanbul ignore else */
if (ch === '-' || ch === '+') {
string += ch;
next();
}
while (ch >= '0' && ch <= '9') {
string += ch;
next();
}
}
num = +string;
if (!isFinite(num)) {
error('Bad number');
} else {
return num;
}
};
var string = function () {
// Parse a string value.
var hex;
var i;
var strn = '';
var uffff;
// When parsing for string values, we must look for " and \ characters.
/* istanbul ignore else */
if (ch === '"') {
while (next()) {
if (ch === '"') {
next();
return strn;
}
if (ch === '\\') {
next();
/* istanbul ignore else */
if (ch === 'u') {
uffff = 0;
// Let's allow this special function to have more complexity than normal
/* jshint maxdepth:6 */
for (i = 0; i < 4; i += 1) {
hex = parseInt(next(), 16);
if (!isFinite(hex)) {
break;
}
uffff = uffff * 16 + hex;
}
strn += String.fromCharCode(uffff);
} else if (typeof escapee[ch] === 'string') {
strn += escapee[ch];
} else {
break;
}
} else {
strn += ch;
}
}
}
error('Bad string');
};
var white = function () {
// Skip whitespace.
while (ch && ch <= ' ') {
next();
}
};
var word = function () {
// true, false, or null.
switch (ch) {
case 't':
next('t');
next('r');
next('u');
next('e');
return true;
case 'f':
next('f');
next('a');
next('l');
next('s');
next('e');
return false;
case 'n':
next('n');
next('u');
next('l');
next('l');
return null;
}
error("Unexpected '" + ch + "'");
};
var value; // Place holder for the value function.
var array = function () {
// Parse an array value.
var arr = [];
/* istanbul ignore else */
if (ch === '[') {
next('[');
white();
if (ch === ']') {
next(']');
return arr; // empty array
}
while (ch) {
arr.push(value());
white();
if (ch === ']') {
next(']');
return arr;
}
next(',');
white();
}
}
error('Bad array');
};
var object = function () {
// Parse an object value.
var key;
var obj = {};
/* istanbul ignore else */
if (ch === '{') {
next('{');
white();
if (ch === '}') {
next('}');
return obj; // empty object
}
while (ch) {
key = string();
white();
next(':');
if (Object.hasOwnProperty.call(obj, key)) {
error('Duplicate key "' + key + '"');
}
obj[key] = value();
white();
if (ch === '}') {
next('}');
return obj;
}
next(',');
white();
}
}
error('Bad object');
};
value = function () {
// Parse a JSON value. It could be an object, an array, a string, a number,
// or a word.
white();
switch (ch) {
case '{':
return object();
case '[':
return array();
case '"':
return string();
case '-':
return number();
default:
return ch >= '0' && ch <= '9' ? number() : word();
}
};
// Return the jsonParse function. It will have access to all of the above functions and variables.
return function (source) {
var result;
if (window.JSON && JSON && JSON.parse) {
/*@<*/
Debug.log('[ ACT_json.js ]: using the native parse function.');
/*>@*/
return JSON.parse(source);
}
text = source;
at = 0;
ch = ' ';
result = value();
white();
if (ch) {
error('Syntax error');
}
return result;
};
}());
/**
* JSON functionality for the ACT Framework. Most of the code in this file uses Public Domain code from https://github.com/douglascrockford/JSON-js/blob/master/json_parse.js
* @class Json
* @module ACT
*/
function ACTjson() {
/* This is a singleton. */
/* istanbul ignore if */
if (ACTjson.prototype.singleton) {
return ACTjson.prototype.singleton;
}
ACTjson.prototype.singleton = this;
}
/**
* @attribute ATTRS
* @type {{NAME: string, version: string}}
* @initOnly
*/
ACTjson.ATTRS = {
NAME: 'JSON'
};
ACTjson.prototype = {
/**
* @method stringify
* @return {String} object in JSON String Notation
* @param {Mixed} object Given a mixed object/element
*/
stringify: stringify,
/**
* @method parse
* @return {Object} object
* @param {String} string Given a string parses it into a proper JavaScript structure
*/
parse: jsonParse
};
return new ACTjson();
});