-
Notifications
You must be signed in to change notification settings - Fork 2
/
Log.js
52 lines (47 loc) · 2.17 KB
/
Log.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
/*
* Copyright (C) 2013 Nikita Krupenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
.pragma library
function serialize(object, maxDepth) {
function _processObject(object, maxDepth, level) {
var output = Array()
var pad = " "
if (maxDepth == undefined) {
maxDepth = -1
}
if (level == undefined) {
level = 0
}
var padding = Array(level + 1).join(pad)
output.push((Array.isArray(object) ? "[" : "{"))
var fields = Array()
for (var key in object) {
var keyText = Array.isArray(object) ? "" : ("\"" + key + "\": ")
if (typeof (object[key]) == "object" && key != "parent" && maxDepth != 0) {
var res = _processObject(object[key], maxDepth > 0 ? maxDepth - 1 : -1, level + 1)
fields.push(padding + pad + keyText + res)
} else {
fields.push(padding + pad + keyText + "\"" + object[key] + "\"")
}
}
output.push(fields.join(",\n"))
output.push(padding + (Array.isArray(object) ? "]" : "}"))
return output.join("\n")
}
return _processObject(object, maxDepth)
}