Skip to content

Commit 57c414d

Browse files
committed
Added failing test for multinode XML with JSON in it.
1 parent a15ede1 commit 57c414d

File tree

2 files changed

+90
-50
lines changed

2 files changed

+90
-50
lines changed

lib/fml.js

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ class FusionMarkupLanguage {
4848
const result = JSON.parse(filteredInputText)
4949

5050
return result
51-
}
52-
catch (e) {
51+
} catch (e) {
5352
if (!e.message.startsWith('Unexpected token < in JSON at position ')) {
5453
throw e
5554
}
@@ -72,13 +71,6 @@ class FusionMarkupLanguage {
7271

7372
let inputWithoutXml = filteredInputText
7473

75-
// elements
76-
// .split('\n')
77-
// .forEach(xmlSegment => {
78-
// inputWithoutXml = inputWithoutXml.replace(xmlSegment, '')
79-
// })
80-
81-
8274
inputWithoutXml = inputWithoutXml.replace(xmlSubstring, '')
8375

8476
const elementName = this.findElementClosestToPosition(inputWithoutXml, lineNumber)
@@ -106,11 +98,42 @@ class FusionMarkupLanguage {
10698
if (error)
10799
throw error
108100

109-
for (let key of Object.keys(result)) {
110-
try {
111-
result[key] = JSON.parse(result[key])
112-
} catch (e) { }
113-
}
101+
const recursiveReplace = function(obj) {
102+
if (typeof obj === 'string') {
103+
// this.parse(obj)
104+
105+
try {
106+
return JSON.parse(obj)
107+
} catch(e) {}
108+
}
109+
110+
for (let key of Object.keys(obj)) {
111+
let contents = obj[key]
112+
113+
if (typeof contents === 'string') {
114+
// this.parse(obj)
115+
116+
try {
117+
obj[key] = JSON.parse(contents)
118+
} catch(e) {}
119+
} else if (Array.isArray(contents)) {
120+
for (let i = 0; i < contents.length; i++) {
121+
contents[i] = recursiveReplace(contents[i])
122+
}
123+
// for (let item of contents) {
124+
// item = recursiveReplace(item)
125+
// }
126+
} else if (typeof contents === 'object' && contents !== null) {
127+
contents = recursiveReplace(contents)
128+
}
129+
130+
// obj[key] = contents;
131+
}
132+
133+
return obj;
134+
};
135+
136+
result = recursiveReplace(result)
114137

115138
return result;
116139
}

test/test.js

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ describe('FusionMarkupLanguage', () => {
88
const dataType = FML.getDataType(`
99
{
1010
"person": {
11-
<name>
12-
<firstName>Rick</firstName>
13-
<lastName>Astley</lastName>
14-
</name>
11+
<firstName>Rick</firstName>
12+
<lastName>Astley</lastName>
1513
}
1614
}
1715
`)
@@ -24,10 +22,8 @@ describe('FusionMarkupLanguage', () => {
2422
<?xml version="1.0" encoding="UTF-8" ?>
2523
<person>
2624
{
27-
"name": {
28-
"firstName": "Rick",
29-
"lastName": "Astley"
30-
}
25+
"firstName": "Rick",
26+
"lastName": "Astley"
3127
}
3228
</person>
3329
`)
@@ -39,10 +35,8 @@ describe('FusionMarkupLanguage', () => {
3935
const dataType = FML.getDataType(`
4036
?person
4137
{
42-
"name": {
43-
"firstName": "Rick",
44-
"lastName": "Astley"
45-
}
38+
"firstName": "Rick",
39+
"lastName": "Astley"
4640
}
4741
?person
4842
`)
@@ -64,20 +58,16 @@ describe('FusionMarkupLanguage', () => {
6458
const parsed = FML.parse(`
6559
{
6660
"person": {
67-
"name": {
68-
"firstName": "Rick",
69-
"lastName": "Astley"
70-
}
61+
"firstName": "Rick",
62+
"lastName": "Astley"
7163
}
7264
}
7365
`)
7466

7567
assert.deepEqual(parsed, {
7668
person: {
77-
name: {
78-
firstName: "Rick",
79-
lastName: "Astley"
80-
}
69+
firstName: "Rick",
70+
lastName: "Astley"
8171
}
8272
})
8373
})
@@ -86,19 +76,15 @@ describe('FusionMarkupLanguage', () => {
8676
const parsed = FML.parse(`
8777
<?xml version="1.0" encoding="UTF-8" ?>
8878
<person>
89-
<name>
90-
<firstName>Rick</firstName>
91-
<lastName>Astley</lastName>
92-
</name>
79+
<firstName>Rick</firstName>
80+
<lastName>Astley</lastName>
9381
</person>
9482
`)
9583

9684
assert.deepEqual(parsed, {
9785
person: {
98-
name: {
99-
firstName: "Rick",
100-
lastName: "Astley"
101-
}
86+
firstName: "Rick",
87+
lastName: "Astley"
10288
}
10389
})
10490
})
@@ -108,20 +94,51 @@ describe('FusionMarkupLanguage', () => {
10894
<?xml version="1.0" encoding="UTF-8" ?>
10995
<person>
11096
{
111-
"name": {
112-
"firstName": "Rick",
113-
"lastName": "Astley"
114-
}
97+
"firstName": "Rick",
98+
"lastName": "Astley"
11599
}
116100
</person>
117101
`)
118102

119103
assert.deepEqual(parsed, {
120104
person: {
121-
name: {
122-
firstName: "Rick",
123-
lastName: "Astley"
124-
}
105+
firstName: "Rick",
106+
lastName: "Astley"
107+
}
108+
})
109+
})
110+
111+
it('should parse multiple nodes with JSON in XML', () => {
112+
const parsed = FML.parse(`
113+
<?xml version="1.0" encoding="UTF-8" ?>
114+
<people>
115+
<person>
116+
{
117+
"firstName": "Rick",
118+
"lastName": "Astley"
119+
}
120+
</person>
121+
<person>
122+
{
123+
"firstName": "Park",
124+
"lastName": "Jae-sang"
125+
}
126+
</person>
127+
</people>
128+
`)
129+
130+
assert.deepEqual(parsed, {
131+
people: {
132+
person: [
133+
{
134+
firstName: "Rick",
135+
lastName: "Astley"
136+
},
137+
{
138+
firstName: "Park",
139+
lastName: "Jae-sang"
140+
}
141+
]
125142
}
126143
})
127144
})

0 commit comments

Comments
 (0)