@@ -51,34 +51,34 @@ function startElement(name, attrs) {
51
51
}
52
52
53
53
function text ( data ) {
54
- currentObject [ '$t' ] = ( currentObject [ '$t' ] || '' ) + data ;
54
+ currentObject [ textNodeName ( ) ] = ( currentObject [ textNodeName ( ) ] || '' ) + data ;
55
55
}
56
56
57
57
function endElement ( name ) {
58
- if ( currentObject [ '$t' ] ) {
58
+ if ( currentObject [ textNodeName ( ) ] ) {
59
59
if ( options . trim ) {
60
- currentObject [ '$t' ] = currentObject [ '$t' ] . trim ( )
60
+ currentObject [ textNodeName ( ) ] = currentObject [ textNodeName ( ) ] . trim ( )
61
61
}
62
62
63
63
if ( options . sanitize ) {
64
- currentObject [ '$t' ] = sanitizer . sanitize ( currentObject [ '$t' ] , true ) ;
64
+ currentObject [ textNodeName ( ) ] = sanitizer . sanitize ( currentObject [ textNodeName ( ) ] , true ) ;
65
65
}
66
66
67
- currentObject [ '$t' ] = coerce ( currentObject [ '$t' ] , name ) ;
67
+ currentObject [ textNodeName ( ) ] = coerce ( currentObject [ textNodeName ( ) ] , name ) ;
68
68
}
69
69
70
70
if ( currentElementName !== name ) {
71
- delete currentObject [ '$t' ] ;
71
+ delete currentObject [ textNodeName ( ) ] ;
72
72
}
73
73
// This should check to make sure that the name we're ending
74
74
// matches the name we started on.
75
75
var ancestor = ancestors . pop ( ) ;
76
76
if ( ! options . reversible ) {
77
- if ( ( '$t' in currentObject ) && ( Object . keys ( currentObject ) . length == 1 ) ) {
77
+ if ( ( textNodeName ( ) in currentObject ) && ( Object . keys ( currentObject ) . length == 1 ) ) {
78
78
if ( ancestor [ name ] instanceof Array ) {
79
- ancestor [ name ] . push ( ancestor [ name ] . pop ( ) [ '$t' ] ) ;
79
+ ancestor [ name ] . push ( ancestor [ name ] . pop ( ) [ textNodeName ( ) ] ) ;
80
80
} else {
81
- ancestor [ name ] = currentObject [ '$t' ] ;
81
+ ancestor [ name ] = currentObject [ textNodeName ( ) ] ;
82
82
}
83
83
}
84
84
}
@@ -112,6 +112,10 @@ function coerce(value,key) {
112
112
return value ;
113
113
}
114
114
115
+ function textNodeName ( ) {
116
+ return options . alternateTextNode ? typeof options . alternateTextNode === 'string' ? options . alternateTextNode : '_t' : '$t'
117
+ }
118
+
115
119
116
120
/**
117
121
* Parses xml to json using node-expat.
@@ -124,6 +128,10 @@ function coerce(value,key) {
124
128
* characterized by the presence of the property $t.
125
129
* - sanitize_values: If true, the parser escapes any element value in the xml
126
130
* that has any of the following characters: <, >, (, ), #, #, &, ", '.
131
+ * - alternateTextNode (boolean OR string):
132
+ * If false or not specified: default of $t is used
133
+ * If true, whenever $t is returned as an end point, is is substituted with _t
134
+ * it String, whenever $t is returned as an end point, is is substituted with the String value (care advised)
127
135
*
128
136
* @return {String|Object } A String or an Object with the JSON representation
129
137
* of the XML.
@@ -147,7 +155,8 @@ module.exports = function(xml, _options) {
147
155
coerce : joi . alternatives ( [ joi . boolean ( ) , joi . object ( ) ] ) . default ( false ) ,
148
156
sanitize : joi . boolean ( ) . default ( true ) ,
149
157
trim : joi . boolean ( ) . default ( true ) ,
150
- arrayNotation : joi . alternatives ( [ joi . boolean ( ) , joi . array ( ) ] ) . default ( false )
158
+ arrayNotation : joi . alternatives ( [ joi . boolean ( ) , joi . array ( ) ] ) . default ( false ) ,
159
+ alternateTextNode : [ joi . boolean ( ) . default ( false ) , joi . string ( ) . default ( false ) ]
151
160
} ;
152
161
var validation = joi . validate ( _options , schema ) ;
153
162
hoek . assert ( validation . error === null , validation . error ) ;
@@ -171,6 +180,6 @@ module.exports = function(xml, _options) {
171
180
172
181
//See: http://timelessrepo.com/json-isnt-a-javascript-subset
173
182
json = json . replace ( / \u2028 / g, '\\u2028' ) . replace ( / \u2029 / g, '\\u2029' ) ;
174
-
183
+
175
184
return json ;
176
185
} ;
0 commit comments