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

Add parser function nodes for XML, JSON and CSV.

Each is dual function - pass in (for example) and get out a js object, pass in a js object and get back out the xml string.
The CSV node must be configured with a column template that specifys the required property names for that column (csv->js), or the properties of the object that should be made into the csv (js->csv)
This commit is contained in:
Dave C-J 2014-04-29 17:01:30 +01:00
parent f2e9b43866
commit 0b49b2cdda
6 changed files with 334 additions and 0 deletions

View File

@ -0,0 +1,67 @@
<!--
Copyright 2014 IBM Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<script type="text/x-red" data-template-name="csv">
<div class="form-row">
<label for="node-input-temp"><i class="icon-list"></i> Columns</label>
<input type="text" id="node-input-temp" placeholder="A,B,C,D">
</div>
<div class="form-row">
<label for="node-input-sep"><i class="icon-chevron-right"></i> Separator</label>
<input type="text" id="node-input-sep" placeholder=','>
</div>
<!-- <div class="form-row">
<label for="node-input-quo"><i class="icon-tag"></i> Escape</label>
<input type="text" id="node-input-quo" placeholder='"'>
</div> -->
<br/>
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div id="node-input-tip" class="form-tips">Tip: you can use "\t" for tab separator.</div>
</script>
<script type="text/x-red" data-help-name="csv">
<p>A function that parses the <b>msg.payload</b> to convert csv to/from a javascript object. Places the result in the payload.</p>
<p>If the input is a string it tries to parse it as CSV and creates a javascript object.</p>
<p>If the input is a javascript object it tries to build a CSV string.</p>
<p>A columns template MUST be specified that contains the ordered list of column headers. For csv input these become the property names.
For csv output these specify the properties to extract from the object and the order for the csv.</p>
<p><b>Note:</b> the columns should always be specified comma separated - even if another separator is chosen for the data.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('csv',{
category: 'function',
color:"#DEBD5C",
defaults: {
name: {value:""},
sep: {value:',',required:true},
//quo: {value:'"',required:true},
temp: {value:"",required:true}
},
inputs:1,
outputs:1,
icon: "arrow-in.png",
label: function() {
return this.name||"csv";
},
labelStyle: function() {
return this.name?"node_label_italic":"";
}
});
</script>

View File

