stringify.js
15.4 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
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// tests stringify()
/*global require console exports */
// set to true to show performance stats
var DEBUG = false;
var assert = require('assert');
var JSON5 = require('../lib/json5');
// Test JSON5.stringify() by comparing its output for each case with
// native JSON.stringify(). The only differences will be in how object keys are
// handled.
var simpleCases = [
null,
9, -9, +9, +9.878,
'', "''", '999', '9aa', 'aaa', 'aa a', 'aa\na', 'aa\\a', '\'', '\\\'', '\\"',
undefined,
true, false,
{}, [], function(){},
Date.now(), new Date(Date.now())
];
exports.stringify = {};
exports.stringify.simple = function test() {
for (var i=0; i<simpleCases.length; i++) {
assertStringify(simpleCases[i]);
}
};
exports.stringify.oddities = function test() {
assertStringify(Function);
assertStringify(Date);
assertStringify(Object);
assertStringify(NaN);
assertStringify(Infinity);
assertStringify(10e6);
assertStringify(19.3223e6);
assertStringify(077);
assertStringify(0x99);
assertStringify(/aa/);
assertStringify(new RegExp('aa'));
assertStringify(new Number(7));
assertStringify(new String(7));
assertStringify(new String(""));
assertStringify(new String("abcde"));
assertStringify(new String(new String("abcde")));
assertStringify(new Boolean(true));
assertStringify(new Boolean());
};
exports.stringify.arrays = function test() {
assertStringify([""]);
assertStringify([1, 2]);
assertStringify([undefined]);
assertStringify([1, 'fasds']);
assertStringify([1, '\n\b\t\f\r\'']);
assertStringify([1, 'fasds', ['fdsafsd'], null]);
assertStringify([1, 'fasds', ['fdsafsd'], null, function(aaa) { return 1; }, false ]);
assertStringify([1, 'fasds', ['fdsafsd'], undefined, function(aaa) { return 1; }, false ]);
};
exports.stringify.objects = function test() {
assertStringify({a:1, b:2});
assertStringify({"":1, b:2});
assertStringify({9:1, b:2});
assertStringify({"9aaa":1, b:2});
assertStringify({aaaa:1, bbbb:2});
assertStringify({a$a_aa:1, bbbb:2});
assertStringify({"a$a_aa":1, 'bbbb':2});
assertStringify({"a$a_aa":[1], 'bbbb':{a:2}});
assertStringify({"a$22222_aa":[1], 'bbbb':{aaaa:2, name:function(a,n,fh,h) { return 'nuthin'; }, foo: undefined}});
assertStringify({"a$222222_aa":[1], 'bbbb':{aaaa:2, name:'other', foo: undefined}});
assertStringify({"a$222222_aa":[1, {}, undefined, function() { }, { jjj: function() { } }], 'bbbb':{aaaa:2, name:'other', foo: undefined}});
// using same obj multiple times
var innerObj = {a: 9, b:6};
assertStringify({a : innerObj, b: innerObj, c: [innerObj, innerObj, innerObj]});
};
exports.stringify.oddKeys = function test() {
assertStringify({"this is a crazy long key":1, 'bbbb':2});
assertStringify({"":1, 'bbbb':2});
assertStringify({"s\ns":1, 'bbbb':2});
assertStringify({'\n\b\t\f\r\'\\':1, 'bbbb':2});
assertStringify({undefined:1, 'bbbb':2});
assertStringify({'\x00':'\x00'});
};
// we expect errors from all of these tests. The errors should match
exports.stringify.circular = function test() {
var obj = { };
obj.obj = obj;
assertStringify(obj, null, true);
var obj2 = {inner1: {inner2: {}}};
obj2.inner1.inner2.obj = obj2;
assertStringify(obj2, null, true);
var obj3 = {inner1: {inner2: []}};
obj3.inner1.inner2[0] = obj3;
assertStringify(obj3, null, true);
};
exports.stringify.replacerType = function test() {
var assertStringifyJSON5ThrowsExceptionForReplacer = function(replacer) {
assert.throws(
function() { JSON5.stringify(null, replacer); },
/Replacer must be a function or an array/
);
};
assertStringifyJSON5ThrowsExceptionForReplacer('string');
assertStringifyJSON5ThrowsExceptionForReplacer(123);
assertStringifyJSON5ThrowsExceptionForReplacer({});
};
exports.stringify.replacer = {};
exports.stringify.replacer.function = {};
exports.stringify.replacer.function.simple = function test() {
function replacerTestFactory(expectedValue) {
return function() {
var lastKey = null,
lastValue = null,
numCalls = 0,
replacerThis;
return {
replacer: function(key, value) {
lastKey = key;
lastValue = value;
numCalls++;
replacerThis = this;
return value;
},
assert: function() {
assert.equal(numCalls, 1, "Replacer should be called exactly once for " + expectedValue);
assert.equal(lastKey, "");
assert.deepEqual(replacerThis, {"":expectedValue});
var expectedValueToJson = expectedValue;
if (expectedValue && expectedValue['toJSON']) {
expectedValueToJson = expectedValue.toJSON();
}
assert.equal(lastValue, expectedValueToJson);
}
}
}
}
for (var i=0; i<simpleCases.length; i++) {
assertStringify(simpleCases[i], replacerTestFactory(simpleCases[i]));
}
};
exports.stringify.replacer.function.complexObject = function test() {
var obj = {
"": "emptyPropertyName",
one: 'string',
two: 123,
three: ['array1', 'array2'],
four: {nested_one:'anotherString'},
five: new Date(),
six: Date.now(),
seven: null,
eight: true,
nine: false,
ten: [NaN, Infinity, -Infinity],
eleven: function() {}
};
var expectedKeys = [
'', // top level object
'', // First key
'one',
'two',
'three', 0, 1, // array keys
'four', 'nested_one', // nested object keys
'five',
'six',
'seven',
'eight',
'nine',
'ten', 0, 1, 2, // array keys
'eleven'
];
var expectedHolders = [
{"": obj},
obj,
obj,
obj,
obj, obj.three, obj.three,
obj, obj.four,
obj,
obj,
obj,
obj,
obj,
obj, obj.ten, obj.ten, obj.ten,
obj
];
var ReplacerTest = function() {
var seenKeys = [];
var seenHolders = [];
return {
replacer: function(key, value) {
seenKeys.push(key);
seenHolders.push(this);
if (typeof(value) == "object") {
return value;
}
return 'replaced ' + (value ? value.toString() : '');
},
assert: function() {
assert.deepEqual(seenKeys, expectedKeys);
assert.deepEqual(seenHolders, expectedHolders);
}
}
};
assertStringify(obj, ReplacerTest);
};
exports.stringify.replacer.function.replacingWithUndefined = function test() {
var obj = { shouldSurvive: 'one', shouldBeRemoved: 'two' };
var ReplacerTest = function() {
return {
replacer: function(key, value) {
if (key === 'shouldBeRemoved') {
return undefined;
} else {
return value;
}
},
assert: function() { /* no-op */ }
}
};
assertStringify(obj, ReplacerTest);
};
exports.stringify.replacer.function.replacingArrayValueWithUndefined = function test() {
var obj = ['should survive', 'should be removed'];
var ReplacerTest = function() {
return {
replacer: function(key, value) {
if (value === 'should be removed') {
return undefined;
} else {
return value;
}
},
assert: function() { /* no-op */ }
}
};
assertStringify(obj, ReplacerTest);
};
exports.stringify.replacer.array = {};
exports.stringify.replacer.array.simple = function test() {
var ReplacerTest = function() {
return {
replacer: [],
assert: function() { /* no-op */ }
}
};
for (var i=0; i<simpleCases.length; i++) {
assertStringify(simpleCases[i], ReplacerTest);
}
};
exports.stringify.replacer.array.emptyStringProperty = function test() {
var obj = {'': 'keep', 'one': 'remove'};
var ReplacerTest = function() {
return {
replacer: [''],
assert: function() {/* no-op */}
}
};
assertStringify(obj, ReplacerTest);
};
exports.stringify.replacer.array.complexObject = function test() {
var obj = {
"": "emptyPropertyName",
one: 'string',
one_remove: 'string',
two: 123,
two_remove: 123,
three: ['array1', 'array2'],
three_remove: ['array1', 'array2'],
four: {nested_one:'anotherString', nested_one_remove: 'anotherString'},
four_remove: {nested_one:'anotherString', nested_one_remove: 'anotherString'},
five: new Date(),
five_remove: new Date(),
six: Date.now(),
six_remove: Date.now(),
seven: null,
seven_remove: null,
eight: true,
eight_remove: true,
nine: false,
nine_remove: false,
ten: [NaN, Infinity, -Infinity],
ten_remove: [NaN, Infinity, -Infinity],
eleven: function() {},
eleven_remove: function() {}
};
var ReplacerTest = function() {
return {
replacer: [
'one', 'two', 'three', 'four', 'nested_one', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 0
],
assert: function() {/* no-op */}
}
};
assertStringify(obj, ReplacerTest);
};
exports.stringify.toJSON = function test() {
var customToJSONObject = {
name: 'customToJSONObject',
toJSON: function() {
return 'custom-to-json-object-serialization';
}
};
assertStringify(customToJSONObject);
var customToJSONPrimitive = "Some string";
customToJSONPrimitive.toJSON = function() {
return 'custom-to-json-string-serialization';
};
assertStringify(customToJSONPrimitive);
var object = {
customToJSONObject: customToJSONObject
};
assertStringify(object);
// Returning an object with a toJSON function does *NOT* have that toJSON function called: it is omitted
var nested = {
name: 'nested',
toJSON: function() {
return customToJSONObject;
}
};
assertStringify(nested);
var count = 0;
function createObjectSerialisingTo(value) {
count++;
return {
name: 'obj-' + count,
toJSON: function() {
return value;
}
};
}
assertStringify(createObjectSerialisingTo(null));
assertStringify(createObjectSerialisingTo(undefined));
assertStringify(createObjectSerialisingTo([]));
assertStringify(createObjectSerialisingTo({}));
assertStringify(createObjectSerialisingTo(12345));
assertStringify(createObjectSerialisingTo(true));
assertStringify(createObjectSerialisingTo(new Date()));
assertStringify(createObjectSerialisingTo(function(){}));
};
function stringifyJSON5(obj, replacer, space) {
var start, res, end;
try {
start = new Date();
res = JSON5.stringify(obj, replacer, space);
end = new Date();
} catch (e) {
res = e.message;
end = new Date();
}
if (DEBUG) {
console.log('JSON5.stringify time: ' + (end-start));
console.log(res);
}
return res;
}
function stringifyJSON(obj, replacer, space) {
var start, res, end;
try {
start = new Date();
res = JSON.stringify(obj, replacer, space);
end = new Date();
// now remove all quotes from keys where appropriate
// first recursively find all key names
var keys = [];
function findKeys(key, innerObj) {
if (innerObj && innerObj.toJSON && typeof innerObj.toJSON === "function") {
innerObj = innerObj.toJSON();
}
if (replacer) {
if (typeof replacer === 'function') {
innerObj = replacer(key, innerObj);
} else if (key !== '' && replacer.indexOf(key) < 0) {
return;
}
}
if (JSON5.isWord(key) &&
typeof innerObj !== 'function' &&
typeof innerObj !== 'undefined') {
keys.push(key);
}
if (typeof innerObj === 'object') {
if (Array.isArray(innerObj)) {
for (var i = 0; i < innerObj.length; i++) {
findKeys(i, innerObj[i]);
}
} else if (innerObj !== null) {
for (var prop in innerObj) {
if (innerObj.hasOwnProperty(prop)) {
findKeys(prop, innerObj[prop]);
}
}
}
}
}
findKeys('', obj);
// now replace each key in the result
var last = 0;
for (var i = 0; i < keys.length; i++) {
// not perfect since we can match on parts of the previous value that
// matches the key, but we can design our test around that.
last = res.indexOf('"' + keys[i] + '"', last);
if (last === -1) {
// problem with test framework
console.log("Couldn't find: " + keys[i]);
throw new Error("Couldn't find: " + keys[i]);
}
res = res.substring(0, last) +
res.substring(last+1, last + keys[i].length+1) +
res.substring(last + keys[i].length + 2, res.length);
last += keys[i].length;
}
} catch (e) {
res = e.message;
end = new Date();
}
if (DEBUG) {
console.log('JSON.stringify time: ' + (end-start));
}
return res;
}
function assertStringify(obj, replacerTestConstructor, expectError) {
if (!replacerTestConstructor) {
replacerTestConstructor = function(){
return {replacer: null, assert: function(){}};
};
}
var testStringsEqual = function(obj, indent) {
var j5ReplacerTest = replacerTestConstructor();
var jReplacerTest = replacerTestConstructor();
var j5, j;
j5 = stringifyJSON5(obj, j5ReplacerTest.replacer, indent);
j = stringifyJSON(obj, jReplacerTest.replacer, indent);
assert.equal(j5, j);
j5ReplacerTest.assert();
};
var indents = [
undefined,
" ",
" ",
" ",
"\t",
"this is an odd indent",
5,
20,
'\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t'
];
for (var i=0; i<indents.length; i++) {
testStringsEqual(obj, indents[i]);
}
if (!expectError) {
// no point in round tripping if there is an error
var origStr = JSON5.stringify(obj), roundTripStr;
if (origStr !== "undefined" && typeof origStr !== "undefined") {
try {
roundTripStr = JSON5.stringify(JSON5.parse(origStr));
} catch (e) {
console.log(e);
console.log(origStr);
throw e;
}
assert.equal(origStr, roundTripStr);
}
}
}