1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

ensure JSON node handles single booleans and numbers

This commit is contained in:
Dave Conway-Jones 2019-02-15 17:07:11 +00:00
parent afd2ccfb4f
commit 28d4084aa0
No known key found for this signature in database
GPG Key ID: 9E7F9C73F5168CD4
2 changed files with 61 additions and 1 deletions

View File

@ -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 (node.action === "" || node.action === "str") {
if (!Buffer.isBuffer(value)) { if (!Buffer.isBuffer(value)) {
try { try {

View File

@ -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) { it('should log an error if asked to parse an invalid 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"}];