Issue with List of Map
Closed this issue · 6 comments
I have the following yaml file:
Chat:
#This is a list of maps
Announcements:
- Sound: CAT_HISS
Permission: none
list:
- a. string
list: not actually a list
# -------------------------------------------------------------------------------------------------
# You can specify broadcasts (how many you want) and
# later you can send them to the game with a command.
# -------------------------------------------------------------------------------------------------
Broadcast:
enabled: true
I compared the output to SnakeYaml's output:
public static void main(String[] args) throws IOException {
InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("config.yml");
YamlMapping defaultMapping = Yaml.createYamlInput(inputStream).readYamlMapping();
System.out.println("eo-yaml:\n" + defaultMapping.value("Chat"));
System.out.println();
Map<Object, Object> map = new org.yaml.snakeyaml.Yaml().load(Main.class.getClassLoader().getResourceAsStream("config.yml"));
System.out.println("SnakeYaml:\n" + map.get("Chat"));
}
The output I got is the following:
eo-yaml:
# This is a list of maps
Announcements:
-
Sound: CAT_HISS
Permission: none
list:
- a. string
SnakeYaml:
{Announcements=[{Sound=CAT_HISS, Permission=none, list=[a. string]}], list=not actually a list, Broadcast={enabled=true}}
As you can see in the output SnakeYaml contains list and Broadcast, but eo-yaml doesn't.
If I do the following:
System.out.println(defaultMapping.value("Chat").asMapping().value("list"));
The following is printed out, which is correct:
---
not actually a list
...
Am I doing something wrong or is this a bug?
@tchristofferson thank you for reporting this. I'll assign someone to take care of it soon.
@amihaiemil I couldn't find any assignee for this task. This is either because there are no contributors with role DEV
available or because the project does not have enough funds.
Please, make sure there is at least one available contributor with the required role and the project can afford to pay them.
@tchristofferson Your YAML is not formatted correctly. This is how it should be formatted. Indentation in YAML is quite important, it dictates the parent-child relationship.
Chat:
#This is a list of maps
Announcements:
- Sound: CAT_HISS
Permission: none
list:
- a. string
list: not actually a list
# -------------------------------------------------------------------------------------------------
# You can specify broadcasts (how many you want) and
# later you can send them to the game with a command.
# -------------------------------------------------------------------------------------------------
Broadcast:
enabled: true
Everything under the Announcements
should be 2 spaces more to the right.
eo-yaml used to have even stricter indentation rules, but we made them more lightweight with recent releases. However, it still mandates that the indentation be respected.
According to the YAML indentation rules, this YAML is invalid:
Chat:
#This is a list of maps
Announcements:
- Sound: CAT_HISS
Permission: none
list:
- a. string
Because it would actually be the Chat
mapping, with a value which is both an array and a mapping? Announcements:
as an empty key and - Sound:
as the first element of a sequence. The problem here is that YAML should also support keys with no value, so in this case it is ambiguous. Is - Sound
the child/value of Announcements
(we don't know because it's not indented properly), or does Announcements have an empty value?
I've added a unit test here to prove this.
If you don't mind, I'd close this Issue as we certainly will not modify this indentation logic anytime soon. Please feel free to open other Issues if you still have problems. :D
@amihaiemil I still think this is a bug. There is a stack overflow post about this here: https://stackoverflow.com/questions/5953338/indenting-a-yaml-sequence-inside-a-mapping
@tchristofferson I'll have a look. In the meantime, either indent your YAML as indicated, or you could also implement your own YamlMapping and YamlSequence based on SnakeYaml. There are only a few simple methods to implement, the rest are interface default methods.
@tchristofferson I just released version 7.0.7
which also handles poor-indentation cases like the one above. Please, open other issues if you still find problems.