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
- Parse a valid xml with the
toJson method, the method must have the reversible and alternateTextNode options.
- 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);
}
Expected Behavior
toXmlmethod should be able to reverse the JSON with different keys in the text nodesCurrent Behavior
When using the
toJsonmethod with thereversibleand ʻalternateTextNode` option set to true, the response returned by the method is not reversible with the toXml method.Possible Solution
add the
alternateTextNodesoption in theToXmlclass and use this option to evaluate if the element is a text nodeSteps to Reproduce
toJsonmethod, the method must have thereversibleandalternateTextNodeoptions.toXmlmethod.Possible Implementation
File
lib/json2xml.jsToXml classFile
lib/json2xml.jsline 39