node-js2xmlparser icon indicating copy to clipboard operation
node-js2xmlparser copied to clipboard

typeHandlers [object Boolean] doesn't work in 4.0.0

Open darksabrefr opened this issue 6 years ago • 3 comments

Hello,

Previously in 3.0.0, using a type handler for boolean field does correct things, it was transforming booleans into what you want. But it seems that, in some cases with 4.0.0, the booleans are stringified, so the type handler for them is not triggered.

darksabrefr avatar May 31 '19 11:05 darksabrefr

Can you provide an example?

michaelkourlas avatar Jul 05 '19 12:07 michaelkourlas

I'm going to close this for now, but if you provide an example I'll re-open the issue.

michaelkourlas avatar Feb 01 '20 22:02 michaelkourlas

Long time after, here's a reproductible example in v4.0.2. The problem is located at https://github.com/michaelkourlas/node-js2xmlparser/blob/master/src/main.ts#L176 : stringify(value) is sometimes called too early, before typeHandlers are handled. Description with examples:

const js2xmlparser = require('js2xmlparser');
const obj = {
	tag: true
};
const options = {
	typeHandlers: {
		'[object Boolean]': bool => bool ? 'Y' : 'N'
	}
};
console.log(js2xmlparser.parse('root', obj, options));

outputs as intended :

<?xml version='1.0'?>
<root>
    <tag>Y</tag>
</root>

but

const js2xmlparser = require('js2xmlparser');
const obj = {
	tag: {
		'#': true
	}
};
const options = {
	typeHandlers: {
		'[object Boolean]': bool => bool ? 'Y' : 'N'
	}
};
console.log(js2xmlparser.parse('root', obj, options));

outputs, which is not intended :

<?xml version='1.0'?>
<root>
    <tag>true</tag>
</root>

I suppose this bug occurs for any type of data, not only boolean.

darksabrefr avatar Feb 14 '22 13:02 darksabrefr