mirror of
				https://github.com/node-red/node-red.git
				synced 2025-03-01 10:36:34 +00:00 
			
		
		
		
	Merge pull request #3296 from node-red/Json-try-parse-buffer
Let JSON node attempt to parse buffer if it contains a valid string
This commit is contained in:
		| @@ -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)); | ||||
|   | ||||
| @@ -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"}]; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user