-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJSONUtil.js
More file actions
129 lines (119 loc) · 4.45 KB
/
JSONUtil.js
File metadata and controls
129 lines (119 loc) · 4.45 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
var JSONUtil = function (options) {
var $this = this;
this.options = options || {maxDepth:10,showMethod:false,compact:false};
function setMaxDepth(maxDepth) { $this.maxDepth = maxDepth; }
function hideMethods() { $this.showMethod = false; }
function showMethods() { $this.showMethod = true; }
function setCompactOutput(compact) {
if (compact) {
$this.tab = $this.lf = '';
} else {
$this.tab = '\t';
$this.lf = '\n';
}
}
setCompactOutput(this.options.compact);
/**
* A more consistent version of 'typeof' that will return:
*
* undefined: undefined
* null: null
* String: String
* class : Object
* integer: Number
* Number: Number
* cast Number: Number
* float: Number
* boolean: Boolean
* Boolean: Boolean
* Array: Array
* Array[]: Array
* Object{}: Object
*
*/
function typeOf(object) {
var type = Object.prototype.toString.call(object);
type = type.substr(8, type.length-9);
if (type === 'global') {
if (typeof object === 'object')
type = 'null';
else if (object === undefined)
type = 'undefined';
}
return type;
}
function stringify(obj, name, indent, depth, filter) {
name = name || '';
indent = indent || $this.tab;
depth = depth || 1;
filter = filter || [];
indent = typeof indent === 'undefined' ? $this.tab : indent;
depth = typeof depth === 'undefined' ? 1 : depth;
if (filter.indexOf(name) !== -1) {
return '';
}
if (depth > $this.options.maxDepth) {
return indent + name + ': <Maximum Depth Reached>\n';
}
var isArray = typeOf(obj) === 'Array';
var isObject = typeOf(obj) === 'Object';
if (isObject || isArray) {
var child = null;
var output = indent;
if (name !== '') output += '"' + name + '":';
if (isArray) {
output += '[';
} else {
output += '{';
}
output += $this.lf;
var savedIndent = indent;
indent += $this.tab;
for (var item in obj) {
try {
child = obj[item];
} catch (e) {
child = '<Unable to Evaluate>';
}
if (typeOf(child) === 'Object' || typeOf(child) === 'Array') {
output += stringify(child, (isObject ? item : null), indent, depth + 1, filter) + ',' + $this.lf;
} else if (typeOf(child) === 'Function') {
if ($this.options.showMethod)
output += indent + item + ': METHOD' + $this.lf;
else if (obj.hasOwnProperty(item)) {
var buffer = ("" + child).replace(/\n|\r|\s{2,}/g, ' ');
var capture = buffer.match(/^\s*function\s*([^(\s]+)\s*\(/);
if (capture !== null && capture[1] !== undefined) {
buffer = capture[1];
}
output += indent + '"'+ item + '": ' + buffer + ',' + $this.lf;
}
} else {
output += indent + (isObject ? '"' + item + '": ' :'');
if (typeOf(child) === 'String')
output += '"' + child + '"';
else output += child;
output += ',' + $this.lf;
}
}
// This is ugly, but it makes the code simplier.
if (output.substr(output.length -1) === $this.lf)
output = output.substr(0, output.length -1);
if (output.substr(output.length -1) === ',')
output = output.substr(0, output.length -1);
output += $this.lf + savedIndent + (isArray ? '],' : '},') + $this.lf;
output = output.substr(0,output.length -2);
return output;
} else {
return obj;
}
}
return {
"stringify" : stringify,
"setMaxDepth" : setMaxDepth,
"hideMethods" : hideMethods,
"showMethods" : showMethods,
"setCompactOutput" : setCompactOutput
};
};
exports.JSONUtil = JSONUtil;