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 f696e1f21..6611877e1 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 @@ -82,7 +82,7 @@ module.exports = function(RED) { } } } - else if (typeof value === "object") { + else if ((typeof value === "object") || (typeof value === "boolean") || (typeof value === "number")) { if (node.action === "" || node.action === "str") { if (!Buffer.isBuffer(value)) { try { diff --git a/test/nodes/core/parsers/70-JSON_spec.js b/test/nodes/core/parsers/70-JSON_spec.js index 767d09ecf..c06b5b09d 100644 --- a/test/nodes/core/parsers/70-JSON_spec.js +++ b/test/nodes/core/parsers/70-JSON_spec.js @@ -80,6 +80,66 @@ describe('JSON node', function() { }); }); + it('should convert a boolean to a json string', 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"); + jn2.on("input", function(msg) { + should.equal(msg.payload, 'true'); + done(); + }); + var obj = true; + jn1.receive({payload:obj}); + }); + }); + + it('should convert a json string to a boolean', 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"); + jn2.on("input", function(msg) { + should.equal(msg.payload, true); + done(); + }); + var obj = "true"; + jn1.receive({payload:obj}); + }); + }); + + it('should convert a number to a json string', 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"); + jn2.on("input", function(msg) { + should.equal(msg.payload, '2019'); + done(); + }); + var obj = 2019; + jn1.receive({payload:obj}); + }); + }); + + it('should convert a json string to a number', 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"); + jn2.on("input", function(msg) { + should.equal(msg.payload, 1962); + done(); + }); + var obj = '1962'; + jn1.receive({payload:obj}); + }); + }); + it('should log an error if asked to parse an invalid json string', function(done) { var flow = [{id:"jn1",type:"json",wires:[["jn2"]]}, {id:"jn2", type:"helper"}];