mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Added YAML parser node (#1034)
Thanks @natcl - (sorry pressed closed by mistake !) * Added YAML parser node * Added YAML error strings in messages.json * Change location of YAML library import * Remove copyright * Remove copyright * Change order of yaml in Template node * Add YAML test * Add working test
This commit is contained in:
committed by
Dave Conway-Jones
parent
b1ab26e3ad
commit
9bbc8eda9d
34
nodes/core/parsers/70-YAML.js
Normal file
34
nodes/core/parsers/70-YAML.js
Normal file
@@ -0,0 +1,34 @@
|
||||
module.exports = function(RED) {
|
||||
"use strict";
|
||||
var yaml = require('js-yaml');
|
||||
function YAMLNode(n) {
|
||||
RED.nodes.createNode(this,n);
|
||||
var node = this;
|
||||
this.on("input", function(msg) {
|
||||
if (msg.hasOwnProperty("payload")) {
|
||||
if (typeof msg.payload === "string") {
|
||||
try {
|
||||
msg.payload = yaml.load(msg.payload);
|
||||
node.send(msg);
|
||||
}
|
||||
catch(e) { node.error(e.message,msg); }
|
||||
}
|
||||
else if (typeof msg.payload === "object") {
|
||||
if (!Buffer.isBuffer(msg.payload)) {
|
||||
try {
|
||||
msg.payload = yaml.dump(msg.payload);
|
||||
node.send(msg);
|
||||
}
|
||||
catch(e) {
|
||||
node.error(RED._("yaml.errors.dropped-error"));
|
||||
}
|
||||
}
|
||||
else { node.warn(RED._("yaml.errors.dropped-object")); }
|
||||
}
|
||||
else { node.warn(RED._("yaml.errors.dropped")); }
|
||||
}
|
||||
else { node.send(msg); } // If no payload - just pass it on.
|
||||
});
|
||||
}
|
||||
RED.nodes.registerType("yaml",YAMLNode);
|
||||
};
|
Reference in New Issue
Block a user