main
Last change
on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
921 bytes
|
Line | |
---|
1 | #!/usr/bin/env node
|
---|
2 |
|
---|
3 | 'use strict';
|
---|
4 |
|
---|
5 | var traverse = require('traverse');
|
---|
6 |
|
---|
7 | var obj = ['five', 6, -3, [7, 8, -2, 1], { f: 10, g: -13 }];
|
---|
8 |
|
---|
9 | var s = '';
|
---|
10 | traverse(obj).forEach(function toS(node) {
|
---|
11 | if (Array.isArray(node)) {
|
---|
12 | this.before(function () { s += '['; });
|
---|
13 | this.post(function (child) {
|
---|
14 | if (!child.isLast) { s += ','; }
|
---|
15 | });
|
---|
16 | this.after(function () { s += ']'; });
|
---|
17 | } else if (typeof node === 'object') {
|
---|
18 | this.before(function () { s += '{'; });
|
---|
19 | this.pre(function (x, key) {
|
---|
20 | toS(key);
|
---|
21 | s += ':';
|
---|
22 | });
|
---|
23 | this.post(function (child) {
|
---|
24 | if (!child.isLast) { s += ','; }
|
---|
25 | });
|
---|
26 | this.after(function () { s += '}'; });
|
---|
27 | } else if (typeof node === 'string') {
|
---|
28 | s += '"' + node.toString().replace(/"/g, '\\"') + '"';
|
---|
29 | } else if (typeof node === 'function') {
|
---|
30 | s += 'null';
|
---|
31 | } else {
|
---|
32 | s += node.toString();
|
---|
33 | }
|
---|
34 | });
|
---|
35 |
|
---|
36 | console.log('JSON.stringify: ' + JSON.stringify(obj));
|
---|
37 | console.log('this stringify: ' + s);
|
---|
Note:
See
TracBrowser
for help on using the repository browser.