1 | var deserialize = require('./deserialize');
|
2 | var serialize = require('./serialize');
|
3 | var shell = require('./modes/shell');
|
4 | var log = require('./modes/log');
|
5 | var es = require('event-stream');
|
6 | var JSONStream = require('JSONStream');
|
7 | var raf = require('raf');
|
8 | var format = require('util').format;
|
9 |
|
10 | function preprocess(text, mode) {
|
11 |
|
12 | mode = mode || 'strict';
|
13 |
|
14 | switch (mode) {
|
15 | case 'strict': return text;
|
16 | case 'shell': return shell.toStrict(text);
|
17 | case 'log': return log.toStrict(text);
|
18 | default:
|
19 | throw new Error(
|
20 | format('unknown mode `%s`. Use `strict` (default), `shell` or `log`.', mode)
|
21 | );
|
22 | }
|
23 | }
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | module.exports.parse = function(text, reviver, mode) {
|
33 | var normalized = preprocess(text, mode);
|
34 | var parsed = JSON.parse(normalized, reviver);
|
35 | return deserialize(parsed);
|
36 | };
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | module.exports.stringify = function(value, replacer, space) {
|
48 | return JSON.stringify(serialize(value), replacer, space);
|
49 | };
|
50 |
|
51 | module.exports.deserialize = deserialize;
|
52 | module.exports.serialize = serialize;
|
53 | module.exports.reviver = require('./reviver');
|
54 |
|
55 |
|
56 | module.exports.createStringifyStream = function(op, sep, cl, indent) {
|
57 | indent = indent || 0;
|
58 | if (op === false) {
|
59 | op = '';
|
60 | sep = '\n';
|
61 | cl = '';
|
62 | } else if (op === null || op === undefined) {
|
63 | op = '[\n';
|
64 | sep = '\n,\n';
|
65 | cl = '\n]\n';
|
66 | }
|
67 |
|
68 |
|
69 |
|
70 | var first = true;
|
71 | var anyData = false;
|
72 |
|
73 | return es.through(function(data) {
|
74 | anyData = true;
|
75 | var json = module.exports.stringify(data, null, indent);
|
76 | if (first) {
|
77 | first = false;
|
78 | this.emit('data', op + json);
|
79 | } else {
|
80 | this.emit('data', sep + json);
|
81 | }
|
82 | }, function() {
|
83 | if (!anyData) {
|
84 | this.emit('data', op);
|
85 | }
|
86 | this.emit('data', cl);
|
87 | this.emit('end');
|
88 | });
|
89 | };
|
90 |
|
91 | module.exports.createParseStream = function(path, map) {
|
92 | var parser = JSONStream.parse(path, map);
|
93 | var wrapper = es.through(function(data) {
|
94 | raf(function() {
|
95 | parser.write(data);
|
96 | });
|
97 | }, function() {
|
98 | this.emit('end');
|
99 | });
|
100 |
|
101 | var emit = function ejsonParseEmit(data) {
|
102 | wrapper.emit('data', data);
|
103 | };
|
104 |
|
105 | parser.on('data', function(data) {
|
106 | deserialize.async(data, function(_, parsed) {
|
107 | if (!Array.isArray(parsed)) {
|
108 | emit(parsed);
|
109 | } else {
|
110 | for (var i = 0; i < parsed.length; i++) {
|
111 | emit(parsed[i]);
|
112 | }
|
113 | }
|
114 | });
|
115 | })
|
116 | .on('error', function(err) {
|
117 | wrapper.emit('error', err);
|
118 | })
|
119 | .on('end', function() {
|
120 | wrapper.emit('end');
|
121 | });
|
122 | return wrapper;
|
123 | };
|