This repository was archived by the owner on Jan 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 212
Options reversible and alternateTextNode are not compatible #198
Copy link
Copy link
Open
Description
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
toJsonmethod, the method must have thereversibleandalternateTextNodeoptions. - The returned object must be passed to the
toXmlmethod.
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);
}Metadata
Metadata
Assignees
Labels
No labels