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

Options reversible and alternateTextNode are not compatible #198

Description

@snnjnn

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);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions