Skip to content
This repository has been archived by the owner on Jan 7, 2021. It is now read-only.

Options reversible and alternateTextNode are not compatible #198

Open
tsackit opened this issue Oct 16, 2020 · 0 comments
Open

Options reversible and alternateTextNode are not compatible #198

tsackit opened this issue Oct 16, 2020 · 0 comments

Comments

@tsackit
Copy link

tsackit commented Oct 16, 2020

Expected Behavior

toXml method should be able to reverse the JSON with different keys in the text nodes

Current Behavior

When using the toJson method with the reversible and ʻalternateTextNode` option set to true, the response returned by the method is not reversible with the toXml method.

Possible Solution

add the alternateTextNodes option in the ToXml class and use this option to evaluate if the element is a text node

Steps to Reproduce

  1. Parse a valid xml with the toJson method, the method must have the reversible and alternateTextNode options.
  2. The returned object must be passed to the toXml method.

Possible Implementation

File lib/json2xml.js ToXml class

function ToXml(options) {
    var defaultOpts = {
        sanitize: false,
        ignoreNull: false,
        alternateTextNode: false,
    };

    if (options) {
        for (var opt in options) {
            defaultOpts[opt] = options[opt];
        }
    }

    this.options = defaultOpts;
    this.xml = '';
    this.tagIncomplete = false;
    
    if(typeof this.options.alternateTextNode === "boolean" ) {
      thist.textNodeKey = this.options.alternateTextNode ? "_t" : "$t";
    } else if (typeof this.options.alternateTextNode === "string") {
      this.textNodeKey = this.options.alternateTextNode;
    } else {
     // or set by default "$t"
      throw new Error("Expected string or boolean value for the alternateTextNode option");
    }
}

File lib/json2xml.js line 39

if (key == this.textNodeKey) {
  self.addTextContent(subVal);
} else {
  self.addAttr(key, subVal);
}
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant