-
Notifications
You must be signed in to change notification settings - Fork 27
/
JSON.prune.js
164 lines (153 loc) · 5.43 KB
/
JSON.prune.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
// JSON.prune : a function to stringify any object without overflow
// two additional optional parameters :
// - the maximal depth (default : 6)
// - the maximal length of arrays (default : 50)
// You can also pass an "options" object.
// examples :
// var json = JSON.prune(window)
// var arr = Array.apply(0,Array(1000)); var json = JSON.prune(arr, 4, 20)
// var json = JSON.prune(window.location, {inheritedProperties:true})
// Web site : http://dystroy.org/JSON.prune/
// JSON.prune on github : https://github.com/Canop/JSON.prune
// This was discussed here : http://stackoverflow.com/q/13861254/263525
// The code is based on Douglas Crockford's code : https://github.com/douglascrockford/JSON-js/blob/master/json2.js
// No effort was done to support old browsers. JSON.prune will fail on IE8.
(function () {
'use strict';
var DEFAULT_MAX_DEPTH = 6;
var DEFAULT_ARRAY_MAX_LENGTH = 50;
var DEFAULT_PRUNED_VALUE = '"-pruned-"';
var seen; // Same variable used for all stringifications
var iterator; // either forEachEnumerableOwnProperty, forEachEnumerableProperty or forEachProperty
// iterates on enumerable own properties (default behavior)
var forEachEnumerableOwnProperty = function(obj, callback) {
for (var k in obj) {
if (Object.prototype.hasOwnProperty.call(obj, k)) callback(k);
}
};
// iterates on enumerable properties
var forEachEnumerableProperty = function(obj, callback) {
for (var k in obj) callback(k);
};
// iterates on properties, even non enumerable and inherited ones
// This is dangerous
var forEachProperty = function(obj, callback, excluded) {
if (obj==null) return;
excluded = excluded || {};
Object.getOwnPropertyNames(obj).forEach(function(k){
if (!excluded[k]) {
callback(k);
excluded[k] = true;
}
});
forEachProperty(Object.getPrototypeOf(obj), callback, excluded);
};
Object.defineProperty(Date.prototype, "toPrunedJSON", {value:Date.prototype.toJSON});
var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
meta = { // table of character substitutions
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',
'\r': '\\r',
'"' : '\\"',
'\\': '\\\\'
};
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 + '"';
}
var prune = function (value, depthDecr, arrayMaxLength) {
var prunedString = DEFAULT_PRUNED_VALUE;
var replacer;
if (typeof depthDecr == "object") {
var options = depthDecr;
depthDecr = options.depthDecr;
arrayMaxLength = options.arrayMaxLength;
iterator = options.iterator || forEachEnumerableOwnProperty;
if (options.allProperties) iterator = forEachProperty;
else if (options.inheritedProperties) iterator = forEachEnumerableProperty
if ("prunedString" in options) {
prunedString = options.prunedString;
}
if (options.replacer) {
replacer = options.replacer;
}
} else {
iterator = forEachEnumerableOwnProperty;
}
seen = [];
depthDecr = depthDecr || DEFAULT_MAX_DEPTH;
arrayMaxLength = arrayMaxLength || DEFAULT_ARRAY_MAX_LENGTH;
function str(key, holder, depthDecr) {
var i, k, v, length, partial, value = holder[key];
if (value && typeof value === 'object' && typeof value.toPrunedJSON === 'function') {
value = value.toPrunedJSON(key);
}
if (value && typeof value.toJSON === 'function') {
value = value.toJSON();
}
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';
}
if (depthDecr<=0 || seen.indexOf(value)!==-1) {
if (replacer) {
var replacement = replacer(value, prunedString, true);
return replacement===undefined ? undefined : ''+replacement;
}
return prunedString;
}
seen.push(value);
partial = [];
if (Object.prototype.toString.apply(value) === '[object Array]') {
length = Math.min(value.length, arrayMaxLength);
for (i = 0; i < length; i += 1) {
partial[i] = str(i, value, depthDecr-1) || 'null';
}
v = '[' + partial.join(',') + ']';
if (replacer && value.length>arrayMaxLength) return replacer(value, v, false);
return v;
}
if (value instanceof RegExp) {
return quote(value.toString());
}
iterator(value, function(k) {
try {
v = str(k, value, depthDecr-1);
if (v) partial.push(quote(k) + ':' + v);
} catch (e) {
// this try/catch due to forbidden accessors on some objects
}
});
return '{' + partial.join(',') + '}';
case 'function':
case 'undefined':
return replacer ? replacer(value, undefined, false) : undefined;
}
}
return str('', {'': value}, depthDecr);
};
prune.log = function() {
console.log.apply(console, Array.prototype.map.call(arguments, function(v) {
return JSON.parse(JSON.prune(v));
}));
};
prune.forEachProperty = forEachProperty; // you might want to also assign it to Object.forEachProperty
if (typeof module !== "undefined") module.exports = prune;
else JSON.prune = prune;
}());