Skip to content

Commit

Permalink
Revert "Merge pull request #91 from node-xmpp/no-json-2" (#97)
Browse files Browse the repository at this point in the history
This reverts commit a1fc70c, reversing
changes made to 6ccfc52.
  • Loading branch information
hyperlink authored and sonnyp committed Jun 7, 2016
1 parent 9369d01 commit 132150e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/Element.js
Expand Up @@ -318,6 +318,16 @@ Element.prototype.toString = function () {
return s
}

Element.prototype.toJSON = function () {
return {
name: this.name,
attrs: this.attrs,
children: this.children.map(function (child) {
return child && child.toJSON ? child.toJSON() : child
})
}
}

Element.prototype._addChildren = function (writer) {
writer('>')
for (var i = 0; i < this.children.length; i++) {
Expand Down
10 changes: 10 additions & 0 deletions test/element-test.js
Expand Up @@ -82,6 +82,16 @@ vows.describe('Element').addBatch({
'serialize with integer text': function () {
var e = new Element('e').t(1000)
assert.equal(e.getText(), 1000)
},
'serialize to json': function () {
var e = new Element('e', { foo: 23, bar: 0, nil: null }).c('f').t(1000).up()
assert.deepEqual(e.toJSON(), {
name: 'e',
attrs: { foo: 23, bar: 0, nil: null },
children: [
{ name: 'f', attrs: {}, children: [1000] }
]
})
}
},
'remove': {
Expand Down

0 comments on commit 132150e

Please sign in to comment.