Skip to content

Commit c35edfc

Browse files
author
Jean-Philippe Gravel
committed
Merge pull request #44 from formix/anyDepthDeclarer
Any depth declarer
2 parents 3cdba58 + 3c3fc92 commit c35edfc

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

damn-simple-xml.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ function createTagContent(root, behavior, fieldPath, level, attrset, buffer, cal
358358
var itemName = root.name + "Item";
359359
if (behavior.arrays[fieldPath]) {
360360
itemName = behavior.arrays[fieldPath];
361+
} else if (behavior.arrays["*." + root.name]) {
362+
itemName = behavior.arrays["*." + root.name];
361363
}
362364
for (var i = 0; i < root.data.length; i++) {
363365
var item = root.data[i];
@@ -442,10 +444,10 @@ function convert(value) {
442444
}
443445

444446

445-
function createObject(stack, nodeName, arrayNameSet) {
447+
function createObject(stack, nodeName, arrays) {
446448
var obj = {};
447449
var fullName = createNodeName(stack, nodeName);
448-
if (arrayNameSet[fullName]) {
450+
if (arrays[fullName] || arrays["*." + nodeName]) {
449451
obj = [];
450452
}
451453
return obj;
@@ -512,3 +514,15 @@ function countFields(obj) {
512514
}
513515
return count;
514516
}
517+
518+
519+
function mergeArrays(a1, a2) {
520+
var target = [];
521+
for (var i = 0; i < a1.length; i++) {
522+
target.push(a1[i]);
523+
}
524+
for (var j = 0; j < a2.length; j++) {
525+
target.push(a2[j]);
526+
}
527+
return target;
528+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "damn-simple-xml",
3-
"version": "1.1.3",
3+
"version": "1.2.0",
44
"description": "XML serialization library meant to simplify programmer's life",
55
"author": "Jean-Philippe Gravel <jeanphilippe.gravel@gmail.com>",
66
"homepage": "https://github.com/formix/damn-simple-xml",

test/test-deserialize.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,70 @@ describe("DamnSimpleXml.deserialize()", function() {
372372

373373
});
374374

375+
376+
377+
describe("using '*.' declarators", function() {
378+
379+
it("should deserialize arrays at any depth", function(done) {
380+
381+
var expected = {
382+
name: "root",
383+
children: [
384+
{
385+
name: "child_1",
386+
children: [
387+
{
388+
name: "child_1_1",
389+
children: []
390+
},
391+
{
392+
name: "child_1_2",
393+
children: []
394+
}
395+
]
396+
}
397+
]
398+
}
375399

400+
var data =
401+
"<item>" +
402+
"<name>root</name>" +
403+
"<children>" +
404+
"<item>" +
405+
"<name>child_1</name>" +
406+
"<children>" +
407+
"<item>" +
408+
"<name>child_1_1</name>" +
409+
"<children/>"+
410+
"</item>" +
411+
"<item>" +
412+
"<name>child_1_2</name>" +
413+
"<children/>" +
414+
"</item>" +
415+
"</children>" +
416+
"</item>" +
417+
"</children>" +
418+
"</item>";
419+
420+
421+
var dsx = new Serializer({
422+
arrays: {
423+
"*.children": "item"
424+
}
425+
});
426+
427+
428+
dsx.deserialize(data, function(err, obj) {
429+
assert.ifError(err);
430+
assert.deepEqual(obj.data, expected);
431+
done();
432+
});
433+
434+
});
435+
436+
// Add other declarators implementation and depth
437+
438+
});
376439

377440

378441
});

0 commit comments

Comments
 (0)