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

Commit 910fa6d

Browse files
committed
merge with master
2 parents b24c9eb + d02893c commit 910fa6d

File tree

7 files changed

+47
-26
lines changed

7 files changed

+47
-26
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ var options = {
5050

5151
* **object:** Returns a Javascript object instead of a JSON string
5252
* **reversible:** Makes the JSON reversible to XML (*)
53-
* **coerce:** Makes type coercion. i.e.: numbers and booleans present in attributes and element values are converted from string to its correspondent data types.
53+
* **coerce:** Makes type coercion. i.e.: numbers and booleans present in attributes and element values are converted from string to its correspondent data types. Coerce can be optionally defined as an object with specific methods of coercion based on attribute name or tag name, with fallback to default coercion.
5454
* **trim:** Removes leading and trailing whitespaces as well as line terminators in element values.
5555
* **arrayNotation:** XML child nodes are always treated as arrays
5656
* **sanitize:** Sanitizes the following characters present in element values:

lib/xml2json.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function startElement(name, attrs) {
1515
if(options.coerce) {
1616
// Looping here in stead of making coerce generic as object walk is unnecessary
1717
for(var key in attrs) {
18-
attrs[key] = coerce(attrs[key]);
18+
attrs[key] = coerce(attrs[key],key);
1919
}
2020
}
2121

@@ -64,7 +64,7 @@ function endElement(name) {
6464
currentObject['$t'] = sanitizer.sanitize(currentObject['$t']);
6565
}
6666

67-
currentObject['$t'] = coerce(currentObject['$t']);
67+
currentObject['$t'] = coerce(currentObject['$t'],name);
6868
}
6969

7070
if (currentElementName !== name) {
@@ -86,11 +86,14 @@ function endElement(name) {
8686
currentObject = ancestor;
8787
}
8888

89-
function coerce(value) {
89+
function coerce(value,key) {
9090
if (!options.coerce || value.trim() === '') {
9191
return value;
9292
}
9393

94+
if (typeof options.coerce[key] === 'function')
95+
return options.coerce[key](value);
96+
9497
var num = Number(value);
9598
if (!isNaN(num)) {
9699
return num;

package.json

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
{
2-
"name": "xml2json",
3-
"version": "0.6.1",
4-
"author": "Andrew Turley",
5-
"email": "[email protected]",
6-
"description": "Converts xml to json and vice-versa, using node-expat.",
7-
"repository": "git://github.com/buglabs/node-xml2json.git",
8-
"license": "MIT",
9-
"main": "index",
10-
"scripts": {
11-
"test": "make test"
12-
},
13-
"contributors": [
14-
{
15-
"name": "Camilo Aguilar",
16-
"email": "[email protected]"
1+
{ "name" : "xml2json",
2+
"version": "0.7.1",
3+
"author": "Andrew Turley",
4+
"email": "[email protected]",
5+
"description" : "Converts xml to json and vice-versa, using node-expat.",
6+
"repository": "git://github.com/buglabs/node-xml2json.git",
7+
"license": "MIT",
8+
"main": "index",
9+
"contributors":[
10+
{"name": "Camilo Aguilar", "email": "[email protected]"}
11+
],
12+
"dependencies": {
13+
"node-expat": "^2.3.7"
14+
},
15+
"bin": {
16+
"xml2json": "bin/xml2json"
1717
}
1818
],
1919
"dependencies": {

test/fixtures/coerce.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"itemRecord":{"value":[{"longValue":"12345"},{"stringValue":{"number":"false","$t":"this is a string value"}},{"moneyValue":{"number":"true","currencyId":"USD","$t":"104.95"}},{"moneyValue":{"currencyId":"USD","$t":"104.95"}},{"longValue":"0","bool":{"id":"0","$t":"true"}},{"longValue":"0"},{"dateValue":"2012-02-16T17:03:33.000-07:00"},{"stringValue":"SmDZ8RlMIjDvlEW3KUibzj2Q"}]}}
1+
{"itemRecord":{"value":[{"longValue":"12345"},{"stringValue":{"number":"false","$t":"this is a string value"}},{"moneyValue":{"number":"true","currencyId":"USD","text":"123.45","$t":"104.95"}},{"moneyValue":{"currencyId":"USD","$t":"104.95"}},{"longValue":"0","bool":{"id":"0","$t":"true"}},{"longValue":"0"},{"dateValue":"2012-02-16T17:03:33.000-07:00"},{"stringValue":"SmDZ8RlMIjDvlEW3KUibzj2Q"},{"text":"42.42"}]}}

test/fixtures/coerce.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</stringValue>
88
</value>
99
<value>
10-
<moneyValue number='true' currencyId="USD">104.95
10+
<moneyValue number='true' currencyId="USD" text="123.45">104.95
1111
</moneyValue>
1212
</value>
1313
<value>
@@ -27,4 +27,7 @@
2727
<value>
2828
<stringValue>SmDZ8RlMIjDvlEW3KUibzj2Q</stringValue>
2929
</value>
30+
<value>
31+
<text>42.42</text>
32+
</value>
3033
</itemRecord>

test/test-coerce.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,24 @@ assert.strictEqual(result.itemRecord.value[0].longValue['$t'], 12345);
1313
assert.strictEqual(result.itemRecord.value[1].stringValue.number, false);
1414
assert.strictEqual(result.itemRecord.value[2].moneyValue.number, true);
1515
assert.strictEqual(result.itemRecord.value[2].moneyValue['$t'], 104.95);
16+
assert.strictEqual(result.itemRecord.value[2].moneyValue.text, 123.45);
17+
assert.strictEqual(result.itemRecord.value[8].text['$t'], 42.42);
18+
1619

1720
// Without coercion
1821
result = parser.toJson(data, {reversible: true, coerce: false, object: true});
1922
assert.strictEqual(result.itemRecord.value[0].longValue['$t'], '12345');
2023
assert.strictEqual(result.itemRecord.value[1].stringValue.number, 'false');
2124
assert.strictEqual(result.itemRecord.value[2].moneyValue.number, 'true');
22-
assert.strictEqual(result.itemRecord.value[2].moneyValue['$t'], '104.95');
25+
assert.strictEqual(result.itemRecord.value[2].moneyValue['$t'], '104.95');
26+
assert.strictEqual(result.itemRecord.value[2].moneyValue.text, '123.45');
27+
assert.strictEqual(result.itemRecord.value[8].text['$t'], '42.42');
28+
29+
// With coercion as an optional object
30+
var result = parser.toJson(data, {reversible: true, coerce: {text:String}, object: true});
31+
assert.strictEqual(result.itemRecord.value[0].longValue['$t'], 12345);
32+
assert.strictEqual(result.itemRecord.value[1].stringValue.number, false);
33+
assert.strictEqual(result.itemRecord.value[2].moneyValue.number, true);
34+
assert.strictEqual(result.itemRecord.value[2].moneyValue['$t'], 104.95);
35+
assert.strictEqual(result.itemRecord.value[2].moneyValue.text, '123.45');
36+
assert.strictEqual(result.itemRecord.value[8].text['$t'], '42.42');

test/test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('xml2json', function () {
3636
var result = parser.toJson(xml, { coerce: false });
3737
var json = internals.readFixture('coerce.json');
3838

39-
expect(result).to.deep.equal(json);
39+
expect(result + '\n').to.deep.equal(json);
4040

4141
done();
4242
});
@@ -47,7 +47,7 @@ describe('xml2json', function () {
4747
var result = parser.toJson(xml, { coerce: false });
4848
var json = internals.readFixture('domain.json');
4949

50-
expect(result+'\n').to.deep.equal(json);
50+
expect(result + '\n').to.deep.equal(json);
5151

5252
done();
5353
});
@@ -58,7 +58,7 @@ describe('xml2json', function () {
5858
var result = parser.toJson(xml, { coerce: false, trim: true, sanitize: false });
5959
var json = internals.readFixture('large.json');
6060

61-
expect(result+'\n').to.deep.equal(json);
61+
expect(result + '\n').to.deep.equal(json);
6262

6363
done();
6464
});
@@ -108,6 +108,7 @@ describe('xml2json', function () {
108108
});
109109
});
110110

111+
111112
describe('json2xml', function () {
112113

113114
it('converts domain to json', function (done) {

0 commit comments

Comments
 (0)