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

Commit 555ff88

Browse files
authored
Merge pull request #103 from dannyrscott/master
allow arrayNotation to accept an array
2 parents 7fa9541 + 771b3a3 commit 555ff88

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

lib/xml2json.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function startElement(name, attrs) {
2020
}
2121

2222
if (! (name in currentObject)) {
23-
if(options.arrayNotation) {
23+
if(options.arrayNotation || options.forceArrays[name]) {
2424
currentObject[name] = [attrs];
2525
} else {
2626
currentObject[name] = attrs;
@@ -147,12 +147,18 @@ module.exports = function(xml, _options) {
147147
coerce: joi.alternatives([joi.boolean(), joi.object()]).default(false),
148148
sanitize: joi.boolean().default(true),
149149
trim: joi.boolean().default(true),
150-
arrayNotation: joi.boolean().default(false)
150+
arrayNotation: joi.alternatives([joi.boolean(), joi.array()]).default(false)
151151
};
152152
var validation = joi.validate(_options, schema);
153153
hoek.assert(validation.error === null, validation.error);
154154
options = validation.value;
155-
155+
options.forceArrays = {};
156+
if (Array.isArray(options.arrayNotation)) {
157+
options.arrayNotation.forEach(function(i) {
158+
options.forceArrays[i] = true;
159+
});
160+
options.arrayNotation = false;
161+
}
156162
if (!parser.parse(xml)) {
157163
throw new Error('There are errors in your xml file: ' + parser.getError());
158164
}

test/fixtures/forceArray.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"json":{"id":"123b","vehicles":[{"year":"1989","annual_mileage":"18000","average_mileage":"30","alarm":"1","ownership":"Financed","make":"TOYOTA","model":"Camry","commute_days_per_week":"5","collision":"$ 500","comprehensive":"$ 500","primary_purpose":"Pleasure"},{"year":"2006","annual_mileage":"37500","average_mileage":"10","alarm":"1","ownership":"Owned","make":"CHRYSLER","model":"CROSSFIRE","commute_days_per_week":"3","collision":"$ 5000","comprehensive":"$ 500","primary_purpose":"CommuteWork"}],"drivers":[{"driver":"Bray Wyatt","birth_date":"1987-05-23","marital_status":"Single","relationship":"Self","gender":"Male","first_licensed":"18","license_status":"ValidLicense","occupation":"Unemployed","education":"HighSchoolDiploma","credit_rating":"Unsure"}]}}

test/fixtures/forceArray.xml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0"?>
2+
<json>
3+
<id>123b</id>
4+
<vehicles>
5+
<year>1989</year>
6+
<annual_mileage>18000</annual_mileage>
7+
<average_mileage>30</average_mileage>
8+
<alarm>1</alarm>
9+
<ownership>Financed</ownership>
10+
<make>TOYOTA</make>
11+
<model>Camry</model>
12+
<commute_days_per_week>5</commute_days_per_week>
13+
<collision>$ 500</collision>
14+
<comprehensive>$ 500</comprehensive>
15+
<primary_purpose>Pleasure</primary_purpose>
16+
</vehicles>
17+
<vehicles>
18+
<year>2006</year>
19+
<annual_mileage>37500</annual_mileage>
20+
<average_mileage>10</average_mileage>
21+
<alarm>1</alarm>
22+
<ownership>Owned</ownership>
23+
<make>CHRYSLER</make>
24+
<model>CROSSFIRE</model>
25+
<commute_days_per_week>3</commute_days_per_week>
26+
<collision>$ 5000</collision>
27+
<comprehensive>$ 500</comprehensive>
28+
<primary_purpose>CommuteWork</primary_purpose>
29+
</vehicles>
30+
<drivers>
31+
<driver>Bray Wyatt</driver>
32+
<birth_date>1987-05-23</birth_date>
33+
<marital_status>Single</marital_status>
34+
<relationship>Self</relationship>
35+
<gender>Male</gender>
36+
<first_licensed>18</first_licensed>
37+
<license_status>ValidLicense</license_status>
38+
<occupation>Unemployed</occupation>
39+
<education>HighSchoolDiploma</education>
40+
<credit_rating>Unsure</credit_rating>
41+
</drivers>
42+
</json>

test/test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ describe('xml2json', function () {
107107
done();
108108
});
109109

110+
it('converts with forceArrays', function(done) {
111+
var xml = internals.readFixture('forceArray.xml');
112+
var result = parser.toJson(xml, {arrayNotation: ['drivers', 'vehicles']});
113+
var json = internals.readFixture('forceArray.json');
114+
115+
expect(result).to.deep.equal(json);
116+
done();
117+
});
118+
110119
it('throws error on bad options', function (done) {
111120

112121
var throws = function() {

0 commit comments

Comments
 (0)