mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Let JSON parser attempt to parse buffers if they contain strings
and add/fix test
This commit is contained in:
parent
7cd3e49f04
commit
6ae42eb787
@ -49,7 +49,11 @@ module.exports = function(RED) {
|
|||||||
}
|
}
|
||||||
var value = RED.util.getMessageProperty(msg,node.property);
|
var value = RED.util.getMessageProperty(msg,node.property);
|
||||||
if (value !== undefined) {
|
if (value !== undefined) {
|
||||||
if (typeof value === "string") {
|
if (typeof value === "string" || Buffer.isBuffer(value)) {
|
||||||
|
// if (Buffer.isBuffer(value) && node.action !== "obj") {
|
||||||
|
// node.warn(RED._("json.errors.dropped")); done();
|
||||||
|
// }
|
||||||
|
// else
|
||||||
if (node.action === "" || node.action === "obj") {
|
if (node.action === "" || node.action === "obj") {
|
||||||
try {
|
try {
|
||||||
RED.util.setMessageProperty(msg,node.property,JSON.parse(value));
|
RED.util.setMessageProperty(msg,node.property,JSON.parse(value));
|
||||||
|
@ -50,6 +50,24 @@ describe('JSON node', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should convert a buffer of a valid json string to a javascript object', function(done) {
|
||||||
|
var flow = [{id:"jn1",type:"json",action:"obj",wires:[["jn2"]]},
|
||||||
|
{id:"jn2", type:"helper"}];
|
||||||
|
helper.load(jsonNode, flow, function() {
|
||||||
|
var jn1 = helper.getNode("jn1");
|
||||||
|
var jn2 = helper.getNode("jn2");
|
||||||
|
jn2.on("input", function(msg) {
|
||||||
|
msg.should.have.property('topic', 'bar');
|
||||||
|
msg.payload.should.have.property('employees');
|
||||||
|
msg.payload.employees[0].should.have.property('firstName', 'John');
|
||||||
|
msg.payload.employees[0].should.have.property('lastName', 'Smith');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
var jsonString = Buffer.from('{"employees":[{"firstName":"John", "lastName":"Smith"}]}');
|
||||||
|
jn1.receive({payload:jsonString,topic: "bar"});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should convert a javascript object to a json string', function(done) {
|
it('should convert a javascript object to a json string', function(done) {
|
||||||
var flow = [{id:"jn1",type:"json",wires:[["jn2"]]},
|
var flow = [{id:"jn1",type:"json",wires:[["jn2"]]},
|
||||||
{id:"jn2", type:"helper"}];
|
{id:"jn2", type:"helper"}];
|
||||||
@ -166,29 +184,55 @@ describe('JSON node', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should log an error if asked to parse something thats not json or js', function(done) {
|
it('should log an error if asked to parse an invalid json string in a buffer', function(done) {
|
||||||
var flow = [{id:"jn1",type:"json",wires:[["jn2"]]},
|
var flow = [{id:"jn1",type:"json",action:"obj",wires:[["jn2"]]},
|
||||||
{id:"jn2", type:"helper"}];
|
{id:"jn2", type:"helper"}];
|
||||||
helper.load(jsonNode, flow, function() {
|
helper.load(jsonNode, flow, function() {
|
||||||
var jn1 = helper.getNode("jn1");
|
try {
|
||||||
var jn2 = helper.getNode("jn2");
|
var jn1 = helper.getNode("jn1");
|
||||||
setTimeout(function() {
|
var jn2 = helper.getNode("jn2");
|
||||||
try {
|
jn1.receive({payload:Buffer.from('{"name":foo}'),topic: "bar"});
|
||||||
var logEvents = helper.log().args.filter(function(evt) {
|
setTimeout(function() {
|
||||||
return evt[0].type == "json";
|
try {
|
||||||
});
|
var logEvents = helper.log().args.filter(function(evt) {
|
||||||
logEvents.should.have.length(1);
|
return evt[0].type == "json";
|
||||||
logEvents[0][0].should.have.a.property('msg');
|
});
|
||||||
logEvents[0][0].msg.toString().should.eql('json.errors.dropped-object');
|
logEvents.should.have.length(1);
|
||||||
done();
|
logEvents[0][0].should.have.a.property('msg');
|
||||||
} catch(err) {
|
logEvents[0][0].msg.should.startWith("Unexpected token o");
|
||||||
done(err);
|
logEvents[0][0].should.have.a.property('level',helper.log().ERROR);
|
||||||
}
|
done();
|
||||||
},50);
|
} catch(err) { done(err) }
|
||||||
jn1.receive({payload:Buffer.from("a")});
|
},20);
|
||||||
|
} catch(err) {
|
||||||
|
done(err);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// it('should log an error if asked to parse something thats not json or js and not in force object mode', function(done) {
|
||||||
|
// var flow = [{id:"jn1",type:"json",wires:[["jn2"]]},
|
||||||
|
// {id:"jn2", type:"helper"}];
|
||||||
|
// helper.load(jsonNode, flow, function() {
|
||||||
|
// var jn1 = helper.getNode("jn1");
|
||||||
|
// var jn2 = helper.getNode("jn2");
|
||||||
|
// setTimeout(function() {
|
||||||
|
// try {
|
||||||
|
// var logEvents = helper.log().args.filter(function(evt) {
|
||||||
|
// return evt[0].type == "json";
|
||||||
|
// });
|
||||||
|
// logEvents.should.have.length(1);
|
||||||
|
// logEvents[0][0].should.have.a.property('msg');
|
||||||
|
// logEvents[0][0].msg.toString().should.eql('json.errors.dropped');
|
||||||
|
// done();
|
||||||
|
// } catch(err) {
|
||||||
|
// done(err);
|
||||||
|
// }
|
||||||
|
// },50);
|
||||||
|
// jn1.receive({payload:Buffer.from("abcd")});
|
||||||
|
// });
|
||||||
|
// });
|
||||||
|
|
||||||
it('should pass straight through if no payload set', function(done) {
|
it('should pass straight through if no payload set', function(done) {
|
||||||
var flow = [{id:"jn1",type:"json",wires:[["jn2"]]},
|
var flow = [{id:"jn1",type:"json",wires:[["jn2"]]},
|
||||||
{id:"jn2", type:"helper"}];
|
{id:"jn2", type:"helper"}];
|
||||||
|
Loading…
Reference in New Issue
Block a user