Toggle navigation
P3X GitList Snapshot
GitHub
Repo
Changelog
To do
Releases
Themes
Change log
Loading change log ...
To do ...
Loading todo ...
browsing:
d355a67f1cf795c57bf1a25f76beb214b9e7459d
Branches
0.1
0.2
html-sanitizer
html-sanitizer-and-mocha-specs
master
sanitize-oversight
Tags
v0.11.2
v0.11.0
v0.9.2
v0.9.0
v0.8.2
v0.6.2
v0.6.1
v0.6.0
v0.5.1
v0.5.0
v0.4.0
v0.3.2
v0.3.1
v0.3.0
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.1.1
0.1.0
Files
Commits
Log
Graph
Stats
xml2json.git
lib
json2xml.js
RSS
Git
Fetch origin
Download
ZIP
TAR
Clone
Raw
View
History
Clone
SSH
HTTPS
Blames found: 41
Mode: javascript
Binary: false
Hang on, we reloading big blames...
34616418
var sanitizer = require('./sanitize.js')
8a4718e3
module.exports = function (json, options) {
8407d456
if (json instanceof Buffer) { json = json.toString(); } var obj = null; if (typeof(json) == 'string') { try { obj = JSON.parse(json); } catch(e) { throw new Error("The JSON structure is invalid"); } } else { obj = json; }
34616418
var toXml = new ToXml(options);
753f6909
toXml.parse(obj); return toXml.xml; }
8407d456
753f6909
ToXml.prototype.parse = function(obj) { var self = this;
8407d456
var keys = Object.keys(obj); var len = keys.length;
db251bde
// First pass, extract strings only for (var i = 0; i < len; i++) {
29b9b183
var key = keys[i], value = obj[key], isArray = Array.isArray(value);
69a937d4
var type = typeof(value); if (type == 'string' || type == 'number' || type == 'boolean' || isArray) {
e4c71b31
var it = isArray ? value : [value];
29b9b183
e4c71b31
it.forEach(function(subVal) { if (typeof(subVal) != 'object') { if (key == '$t') {
753f6909
self.addTextContent(subVal);
e4c71b31
} else {
753f6909
self.addAttr(key, subVal);
e4c71b31
} }
34616418
});
db251bde
} } // Second path, now handle sub-objects and arrays
8407d456
for (var i = 0; i < len; i++) { var key = keys[i]; if (Array.isArray(obj[key])) { var elems = obj[key]; var l = elems.length; for (var j = 0; j < l; j++) {
e4c71b31
var elem = elems[j];
29b9b183
e4c71b31
if (typeof(elem) == 'object') {
753f6909
self.openTag(key); self.parse(elem); self.closeTag(key);
e4c71b31
}
8407d456
} } else if (typeof(obj[key]) == 'object') {
753f6909
self.openTag(key); self.parse(obj[key]); self.closeTag(key);
8407d456
} } };
753f6909
ToXml.prototype.openTag = function(key) { this.completeTag(); this.xml += '<' + key; this.tagIncomplete = true; } ToXml.prototype.addAttr = function(key, val) {
1af71b6c
if (this.options.sanitize) {
39f3d232
val = sanitizer.sanitize(val, false, true);
1af71b6c
}
753f6909
this.xml += ' ' + key + '="' + val + '"'; } ToXml.prototype.addTextContent = function(text) { this.completeTag();
e5230ad7
var newText = (this.options.sanitize ? sanitizer.sanitize(text) : text); this.xml += newText;
753f6909
} ToXml.prototype.closeTag = function(key) { this.completeTag(); this.xml += '</' + key + '>'; } ToXml.prototype.completeTag = function() { if (this.tagIncomplete) { this.xml += '>'; this.tagIncomplete = false; } }
34616418
function ToXml(options) { var defaultOpts = { sanitize: false };
1af71b6c
if (options) { for (var opt in options) { defaultOpts[opt] = options[opt]; } }
34616418
1af71b6c
this.options = defaultOpts;
753f6909
this.xml = ''; this.tagIncomplete = false; }