@ -0,0 +1,78 @@
/**
* Copyright 2014 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var RED = require(process.env.NODE_RED_HOME+"/red/red");
function CSVNode(n) {
RED.nodes.createNode(this,n);
this.template = n.temp.split(",");
this.sep = n.sep || ',';
this.sep = this.sep.replace("\\n","\n").replace("\\r","\r").replace("\\t","\t");
this.quo = '"';
var node = this;
this.on("input", function(msg) {
if (msg.hasOwnProperty("payload")) {
if (typeof msg.payload == "object") { // convert to csv
try {
var o = "";
for (var i in node.template) {
if (msg.payload.hasOwnProperty(node.template[i])) {
if (msg.payload[node.template[i]].indexOf(node.sep) != -1) {
o += node.quo + msg.payload[node.template[i]] + node.quo + node.sep;
}
else if (msg.payload[node.template[i]].indexOf(node.quo) != -1) {
msg.payload[node.template[i]] = msg.payload[node.template[i]].replace(/"/g, '""');
o += node.quo + msg.payload[node.template[i]] + node.quo + node.sep;
}
else { o += msg.payload[node.template[i]] + node.sep; }
}
}
msg.payload = o.slice(0,-1);
node.send(msg);
}
catch(e) { node.log(e); }
}
else if (typeof msg.payload == "string") { // convert to object
try {
var f = true;
var j = 0;
var k = [""];
var o = {};
for (var i = 0; i < msg.payload.length; i++) {
if (msg.payload[i] === node.quo) {
f = !f;
if (msg.payload[i-1] === node.quo) { k[j] += '\"'; }
}
else if ((msg.payload[i] === node.sep) && f) {
if ( node.template[j] && (node.template[j] != "") ) { o[node.template[j]] = k[j]; }
j += 1;
k[j] = "";
}
else {
k[j] += msg.payload[i];
}
}
if ( node.template[j] && (node.template[j] != "") ) { o[node.template[j]] = k[j]; }
msg.payload = o;
node.send(msg);
}
catch(e) { node.log(e); }
}
else { node.log("This node only handles csv strings or js objects."); }
}
});
}
RED.nodes.registerType("csv",CSVNode);

View File

@ -0,0 +1,47 @@
<!--
Copyright 2014 IBM Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<script type="text/x-red" data-template-name="json">
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
</script>
<script type="text/x-red" data-help-name="json">
<p>A function that parses the <b>msg.payload</b> to convert a JSON string to/from a javascript object. Places the result back into the payload.</p>
<p>If the input is a JSON string it tries to parse it to a javascript object.</p>
<p>If the input is a javascript object it creates a JSON string.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('json',{
category: 'function',
color:"#DEBD5C",
defaults: {
name: {value:""}
},
inputs:1,
outputs:1,
icon: "arrow-in.png",
label: function() {
return this.name||"json";
},
labelStyle: function() {
return this.name?"node_label_italic":"";
}
});
</script>

View File

@ -0,0 +1,44 @@
/**
* Copyright 2014 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var RED = require(process.env.NODE_RED_HOME+"/red/red");
var util = require("util");
function JSONNode(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 = JSON.parse(msg.payload);
node.send(msg);
}
catch(e) { node.log(e+ "\n"+msg.payload); }
}
else if (typeof msg.payload === "object") {
if (!Buffer.isBuffer(msg.payload) ) {
if (!util.isArray(msg.payload)) {
msg.payload = JSON.stringify(msg.payload);
node.send(msg);
}
}
}
else { node.log("dropped: "+msg.payload); }
}
});
}
RED.nodes.registerType("json",JSONNode);

View File

@ -0,0 +1,48 @@
<!--
Copyright 2014 IBM Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<script type="text/x-red" data-template-name="xml">
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
</script>
<script type="text/x-red" data-help-name="xml">
<p>A function that parses the <b>msg.payload</b> to convert xml to/from a javascript object. Places the result in the payload.</p>
<p>If the input is a string it tries to parse it as XML and creates a javascript object.</p>
<p>If the input is a javascript object it tries to build an XML string.</p>
<p>See <a href="https://github.com/Leonidas-from-XIV/node-xml2js/blob/master/README.md" target="_new">the xml2js docs <i>here</i></a> for more information.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('xml',{
category: 'function',
color:"#DEBD5C",
defaults: {
name: {value:""}
},
inputs:1,
outputs:1,
icon: "arrow-in.png",
label: function() {
return this.name||"xml";
},
labelStyle: function() {
return this.name?"node_label_italic":"";
}
});
</script>

View File

@ -0,0 +1,50 @@
/**
* Copyright 2014 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var RED = require(process.env.NODE_RED_HOME+"/red/red");
var xml2js = require('xml2js');
var parseString = xml2js.parseString;
var builder = new xml2js.Builder({renderOpts:{pretty:false}});
function XMLNode(n) {
RED.nodes.createNode(this,n);
var node = this;
this.on("input", function(msg) {
if (msg.hasOwnProperty("payload")) {
if (typeof msg.payload == "object") {
try {
msg.payload = builder.buildObject(msg.payload);
node.send(msg);
}
catch(e) { node.log(e); }
}
else if (typeof msg.payload == "string") {
try {
parseString(msg.payload, {strict:true,async:true}, function (err, result) {
if (err) { node.error(err); }
else {
msg.payload = result;
node.send(msg);
}
});
}
catch(e) { node.log(e); }
}
else { node.log("This node only handles xml strings or js objects."); }
}
});
}
RED.nodes.registerType("xml",XMLNode);