是否有一个nodejs解析器可以连接到一个分块的JSON(或XML)数据流,并且会发出类似于内置JSON解析器的事件?
我要解析的流是来自HTTP请求到例如后端服务器。传入的JSON不能采用特殊格式。 IOW,nodejs - parsing chunked twitter json中的解决方案对我不起作用。
扬
答案 0 :(得分:3)
答案 1 :(得分:1)
答案 2 :(得分:-3)
对于json,您可以使用以下代码段。
的CoffeeScript
make_parser = (callback)->
state_parse=0
level=0
buffer=0
parse_out = (data)->
m = data.match /[{}"]/
if m?
c = m[0]
buffer += data[..m.index]
remaining = data[m.index+1..]
if c == "}"
level -= 1
if level == 0
callback JSON.parse(buffer)
init_state()
else if c == "{"
level += 1
else if c == '"'
state_parse = parse_string
state_parse remaining
else
buffer += data
parse_string = (data)->
m = data.match /["\\]/
if m?
c = m[0]
buffer += data[..m.index]
remaining = data[m.index+1..]
transition =
'\\': parse_special
'\"': parse_out
state_parse = transition[c]
state_parse remaining
else
buffer += data
parse_special = (data)->
if data.length > 0
buffer += data[0]
state_parse = parse_string
state_parse data[1..]
init_state =->
state_parse = parse_out
level = 0
buffer = ""
init_state()
(data)->
state_parse data
fs = require "fs"
s = fs.createReadStream "somefile.json"
s.setEncoding 'utf8'
s.on "data", make_parser (d)->
console.log "-----"
console.log d
的javascript:
var fs,make_parser,s;
make_parser = function(callback) {
var buffer, init_state, level, parse_out, parse_special, parse_string, state_parse;
state_parse = 0;
level = 0;
buffer = 0;
parse_out = function(data) {
var c, m, remaining;
m = data.match(/[{}"]/);
if (m != null) {
c = m[0];
buffer += data.slice(0, m.index + 1 || 9e9);
remaining = data.slice(m.index + 1);
if (c === "}") {
level -= 1;
if (level === 0) {
callback(JSON.parse(buffer));
init_state();
}
} else if (c === "{") {
level += 1;
} else if (c === '"') {
state_parse = parse_string;
}
return state_parse(remaining);
} else {
return buffer += data;
}
};
parse_string = function(data) {
var c, m, remaining, transition;
m = data.match(/["\\]/);
if (m != null) {
c = m[0];
buffer += data.slice(0, m.index + 1 || 9e9);
remaining = data.slice(m.index + 1);
transition = {
'\\': parse_special,
'\"': parse_out
};
state_parse = transition[c];
return state_parse(remaining);
} else {
return buffer += data;
}
};
parse_special = function(data) {
if (data.length > 0) {
buffer += data[0];
state_parse = parse_string;
return state_parse(data.slice(1));
}
};
init_state = function() {
state_parse = parse_out;
level = 0;
return buffer = "";
};
init_state();
return function(data) {
return state_parse(data);
};
};
fs = require("fs");
s = fs.createReadStream("somefile.json");
s.setEncoding('utf8');
s.on("data", make_parser(function(d) {
console.log("-----");
return console.log(d);
}));