Skip to content

Commit

Permalink
Merge pull request estheban#14 from toiine/fix-issue-13_undefined-null
Browse files Browse the repository at this point in the history
fixed bug with undefined and null values
  • Loading branch information
estheban committed Mar 17, 2015
2 parents c4720fc + 1d5401c commit 28e1a6c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/json2xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ module.exports = function xml(json, opts) {
var node = json[key],
attributes = '';

if(typeof node === 'undefined' || null === node) node = '';

if(opts.attributes_key && json[opts.attributes_key]){
Object.keys(json[opts.attributes_key]).forEach(function(k){
attributes += util.format(' %s="%s"', k, json[opts.attributes_key][k]);
Expand Down
6 changes: 4 additions & 2 deletions test/json2xml_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,18 @@ exports['json2xml'] = {
test.done();
},
'opts': function(test) {
test.expect(6);
test.expect(8);
test.equal(json2xml({a:''}),'<a/>');
test.equal(json2xml({a:null}),'<a/>');
test.equal(json2xml({a:undefined}),'<a/>');

test.equal(json2xml({a:1}),'<a>1</a>');
test.equal(json2xml([{a:1}, {b:2}]),'<a>1</a><b>2</b>');

test.equal(json2xml({ 'items':[{item:1},{item:2}]} ),'<items><item>1</item><item>2</item></items>');

test.equal(json2xml({a:1}, { header:true }),'<?xml version="1.0" encoding="UTF-8"?><a>1</a>');
test.equal(json2xml({a:1, attr:{b:2,c:3 }}, { attributes_key:'attr' }), '<a b="2" c="3">1</a>');
test.equal(json2xml({a:1, attr:{b:2,c:3 }}, { attributes_key:'attr' }), '<a b="2" c="3">1</a>');
test.done();
}

Expand Down

0 comments on commit 28e1a6c

Please sign in to comment.