-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonlike-json-tool.js
More file actions
36 lines (29 loc) · 965 Bytes
/
pythonlike-json-tool.js
File metadata and controls
36 lines (29 loc) · 965 Bytes
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
(function (global) {
var INDENT = ' '
function pythonlikeJsonToolStringify (node) {
if (typeof node !== 'object' || node == null) {
return JSON.stringify(node)
}
if (Array.isArray(node)) {
if (node.length === 0) { return '[]' }
return '[\n' + node.map(function (element) {
return indent(pythonlikeJsonToolStringify(element))
}).join(',\n') + '\n]'
}
var keys = Object.keys(node).sort()
if (keys.length === 0) { return '{}' }
return '{\n' + keys.map(function (key) {
return indent(JSON.stringify(key) + ': ' + pythonlikeJsonToolStringify(node[key]))
}).join(',\n') + '\n}'
}
function indent (str) {
return str.split('\n').map(function (line) {
return INDENT + line
}).join('\n')
}
if (typeof module === 'undefined') {
global.pythonlikeJsonToolStringify = pythonlikeJsonToolStringify
} else {
module.exports = pythonlikeJsonToolStringify
}
})(this)