forked from Mermade/jgeXml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml2json.js
162 lines (138 loc) · 3.76 KB
/
xml2json.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
'use strict';
var util = require('util');
var jgeXml = require('./jgeXml.js');
function filterInt(value) {
if (/^(\-|\+)?([0-9]+|Infinity)$/.test(value)) return Number(value);
return NaN;
}
function filterFloat(value) {
if(/^(\-|\+)?([0-9]*(\.[0-9]+)?|Infinity)$/.test(value)) return Number(value);
return NaN;
}
function emit(token,coerceTypes) {
if (coerceTypes) {
var timestamp = Date.parse(token);
if (!isNaN(timestamp) && (token.match('^[0-9]{4}\-[0-9]{2}\-[0-9]{2}.*$'))) {
return token;
}
var num = filterFloat(token);
if (!isNaN(num)) {
return num;
}
num = filterInt(token); //parseInt
if (!isNaN(num)) {
return num;
}
if ((token === 'true') || (token === 'false')) {
return token === 'true';
}
if ((Object.keys(token).length === 0) || (token == 'xsi:nil')) {
return 'null';
}
}
return token;
}
function getString() {
// deprecated
return '';
}
function postProcess(obj,parent,options) {
for (var key in obj) {
// skip loop if the property is from prototype
if (!obj.hasOwnProperty(key)) continue;
var propArray = Array.isArray(obj[key]);
if (propArray && obj[key].length == 1) {
obj[key] = obj[key][0];
}
if ((typeof obj[key] == 'object') && (parent !== '')) {
var firstKey = Object.keys(obj[key])[0];
if ((firstKey == options.textName) || (firstKey == options.valName)) {
if ((Object.keys(obj[key]).length == 1) && (typeof obj[key][firstKey] != 'object')) {
obj[key] = obj[key][firstKey];
}
}
}
if (typeof obj[key] == 'object') {
postProcess(obj[key],key,options);
}
}
return obj;
}
function parseString(xml,options) {
var stack = [];
var context = {};
var lastElement = '';
var defaults = {
attributePrefix: "@",
textName: '#text',
valName: '#value',
valueProperty: false,
coerceTypes: false
};
options = Object.assign({},defaults,options); // merge/extend
var obj = {};
var newCursor = obj;
var cursor = obj;
var currentElementName = '';
var currentAttributeName = '';
var index = -1;
jgeXml.parse(xml,function(state,token) {
if (state == jgeXml.sElement) {
var parentElementName = currentElementName;
context = {};
context.cursor = newCursor;
context.parent = cursor;
context.index = index;
context.elementName = currentElementName;
stack.push(context);
cursor = newCursor;
currentElementName = token;
if (newCursor[currentElementName]) {
var n = {};
newCursor[currentElementName].push(n);
index = newCursor[currentElementName].length-1;
newCursor = n;
}
else {
newCursor[currentElementName] = [{}]; // we start off assuming each element is an object in an array not just a property
newCursor = newCursor[currentElementName][0];
index = 0;
}
}
else if ((state == jgeXml.sContent) || (state == jgeXml.sCData)) {
token = emit(token,options.coerceTypes);
var target = cursor[currentElementName][index][options.textName];
if (!target) {
target = cursor[currentElementName][index][options.textName] = [];
}
var nt = {};
nt[options.valName] = token;
target.push(nt);
}
else if (state == jgeXml.sEndElement) {
// finish up
context = stack[stack.length-1];
currentElementName = context.elementName;
newCursor = context.cursor;
cursor = context.parent;
index = context.index;
stack.pop();
}
else if (state == jgeXml.sAttribute) {
currentAttributeName = options.attributePrefix+token;
}
else if (state == jgeXml.sValue) {
token = emit(token,options.coerceTypes);
cursor[currentElementName][index][currentAttributeName] = token;
}
});
if (!options.valueProperty) {
obj = postProcess(obj,'',options); // first pass
obj = postProcess(obj,'',options); // second pass
}
return obj;
}
module.exports = {
xml2json : parseString,
getString : getString
};