Toggle navigation
P3X GitList Snapshot
GitHub
Repo
Changelog
To do
Releases
Themes
Change log
Loading change log ...
To do ...
Loading todo ...
browsing:
d2120a9142abf117f2854fd201e83994e19e4158
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
src
json2xml.js
RSS
Git
Fetch origin
Download
ZIP
TAR
Clone
Raw
View
History
Clone
SSH
HTTPS
Blames found: 61
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;
17a24607
if (typeof (json) == 'string') {
8407d456
try { obj = JSON.parse(json);
17a24607
} catch (e) {
8407d456
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
17a24607
ToXml.prototype.parse = function (obj) {
2d87cdb1
if (!obj) return;
753f6909
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);
17a24607
var type = typeof (value);
69a937d4
if (type == 'string' || type == 'number' || type == 'boolean' || isArray) {
e4c71b31
var it = isArray ? value : [value];
29b9b183
17a24607
it.forEach(function (subVal) { if (typeof (subVal) != 'object') {
e4c71b31
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
17a24607
if (typeof (elem) == 'object') {
753f6909
self.openTag(key); self.parse(elem); self.closeTag(key);
e4c71b31
}
8407d456
}
17a24607
} else if (typeof (obj[key]) == 'object' && !(self.options.ignoreNull && obj[key] === null)) {
753f6909
self.openTag(key); self.parse(obj[key]); self.closeTag(key);
8407d456
} } };
17a24607
ToXml.prototype.openTag = function (key) {
753f6909
this.completeTag(); this.xml += '<' + key; this.tagIncomplete = true; }
17a24607
ToXml.prototype.addAttr = function (key, val) {
1af71b6c
if (this.options.sanitize) {
39f3d232
val = sanitizer.sanitize(val, false, true);
1af71b6c
}
753f6909
this.xml += ' ' + key + '="' + val + '"'; }
17a24607
ToXml.prototype.addTextContent = function (text) {
753f6909
this.completeTag();
e5230ad7
var newText = (this.options.sanitize ? sanitizer.sanitize(text) : text); this.xml += newText;
753f6909
}
17a24607
ToXml.prototype.closeTag = function (key) {
753f6909
this.completeTag(); this.xml += '</' + key + '>'; }
17a24607
ToXml.prototype.completeTag = function () {
753f6909
if (this.tagIncomplete) { this.xml += '>'; this.tagIncomplete = false; } }
17a24607
34616418
function ToXml(options) { var defaultOpts = {
2d87cdb1
sanitize: false, ignoreNull: false
34616418
};
1af71b6c
if (options) { for (var opt in options) { defaultOpts[opt] = options[opt]; } }
34616418
1af71b6c
this.options = defaultOpts;
753f6909
this.xml = ''; this.tagIncomplete = false; }