Skip to content

Commit

Permalink
options.textNodeKey specifies the json key for a node's text data. if…
Browse files Browse the repository at this point in the history
… unspecified, defaults to usual $t.
  • Loading branch information
Huned Botee committed Feb 1, 2012
1 parent 692b944 commit 1d0aff2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ console.log(json);
``` ```
* if you want to get the Javascript object then you might want to invoke parser.toJson(xml, {object: true}); * if you want to get the Javascript object then you might want to invoke parser.toJson(xml, {object: true});
* if you want a reversible json to xml then you should use parser.toJson(xml, {reversible: true}); * if you want a reversible json to xml then you should use parser.toJson(xml, {reversible: true});

* if you want to override the default "$t" key name for a node's text value, specify the textNodeKey option. e.g., `parser.toJson(xml, {textNodeKey: "text"})`


## License ## License
Copyright 2011 BugLabs Inc. All rights reserved. Copyright 2011 BugLabs Inc. All rights reserved.
11 changes: 6 additions & 5 deletions lib/xml2json.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ function text(data) {
if (!data.length) { if (!data.length) {
return; return;
} }
currentObject['$t'] = (currentObject['$t'] || "") + data; currentObject[options.textNodeKey] = (currentObject[options.textNodeKey] || "") + data;
} }


function endElement(name) { function endElement(name) {
// This should check to make sure that the name we're ending // This should check to make sure that the name we're ending
// matches the name we started on. // matches the name we started on.
var ancestor = ancestors.pop(); var ancestor = ancestors.pop();
if (!options.reversible) { if (!options.reversible) {
if ((Object.keys(currentObject).length == 1) && ('$t' in currentObject)) { if ((Object.keys(currentObject).length == 1) && (options.textNodeKey in currentObject)) {
if (ancestor[name] instanceof Array) { if (ancestor[name] instanceof Array) {
ancestor[name].push(ancestor[name].pop()['$t']); ancestor[name].push(ancestor[name].pop()[options.textNodeKey]);
} else { } else {
ancestor[name] = currentObject['$t']; ancestor[name] = currentObject[options.textNodeKey];
} }
} }
} }
Expand All @@ -71,7 +71,8 @@ module.exports = function(xml, _options) {


options = { options = {
object: false, object: false,
reversible: false reversible: false,
textNodeKey: '$t'
}; };


for (var opt in _options) { for (var opt in _options) {
Expand Down
12 changes: 12 additions & 0 deletions test/test.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -47,3 +47,15 @@ fs.readdir(fixturesPath, function(err, files) {
} }
}); });


// test options.textNodeKey custom value
var xml = '<ref href="http://example.com">http://example.com</ref>';
var actual = parser.toJson(xml, {object: true, textNodeKey: 'hi'});
var json = '{"ref":{"href":"http://example.com","hi":"http://example.com"}}';
assert.deepEqual(actual, JSON.parse(json));

// test options.textNodeKey default value
var actual = parser.toJson(xml, {object: true});
var json = json.replace('"hi":', '"$t":');
assert.deepEqual(actual, JSON.parse(json));

console.log('[xml2json options.textNodeKey] passed');

0 comments on commit 1d0aff2

Please sign in to comment.