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

add a config.alwaysObjects #21

Open
wants to merge 4 commits 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ The library is very small and has no any dependencies.
* `xmlElementsFilter : []` - Filter incoming XML elements. You can pass a stringified path (like 'parent.child1.child2'), regexp or function
* `jsonPropertiesFilter : []` - Filter JSON properties for output XML. You can pass a stringified path (like 'parent.child1.child2'), regexp or function
* `keepCData : true|false` - If this property defined as false and an XML element has only CData node it will be converted to text without additional property "__cdata". Default is false.
* `alwaysObjects : true|false` - If this property defined as true and an XML element is always parsed as Object indipendently of presence of attributes. Default is false.

## Online demo

Expand Down
8 changes: 4 additions & 4 deletions xml2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,15 +276,15 @@
delete result["#cdata-section_asArray"];
}

if( result.__cnt == 0 && config.emptyNodeForm=="text" ) {
if( result.__cnt == 0 && config.emptyNodeForm=="text" && !config.alwaysObjects) {
result = '';
}
else
if( result.__cnt == 1 && result.__text!=null ) {
if( result.__cnt == 1 && result.__text!=null && !config.alwaysObjects ) {
result = result.__text;
}
else
if( result.__cnt == 1 && result.__cdata!=null && !config.keepCData ) {
if( result.__cnt == 1 && result.__cdata!=null && !config.keepCData && !config.alwaysObjects ) {
result = result.__cdata;
}
else
Expand Down Expand Up @@ -582,4 +582,4 @@
return VERSION;
};
}
}))
}))