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

Error saving a serializable List #52

Closed
EverNife opened this issue Feb 3, 2022 · 4 comments
Closed

Error saving a serializable List #52

EverNife opened this issue Feb 3, 2022 · 4 comments
Labels

Comments

@EverNife
Copy link

EverNife commented Feb 3, 2022

Saving a serializable List will generate a corrupted yml file.

For example, editing this test over here
https://github.com/Carleslc/Simple-YAML/blob/master/Simple-Yaml/src/test/java/org/simpleyaml/examples/YamlSerializationExample.java#L41-L46

to this:

// Write an object to the YAML file
final Person p = new Person("12345678A", "John", 1990);
final Person p2 = new Person("12345678B", "Maria", 1990);

yamlFile.set("test.people." + p.getDni(), p);

yamlFile.set("test.peoples", Arrays.asList(
        p, p2
));

// Don't forget to save the file!
yamlFile.save();

Will result in this yml.file

test:
  people:
    12345678A: &id001
      ==: org.simpleyaml.examples.Person
      dni: 12345678A
      name: John
      birthYear: 1990
      isAlive: true
  peoples:
    - *id001
    -
    ==: org.simpleyaml.examples.Person
    dni: 12345678B
    name: Maria
    birthYear: 1990
    isAlive: true

Which is not a valid YAMLfile! Its missing indentation on it.
It should be something like this:

...
  peoples:
    - *id001
    -
      ==: org.simpleyaml.examples.Person
      dni: 12345678B
      name: Maria
      birthYear: 1990
      isAlive: true

From what i tested, loading a serializable list after manully fixing this identation problem works normaly.

@Carleslc Carleslc added the bug label Feb 3, 2022
@Carleslc Carleslc added this to Next release (1.7.2) in Roadmap Feb 3, 2022
@Carleslc Carleslc removed this from Next release (1.7.3) in Roadmap Feb 3, 2022
@Carleslc Carleslc added this to Next release (1.7.3) in Roadmap Feb 3, 2022
@Carleslc
Copy link
Owner

Carleslc commented Feb 3, 2022

Seems related to #43.

With snakeyaml indicatorIndent to 0 (i.e. with no list indentation at all) it works as expected:

...
  peoples:
  - *id001
  - ==: org.simpleyaml.examples.Person
    dni: 12345678B
    name: Maria
    birthYear: 1990
    isAlive: true

This is valid YAML because - character is considered to be indentation as per the YAML spec (see this comment).

This was the prior default behaviour, but it was changed in 1.7.1 to improve indentation consistency, so now indicatorIndent is set to the same value as indent (2 by default).

With newer versions of snakeyaml it seems to be a valid alternative for lists of maps using the same value for both indent and list indentation (snakeyaml issue 416), so I will update the default options to set this indentWithIndicator option.

An indentList option will be added to YamlConfigurationOptions to change the list elements indentation if desired.

@EverNife
Copy link
Author

EverNife commented Feb 3, 2022

You are right!

...yml
  peoples:
  - *id001
  - ==: org.simpleyaml.examples.Person
    dni: 12345678B
    name: Maria
    birthYear: 1990
    isAlive: true

Is a valid YML, But

  peoples:
    - *id001
    -
    ==: org.simpleyaml.examples.Person
    dni: 12345678B
    name: Maria
    birthYear: 1990
    isAlive: true

is not!

I think for now is easier to add an option to YamlConfigurationOptions to set setIndicatorIndent
And change the default value to 0 rather than the 2 that is now

@Carleslc
Copy link
Owner

Carleslc commented Feb 3, 2022

I have just released 1.7.3 with the YamlConfigurationOptions::indentList option to change the indicatorIndent (list elements indentation).

It is still 2 by default, but now it should work properly for serialized objects and maps with the indentWithIndicator change.

You can set yamlFile.options().indentList(0) if you prefer having no extra indentation for your list elements.

Now your example output is valid YAML without any addition:

test:
  people:
    12345678A: &id001
      ==: org.simpleyaml.examples.Person
      dni: 12345678A
      name: John
      birthYear: 1990
      isAlive: true
peoples:
  - *id001
  - ==: org.simpleyaml.examples.Person
    dni: 12345678B
    name: Maria
    birthYear: 1990
    isAlive: true

@EverNife
Copy link
Author

EverNife commented Feb 4, 2022

Thankyou, its properly fixed :D

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

No branches or pull requests

2 participants