★ wanayoo — archive 1999 https://github.com/NaturalIntelligence/fast-xml-parser/pull/237Nouvelle recherche | Portail wanayoo
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add supressEmptyNodeWithoutAttributes option #237

Open
wants to merge 1 commit into
base: master
from

Conversation

@iteixeira
Copy link

iteixeira commented Mar 30, 2020 •

Purpose / Goal

I needed to convert a JSON attribute with an empty value to a not self closing XML tag:

Input:

{
  "name": "",
  "address": ""
}

Expected output:

<name></name><address></address>

Actual output:

<name/><address/>

Benchmark results

Results of the node benchmark\perfTest3.js command, as requested at CONTRIBUTING.md:

Before changes:

Running Suite: XML Parser benchmark
validation : 22506.73362810279 requests/second
xml to json : 23379.649527832113 requests/second
xml to json + json string : 20339.19984773564 requests/second
xml to json string : 3201.794682753688 requests/second
xml2js : 5631.943439669823 requests/second

After changes:

Running Suite: XML Parser benchmark
validation : 22660.97418380873 requests/second
xml to json : 22143.350711227682 requests/second
xml to json + json string : 19650.172211612786 requests/second
xml to json string : 3152.241109975055 requests/second
xml2js : 5766.8773762699975 requests/second

Type

Please mention the type of PR

  • [ ]Bug Fix
  • [ ]Refactoring / Technology upgrade
  • [X]New Feature
@iteixeira iteixeira force-pushed the iteixeira:add-supress-with-attr-option branch from 0c9e587 to 5be611e Mar 30, 2020
@guardrails
Copy link

guardrails bot commented Mar 30, 2020

⚠️ We detected security issues in this pull request:

Vulnerable Libraries (2)
  • handlebars@4.4.0 upgrade to >=3.0.8 <4.0.0 || >=4.5.3
  • qs@2.3.3 upgrade to >=6.0.4 <6.1.0 || >=6.1.2 <6.2.0 || >=6.2.3 <6.3.0 || >=6.3.2

More info on how to fix Vulnerable Libraries in Javascript.


👉 Go to the dashboard for detailed results.

📥 Happy? Share your feedback with us.

@iteixeira iteixeira changed the title feat: add supressEmptyNodeWithoutAttributes to options feat: add supressEmptyNodeWithoutAttributes option Mar 30, 2020
@guardrails
Copy link

guardrails bot commented Mar 30, 2020 •

All previously detected findings have been fixed. Good job! 👍🎉

We will keep this comment up-to-date as you go along and notify you of any security issues that we identify.


👉 Go to the dashboard for detailed results.

📥 Happy? Share your feedback with us.

@iteixeira iteixeira force-pushed the iteixeira:add-supress-with-attr-option branch from 5be611e to ed63f94 Mar 30, 2020
@amitguptagwl
Copy link
Member

amitguptagwl commented Apr 2, 2020

What output do you get when you set supressEmptyNode: false without your changes?

@iteixeira
Copy link
Author

iteixeira commented Apr 2, 2020

If I set supressEmptyNode: false, I'll get something like:

JSON:

{
  "person": {
    "@_name: "John"
  },
  "address": ""
}

XML:

<person "name"="John"></person>
<address></address>

But I need the XML to be like:

<person "name"="John"/>
<address></address>

To get the result above, I have to set supressEmptyNode: true and supressEmptyNodeWithoutAttributes: false, then it won't supreess tags without attributes (<address></address>).

@amitguptagwl
Copy link
Member

amitguptagwl commented Apr 3, 2020

Thanks. Now it is more clear to me. However, the option name is still confusing supressEmptyNodeWithoutAttributes. It may be understood as an option to suppress a node excluding its attributes. I'll have to think more on both options.

@iteixeira
Copy link
Author

iteixeira commented Apr 3, 2020 •

Yes, thinking about it now, it's confuse. Sorry about that. But I didn't think a better name for it. What do you think if I create an supressEmptyNodeConditional or supressEmptyNodeConditionalProcessor, where you could set a function that receive the node infos as parameters (key, value and attrs), and it returns a boolean value? With that, I can suppress the node conditionally.

Example:

const j2xParser = new parser.j2xParser({
  ignoreAttributes: false,
  attributeNamePrefix: '@_',
  supressEmptyNode: true,
  supressEmptyNodeConditionalProcessor: (key, value, attrs) => attrs === '',
});

In that way, I think there will be no doubt about the option's name meaning, and it will work for what I need.

@amitguptagwl
Copy link
Member

amitguptagwl commented Apr 5, 2020

What do you think about emptyNodeProcessor? If it can decide

  1. if a node without attributes should be converted to self closing node.
  2. if a node with attributes should be converted to self closing node.
  3. if a node should be assigned with some default value
    etc.

However, I strongly feel that if supressEmptyNode:true then a node with or without attributes should convert to self-closing tag instead of another option. It may confuse people.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants
You can’t perform that action at this time.