Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: if object is empty always add emptytag, fixes #647 #712

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions lib/parser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions src/parser.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ defaults = require('./defaults').defaults
isEmpty = (thing) ->
return typeof thing is "object" && thing? && Object.keys(thing).length is 0

hasNoContent = (thing) ->
return typeof thing is "object" && thing? && (Object.keys(thing).length is 0 || (Object.keys(thing).length is 1 && '$' of thing))

processItem = (processors, item, key) ->
item = process(item, key) for process in processors
return item
Expand Down Expand Up @@ -154,11 +157,15 @@ class exports.Parser extends events
if Object.keys(obj).length == 1 and charkey of obj and not @EXPLICIT_CHARKEY
obj = obj[charkey]

if typeof @options.emptyTag == 'function'
emptyTag = @options.emptyTag()
else
emptyTag = if @options.emptyTag != '' then @options.emptyTag else emptyStr

if (isEmpty obj)
if typeof @options.emptyTag == 'function'
obj = @options.emptyTag()
else
obj = if @options.emptyTag != '' then @options.emptyTag else emptyStr
obj = emptyTag
else if (hasNoContent obj)
obj['_'] = emptyTag

if @options.validator?
xpath = "/" + (node["#name"] for node in stack).concat(nodeName).join("/")
Expand Down
26 changes: 26 additions & 0 deletions test/parser.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,32 @@ module.exports =
equ r.sample.listtest[0].item[1], 'Qux.'
equ r.sample.listtest[0].item[2], 'Quux.')

'test parseStringPromise with emptyTag and explicitArray': (test) ->
xml = """
<test xmlns="http://example.com/namespace">
<value1>value1</value1>
<value2></value2>
<value3 xmlns="http://example.com/namespace">value3</value3>
<value4 xmlns="http://example.com/namespace"></value4>
</test>
"""
parser = new xml2js.Parser
emptyTag: '--EMPTY--'
explicitArray: false

parser.parseStringPromise(xml)
.then (result) ->
equ result.test.$?.xmlns, 'http://example.com/namespace'
equ result.test.value1, 'value1'
equ result.test.value2, '--EMPTY--'
equ result.test.value3._, 'value3'
equ result.test.value3.$?.xmlns, 'http://example.com/namespace'
equ result.test.value4._, '--EMPTY--'
equ result.test.value4.$?.xmlns, 'http://example.com/namespace'
test.finish()
.catch (err) ->
test.fail('Should not error: ' + err)

'test simple callback mode': (test) ->
x2js = new xml2js.Parser()
fs.readFile fileName, (err, data) ->
Expand Down