Skip to content
This repository was archived by the owner on Jan 7, 2021. It is now read-only.

Commit e982acb

Browse files
committed
Add toJson arrayNotation option
1 parent 976a158 commit e982acb

6 files changed

Lines changed: 45 additions & 17 deletions

File tree

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Simple SAX-based XML2JSON Parser.
22

3-
It does not parse the following elements:
3+
It does not parse the following elements:
44

55
* CDATA sections (*)
66
* Processing instructions
@@ -13,7 +13,7 @@ It does not parse the following elements:
1313
$ npm install xml2json
1414
```
1515

16-
## Usage
16+
## Usage
1717
```javascript
1818
var parser = require('xml2json');
1919

@@ -39,18 +39,20 @@ var options = {
3939
reversible: false,
4040
coerce: true,
4141
sanitize: true,
42-
trim: true
42+
trim: true,
43+
arrayNotation: false
4344
};
4445
```
4546

4647
* **object:** Returns a Javascript object instead of a JSON string
4748
* **reversible:** Makes the JSON reversible to XML (*)
4849
* **coerce:** Makes type coercion. i.e.: numbers and booleans present in attributes and element values are converted from string to its correspondent data types.
4950
* **trim:** Removes leading and trailing whitespaces as well as line terminators in element values.
51+
* **arrayNotation:** XML child nodes are always treated as arrays
5052
* **sanitize:** Sanitizes the following characters present in element values:
5153

5254
```javascript
53-
var chars = {
55+
var chars = {
5456
'<': '&lt;',
5557
'>': '&gt;',
5658
'(': '&#40;',
@@ -88,4 +90,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
8890
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8991
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
9092
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
91-
IN THE SOFTWARE.
93+
IN THE SOFTWARE.

lib/xml2json.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var expat = require('node-expat');
22
var fs = require('fs');
33

44
// This object will hold the final result.
5-
var obj = {};
5+
var obj = {};
66
var currentObject = {};
77
var ancestors = [];
88
var currentElementName = null;
@@ -18,7 +18,11 @@ function startElement(name, attrs) {
1818
}
1919

2020
if (! (name in currentObject)) {
21-
currentObject[name] = attrs;
21+
if(options.arrayNotation) {
22+
currentObject[name] = [attrs];
23+
} else {
24+
currentObject[name] = attrs;
25+
}
2226
} else if (! (currentObject[name] instanceof Array)) {
2327
// Put the existing object in an array.
2428
var newArray = [currentObject[name]];
@@ -49,7 +53,7 @@ function text(data) {
4953
/*if (!data.trim().length) {
5054
return;
5155
}*/
52-
56+
5357
if (options.trim) {
5458
data = data.trim();
5559
}
@@ -65,7 +69,7 @@ function endElement(name) {
6569
if (currentElementName !== name) {
6670
delete currentObject['$t'];
6771
}
68-
// This should check to make sure that the name we're ending
72+
// This should check to make sure that the name we're ending
6973
// matches the name we started on.
7074
var ancestor = ancestors.pop();
7175
if (!options.reversible) {
@@ -85,14 +89,14 @@ function coerce(value) {
8589
if (!options.coerce) {
8690
return value;
8791
}
88-
92+
8993
var num = Number(value);
9094
if (!isNaN(num)) {
9195
return num;
9296
}
9397

9498
var _value = value.toLowerCase();
95-
99+
96100
if (_value == 'true' || _value == 'yes') {
97101
return true;
98102
}
@@ -106,7 +110,7 @@ function coerce(value) {
106110

107111

108112
/**
109-
* Simple sanitization. It is not intended to sanitize
113+
* Simple sanitization. It is not intended to sanitize
110114
* malicious element values.
111115
*
112116
* character | escaped
@@ -151,7 +155,7 @@ function sanitize(value) {
151155
* characterized by the presence of the property $t.
152156
* - sanitize_values: If true, the parser escapes any element value in the xml
153157
* that has any of the following characters: <, >, (, ), #, #, &, ", '.
154-
*
158+
*
155159
* @return {String|Object} A String or an Object with the JSON representation
156160
* of the XML.
157161
*/
@@ -190,7 +194,6 @@ module.exports = function(xml, _options) {
190194

191195
//See: http://timelessrepo.com/json-isnt-a-javascript-subset
192196
json = json.replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029');
193-
197+
194198
return json;
195199
};
196-

test/fixtures/array-notation.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"abcd":[{"efg":[{"hijk":["qrstuv",{"lmnop":["wxyz"]}]}]}]}

test/fixtures/array-notation.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<abcd>
2+
<efg>
3+
<hijk>qrstuv</hijk>
4+
<hijk>
5+
<lmnop>wxyz</lmnop>
6+
</hijk>
7+
</efg>
8+
</abcd>

test/test-array-notation.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var fs = require('fs');
2+
var parser = require('../lib');
3+
var assert = require('assert');
4+
5+
var xml = fs.readFileSync('./fixtures/array-notation.xml');
6+
var expectedJson = JSON.parse( fs.readFileSync('./fixtures/array-notation.json') );
7+
8+
var json = parser.toJson(xml, {object: true, arrayNotation: true});
9+
10+
assert.deepEqual(json, expectedJson);
11+
12+
console.log('xml2json options.arrayNotation passed!');

test/test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ fs.readdir(fixturesPath, function(err, files) {
2525
result = parser.toJson(data2, {coerce: false});
2626
} else if (file.indexOf('large') >= 0) {
2727
result = parser.toJson(data2, {coerce: false, trim: true, sanitize: false});
28+
} else if (file.indexOf('array-notation') >= 0) {
29+
result = parser.toJson(data2, {arrayNotation: true});
2830
} else {
2931
result = parser.toJson(data2, {trim: false});
3032
}
@@ -45,10 +47,10 @@ fs.readdir(fixturesPath, function(err, files) {
4547
if (basename.match('reversible')) {
4648
var data = fs.readFileSync(fixturesPath + '/' + file);
4749
var result = parser.toXml(data);
48-
50+
4951
var xmlFile = basename.split('-')[0] + '.xml';
5052
var expected = fs.readFileSync(fixturesPath + '/' + xmlFile) + '';
51-
53+
5254
if (expected) {
5355
expected = expected.trim();
5456
}

0 commit comments

Comments
 (0)