From 6ae42eb787d81104709bbd5d073e4f82b5aaa854 Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Fri, 10 Dec 2021 15:21:43 +0000 Subject: [PATCH] Let JSON parser attempt to parse buffers if they contain strings and add/fix test --- .../@node-red/nodes/core/parsers/70-JSON.js | 6 +- test/nodes/core/parsers/70-JSON_spec.js | 80 ++++++++++++++----- 2 files changed, 67 insertions(+), 19 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/parsers/70-JSON.js b/packages/node_modules/@node-red/nodes/core/parsers/70-JSON.js index a68edc681..e16c4ec23 100644 --- a/packages/node_modules/@node-red/nodes/core/parsers/70-JSON.js +++ b/packages/node_modules/@node-red/nodes/core/parsers/70-JSON.js @@ -49,7 +49,11 @@ module.exports = function(RED) { } var value = RED.util.getMessageProperty(msg,node.property); 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") { try { RED.util.setMessageProperty(msg,node.property,JSON.parse(value)); diff --git a/test/nodes/core/parsers/70-JSON_spec.js b/test/nodes/core/parsers/70-JSON_spec.js index ad469bbd5..32c5062e8 100644 --- a/test/nodes/core/parsers/70-JSON_spec.js +++ b/test/nodes/core/parsers/70-JSON_spec.js @@ -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) { var flow = [{id:"jn1",type:"json",wires:[["jn2"]]}, {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) { - var flow = [{id:"jn1",type:"json",wires:[["jn2"]]}, + it('should log an error if asked to parse an invalid json string in a buffer', 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"); - 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-object'); - done(); - } catch(err) { - done(err); - } - },50); - jn1.receive({payload:Buffer.from("a")}); + try { + var jn1 = helper.getNode("jn1"); + var jn2 = helper.getNode("jn2"); + jn1.receive({payload:Buffer.from('{"name":foo}'),topic: "bar"}); + 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.should.startWith("Unexpected token o"); + logEvents[0][0].should.have.a.property('level',helper.log().ERROR); + done(); + } catch(err) { done(err) } + },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) { var flow = [{id:"jn1",type:"json",wires:[["jn2"]]}, {id:"jn2", type:"helper"}];