Leonidas-from-XIV / node-xml2js

XML to JavaScript object converter.
MIT License
4.88k stars 602 forks source link

Feature Request: Ignore specified object's attributes in XML Builder or ignore functions #543

Closed pkozuchowski closed 2 years ago

pkozuchowski commented 4 years ago

I need to serialize only part of JSON object to XML. The other attributes are metadata I need for processing.

I thought I could do that by hiding attributes in function, but the function is also serialized to xml using .toString() (I have doubts about usefulness of this behavior)

This is what I'd like to achieve:

const builder = new xml2js.Builder({
    rootName: 'Something',
    ignoreAttrs = ['type']
});

const serializedObject = {
    attr1: '1',
    attr2: '2',
    type: 'metadataType',

    type2: function(){
        return 'metadataType'
    }
};

Expected:

<Something>
    <attr1>1</attr1>
    <attr2>2</attr2>
</Something>

Actual:

<Something>
    <attr1>1</attr1>
    <attr2>2</attr2>
    <type>metadataType</type>
    <type2>function (){
        return 'metadataType'
    }</type2>
</Something>

Alternatively, processor functions for builder would be nice.

Leonidas-from-XIV commented 2 years ago

I would suggest removing the attributes to that should not be serialized. It seems nonsensical to me to pass an object and say "but don't actually serialize half of it".

(I do agree however that serializing functions as strings makes very little sense, these should be filtered out)