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

XMLParserConfiguration needs to be opened up. #541

Closed
stleary opened this issue Jul 20, 2020 · 4 comments · Fixed by #543
Closed

XMLParserConfiguration needs to be opened up. #541

stleary opened this issue Jul 20, 2020 · 4 comments · Fixed by #543

Comments

@stleary
Copy link
Owner

stleary commented Jul 20, 2020

High priority - at least one pull request is waiting on this.
See comments in #540 and #412
Existing XMLParserConfiguration constructors to be deprecated.
Add property setters.
Add default constructor.

@johnjaylward
Copy link
Contributor

I don't think public setters on the configuration object is a good idea. For instance, if someone wanted to parse 2 or more XML files on different threads they could do something like this:

void main() {
  XMLParserConfiguration myConfig = new XMLParserConfiguration();
  myConfig.option1 = someSetting;
  myConfig.option2 = someOtherSetting;

  ConcurrentList<JSONObject> cl = new ConcurrentList<JSONObject>();

  XmlFileList.stream().parallel().foreach((f) -> cl.add(processFile(f, myConfig)));
  // do something with the completed list of JSONObjects
}

JSONObject processFile(File f, XMLParserConfiguration config) {
  if (f.getName().contains(something)) {
    // this will incorrectly be set the wrong file. It's not thread safe but is not at-a-glance wrong here.
    // however, the "config" option is used by multiple threads
    config.option1 = someSetting3; 
  }
  try (FileReader fr = new FileReader(f)) {
    return XML.toJSON(fr, myConfig);
  }
}

What I would prefer is instead to use a withOption and leave the values as private final.

What this could mean is that we still have a single simple constructor, but building up your config would have more GC overhead.

using the example above, similar code would look list this:

void main() {
  XMLParserConfiguration myConfig = new XMLParserConfiguration();
  myConfig = myConfig.withOption1(someSetting);
  myConfig = myConfig.withOption2(someOtherSetting);

  ConcurrentList<JSONObject> cl = new ConcurrentList<JSONObject>();

  XmlFileList.stream().parallel().foreach((f) -> cl.add(processFile(f, myConfig)));
  // do something with the completed list of JSONObjects
}

JSONObject processFile(File f, XMLParserConfiguration config) {
  if (f.getName().contains(something)) {
    // this will correctly be set for only this file. It's thread safe and is at-a-glance correct.
    config = config.withOption1(someSetting3); 
  }
  try (FileReader fr = new FileReader(f)) {
    return XML.toJSON(fr, myConfig);
  }
}

@stleary
Copy link
Owner Author

stleary commented Jul 21, 2020

That could work. What if someone needs to override multiple defaults on a single config?
For example, not executing in parallel, if the XML needs to configure keepStrings, convertNilAttributeToNull, and typeCastClass in a single XMLParserConfiguration instance?

@johnjaylward
Copy link
Contributor

johnjaylward commented Jul 21, 2020

XMLParserConfiguration myConfig = new XMLParserConfiguration()
    .withOption1(setting1)
    .withOption2(setting2)
    .withOption3(setting3)
;

the with* methods would all return the new configuration, so you can chain them like a builder.

@stleary
Copy link
Owner Author

stleary commented Jul 21, 2020

Sounds good! Do you want to add the code?

johnjaylward pushed a commit to johnjaylward/JSON-java that referenced this issue Jul 21, 2020
Updates XML configuration to use a builder pattern instead of
constructors with many parameters
@stleary stleary closed this as completed Jul 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants