Carleslc / Simple-YAML

This Java API provides an easy-to-use way to store data and provide configurations using the YAML format.
https://carleslc.me/Simple-YAML
GNU General Public License v3.0
130 stars 38 forks source link

YamlFile#load & #loadWithComments behave different on double quote value #79

Open Ancash opened 11 months ago

Ancash commented 11 months ago

Version: 1.8.4 i have a yaml file:

test:
- "&8%plugin_so-sum_{cat}_{sub-cat}%&8 in &8%plugin_so-count_{cat}_{sub-cat}%&8 offers"

when i load it with YamlFile#load everything is fine. when i load it with YamlFile#loadWithComments the file now looks like this:

test: offers'    offers'

this is not the behaviour i expected because there are no "#" denoting a comment

Ancash commented 11 months ago

Using the following produces not the expected output (replaces the input with the output mentioned in the post before)

YamlFile yaml = new YamlFile();
yaml.options().useComments(true);
yaml.set("test", Arrays.asList("&8%plugin_so-sum_{cat}_{sub-cat}%&8 in &8%plugin_so-count_{cat}_{sub-cat}%&8 offers"));
System.out.println(yaml.saveToString());

but the following snippets do

YamlFile yaml = new YamlFile();
yaml.set("test", Arrays.asList("&8%plugin_so-sum_{cat}_{sub-cat}%&8 in &8%plugin_so-count_{cat}_{sub-cat}%&8 offers"));
System.out.println(yaml.saveToString());
YamlFile yaml = new YamlFile();
yaml.set("test", "&8%plugin_so-sum_{cat}_{sub-cat}%&8 in &8%plugin_so-count_{cat}_{sub-cat}%&8 offers");
System.out.println(yaml.saveToString());
YamlFile yaml = new YamlFile();
yaml.options().useComments(true);
yaml.set("test", "&8%plugin_so-sum_{cat}_{sub-cat}%&8 in &8%plugin_so-count_{cat}_{sub-cat}%&8 offers");
System.out.println(yaml.saveToString());
Ancash commented 11 months ago

somehow leaving out the &8 makes it work

YamlFile yaml = new YamlFile();
yaml.options().useComments(true);
yaml.set("test", Arrays.asList("%plugin_so-sum_{cat}_{sub-cat}% in %plugin_so-count_{cat}_{sub-cat}% offers"));
System.out.println(yaml.saveToString());
Carleslc commented 11 months ago

According to your examples seems to be an issue with list elements having some special characters while using the comment parser. With a simple string value does not happen 🤔

& is used for key references in yaml (anchors and aliases): https://yaml.org/spec/1.2.2/#692-node-anchors Maybe is something related to that.

Try these other snippets independently and see if the same happens:

Ancash commented 11 months ago
YamlFile yaml = new YamlFile();
yaml.options().useComments(true);
yaml.set("test",Arrays.asList("&8%plugin_so-sum_{cat}_{sub-cat}%&8 in &8%plugin_so-count_{cat}_{sub-cat}%&8 offers"), QuoteStyle.DOUBLE);
System.out.println(yaml.saveToString());

outputs

test:

    \ offers"    \ offers"
YamlFile yaml = new YamlFile();
yaml.options().useComments(true);
yaml.set("test",Arrays.asList("&8%plugi_so-sum_{cat}_{sub-cat}%&8 in &8%plugin_so-count_{cat}_{sub-cat}%&8 offers"));
System.out.println(yaml.saveToString());

outputs

test:
  - '&8%plugi_so-sum_{cat}_{sub-cat}%&8 in &8%plugin_so-count_{cat}_{sub-cat}%&8 offers'
YamlFile yaml = new YamlFile();
yaml.options().useComments(true);
yaml.set("test",Arrays.asList("&8%plugin_so-sum%&8 in &8%plugin_so-count%&8 offers"));
System.out.println(yaml.saveToString());

outputs

test:
  - '&8%plugin_so-sum%&8 in &8%plugin_so-count%&8 offers'
YamlFile yaml = new YamlFile();
yaml.options().useComments(true);
yaml.set("test",Arrays.asList("abcd&8%plugin_so-sum_{cat}_{sub-cat}%&8 in &8%plugin_so-count_{cat}_{sub-cat}%&8 offers"));
System.out.println(yaml.saveToString());

outputs

test:
  - abcd&8%plugin_so-sum_{cat}_{sub-cat}%&8 in &8%plugin_so-count_{cat}_{sub-cat}%&8
    offers
YamlFile yaml = new YamlFile();
yaml.options().useComments(true);
yaml.set("test",Arrays.asList("&8%plugin_so-sum_%cat%_%sub-cat%%&8 in &8%plugin_so-count_%cat%_%sub-cat%%&8 offers"), QuoteStyle.DOUBLE);
System.out.println(yaml.saveToString());

outputs

test:

    \ offers"    \ offers"
YamlFile yaml = new YamlFile();
yaml.options().useComments(true);
yaml.set("test",Arrays.asList("&8%plugin_so-sum_cat%_%sub-cat%%&8 in &8%plugin_so-count_%cat%_%sub-cat%%&8 offers"));
System.out.println(yaml.saveToString());

outputs

test:
  - '&8%plugin_so-sum_cat%_%sub-cat%%&8 in &8%plugin_so-count_%cat%_%sub-cat%%&8 offers'
Ancash commented 10 months ago

using the following works in 1.7.3 but not in 1.8.1 1.8.2 1.8.3

YamlFile yaml = new YamlFile();
yaml.set("test",Arrays.asList("&8%plugin_so-sum_%cat%_%sub-cat%%&8 in &8%plugin_so-count_%cat%_%sub-cat%%&8 offers"));
yaml = YamlFile.loadConfiguration(new StringReader(yaml.saveToString()), true);
System.out.println(yaml.saveToString());
Ancash commented 10 months ago

Using SnakeYamlImplementation works. After some more testing it seems like SimpleYamlImplementation is having trouble with dumping multi line list elements.

DumperOptions dop = new DumperOptions();
dop.setWidth(10);
YamlFile yaml = new YamlFile();
yaml.set("test",Arrays.asList("&8%plugin_so-sum_%cat%_%sub-cat%%&8 in &8%plugin_so-count_%cat%_%sub-cat%%&8 offers"),   QuoteStyle.DOUBLE);
String savedToString = yaml.saveToString();
System.out.println(savedToString);
yaml = new YamlFile(new SimpleYamlImplementation(new LoaderOptions(), dop));
yaml.options().useComments(true);
yaml.loadFromString(savedToString);
System.out.println(yaml.saveToString());

outputs

test:
  - "&8%plugin_so-sum_%cat%_%sub-cat%%&8 in &8%plugin_so-count_%cat%_%sub-cat%%&8\
    \ offers"

test:

    in &8%plugin_so-count_%cat%_%sub-cat%%&8
    offers'    offers'

where as

DumperOptions dop = new DumperOptions();
dop.setWidth(100);
YamlFile yaml = new YamlFile();
yaml.set("test",Arrays.asList("&8%plugin_so-sum_%cat%_%sub-cat%%&8 in &8%plugin_so-count_%cat%_%sub-cat%%&8 offers"),   QuoteStyle.DOUBLE);
String savedToString = yaml.saveToString();
System.out.println(savedToString);
yaml = new YamlFile(new SimpleYamlImplementation(new LoaderOptions(), dop));
yaml.options().useComments(true);
yaml.loadFromString(savedToString);
System.out.println(yaml.saveToString());

outputs

test:
  - "&8%plugin_so-sum_%cat%_%sub-cat%%&8 in &8%plugin_so-count_%cat%_%sub-cat%%&8\
    \ offers"

test:
  - '&8%plugin_so-sum_%cat%_%sub-cat%%&8 in &8%plugin_so-count_%cat%_%sub-cat%%&8 offers'