Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Empty Array from JSON generates Element #190

Closed
kaipaysen opened this issue Nov 16, 2018 · 6 comments
Closed

Empty Array from JSON generates Element #190

kaipaysen opened this issue Nov 16, 2018 · 6 comments
Labels

Comments

@kaipaysen
Copy link

kaipaysen commented Nov 16, 2018

Whats wrong?

Building a xml from a json object with an empty array creates an unexpected element in the xml.
(Version 10.1.1)

Code

        let jsonData = { 
            root: {
                item: []
            } 
        };
        console.log(xmlbuilder
            .create(jsonData, { encoding: 'utf-8' })
            .end({ pretty: true })
        );

Output

<?xml version="1.0" encoding="utf-8"?>
<root>
  <item/>
</root>

expected Output

<?xml version="1.0" encoding="utf-8"?>
<root />
@oozcitak
Copy link
Owner

This is not a bug. { item: [] } is interpreted as a node (item) without any child nodes ([]). Accordingly a single node without children are created (<item/>).

@kaipaysen
Copy link
Author

kaipaysen commented Nov 26, 2018

Ok, well, just to get it right:

item: []
equals
item: {}
equals
item: [{}]

This does feel a bit wobbly. I'd totally agree about the 2nd and 3rd case producing one empty "item" element. An empty array producing an empty element seems to be just wrong. How can an element be created from nothing? Whats the rational behind that decision?

@oozcitak
Copy link
Owner

I gave this some more thought. You are right about arrays:

  • item: [] should produce no nodes.
  • item: [] should produce one item node without child nodes, if separateArrayItems is true.
  • item: {} should produce one item node without child nodes.

@oozcitak oozcitak added the Bug label Jan 22, 2019
@Simran-B
Copy link

Hm, what about item: null or item: undefined (not in JSON, only JS)?

@kaipaysen
Copy link
Author

Good point @Simran-B. In both cases I'd expect no element.

Case Expected Output
item: null
item: undefined
item: []
item: {} <item />
item: [{}] <item />

@oozcitak
Copy link
Owner

oozcitak commented Feb 5, 2019

Fixed by 66f8165, be07739 and 782540c.

@oozcitak oozcitak closed this as completed Feb 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants