-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringify-test.html
102 lines (90 loc) · 3.48 KB
/
stringify-test.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®,Microshaoft">
<meta name="Author" content="EditPlus®,Microshaoft">
<meta name="Keywords" content="EditPlus®,Microshaoft">
<meta name="Description" content="EditPlus®,Microshaoft">
<title>Document</title>
<script type="text/javascript">
// fast-json-stable-stringify
function stringify (data, opts) {
if (!opts) opts = {};
if (typeof opts === 'function') opts = { cmp: opts };
var cycles = (typeof opts.cycles === 'boolean') ? opts.cycles : false;
var cmp = opts.cmp && (function (f) {
return function (node) {
return function (a, b) {
var aobj = { key: a, value: node[a] };
var bobj = { key: b, value: node[b] };
return f(aobj, bobj);
};
};
})(opts.cmp);
var seen = [];
return (function stringify (node) {
if (node && node.toJSON && typeof node.toJSON === 'function') {
node = node.toJSON();
}
if (node === undefined) return;
if (typeof node == 'number') return isFinite(node) ? '' + node : 'null';
if (typeof node !== 'object') return JSON.stringify(node);
var i, out;
if (Array.isArray(node)) {
out = '[';
for (i = 0; i < node.length; i++) {
if (i) out += ',';
out += stringify(node[i]) || 'null';
}
return out + ']';
}
if (node === null) return 'null';
if (seen.indexOf(node) !== -1) {
if (cycles) return JSON.stringify('__cycle__');
throw new TypeError('Converting circular structure to JSON');
}
var seenIndex = seen.push(node) - 1;
var keys = Object.keys(node).sort(cmp && cmp(node));
out = '';
for (i = 0; i < keys.length; i++) {
var key = keys[i];
var value = stringify(node[key]);
if (!value) continue;
if (out) out += ',';
out += JSON.stringify(key) + ':' + value;
}
seen.splice(seenIndex, 1);
return '{' + out + '}';
})(data);
};
function reStringify(x) {
var a = JSON.parse(x);
var reStringifyJson = stringify(a);
a = JSON.parse(reStringifyJson);
reStringifyJson = JSON.stringify(a, function (k, v) {
if (typeof v == 'string') {
//console.log('%s : %s', k, JSON.stringify(v));
return '********';
} else if (typeof v == 'number') {
return -1;
} else if (typeof v == 'boolean') {
return true;
} else if (typeof v == 'undeclaredVariable') {
return undefined;
}
return v;
}, 4);
return reStringifyJson;
}
function test() {
var json = '{"z":1,"a":2}';
json = reStringify(json);
console.log(json);
}
</script>
</head>
<body>
<button onclick="test();">click me</button>
</body>
</html>